How to connect the postgress db using jdbc url

“Usually, when I connect to the PostgreSQL database, I provide the connection URL, for example:
dialect+driver://username:password@host:port/database.
However, now I want to provide the JDBC URL, password, and username separately. How should I proceed?”

Can you give us a bit more background information so we can understand what you’re trying to do? Where will the separate variables originate, and how are they going to be passed?

Hi @manics Thanks for the quick reply.Here we creating the Database is creating on the fly and stores the jdb url, jdbc password, jdbc username in vault. and we need to fetch it.

I’m not familiar with JDBC, but generally username and password can come from env variables, such as $PGUSER and $PGPASSWORD. It is up to your database driver (e.g. psycopg2) to interpret these environment variables, so that’s where you should look for documentation on what variables to set.

If your vault provisions Kubernetes secrets, you can pass the credentials via environment variables (PostgreSQL example):

hub:
  db:
    type: postgres
    url: 'postgresql+psycopg2://'
  extraEnv:
    PGDATABASE: jupyterhub
    PGHOST: postgres-service
    PGPASSWORD:
      valueFrom:
        secretKeyRef:
          key: password
          name: postgresql-credentials-demo
    PGUSER:
      valueFrom:
        secretKeyRef:
          key: username
          name: postgresql-credentials-demo

If your vault does not create Kubernetes secrets and you have to rely on additional communication (e.g., REST calls), I assume that you can create a custom JupyterHub configuration that sets the database URL after fetching the credentials.

1 Like