JDBC connection guide
PostgreSQL JDBC Connection String
Build a PostgreSQL JDBC URL with SSL parameters and keep the username and password out of source code.
Use the connection value generated by JDBC, keep it in an environment variable, and test it before changing application code. The URI shape below is a template, not a credential to paste unchanged.
- URI scheme
- jdbc:postgresql://
- Default PostgreSQL port
- 5432
- Credential handling
- Environment variable or secret manager
- First test
- SELECT current_database(), current_user, ssl_is_used();
Connection string template
jdbc:postgresql://HOST:5432/DB_NAME?sslmode=requireReplace every uppercase placeholder. Percent-encode reserved characters in URI usernames and passwords.
Environment variable
JDBC_DATABASE_URL=jdbc:postgresql://HOST:5432/DB_NAME?sslmode=requireWhere to get the JDBC values
A JDBC URL starts with jdbc:postgresql://, followed by the host, optional port, and database. Supply user and password as connection properties or through your framework's secret configuration.
The failure mode worth checking first
JDBC URLs are not ordinary PostgreSQL URIs: the jdbc: prefix matters. For certificate verification, use verify-full and configure the root certificate instead of treating require as full identity verification.
Test the connection
Run a cheap read-only command before migrations or application startup. That separates network, TLS, and authentication errors from framework configuration problems.
Connection check
SELECT current_database(), current_user, ssl_is_used();Open it in PostgresGUI
Paste the PostgreSQL URI into PostgresGUI or enter the same host, port, database, user, password, and SSL mode as separate fields. A desktop connection is useful for checking the visible schemas and role permissions before debugging the application layer.
Questions that come up
Should the connection string be committed to Git?
No. Commit an example with placeholders and load the real value from an environment variable or secret manager.
Why does a password with @ or # break the URI?
Those characters have meaning inside a URL. Percent-encode the username and password, or use a structured connection API instead of assembling a URL by hand.
Do I need sslmode=require?
Most hosted PostgreSQL services require TLS. For strict certificate and hostname verification, use sslmode=verify-full with the provider's trusted root certificate when the client supports it.