PostgreSQL Connection String Format
Learn how to build a PostgreSQL connection string with host, port, database, credentials, and SSL options. Covers the standard postgresql:// URI format.
Detailed Explanation
PostgreSQL URI Format
The standard PostgreSQL connection string uses the postgresql:// (or postgres://) URI scheme. The full format is:
postgresql://[user[:password]@][host][:port][/database][?param=value&...]
Breaking Down Each Component
| Component | Example | Required |
|---|---|---|
| Protocol | postgresql:// |
Yes |
| Username | postgres |
No (defaults to OS user) |
| Password | :mypassword |
No |
| Host | localhost |
No (defaults to localhost) |
| Port | :5432 |
No (defaults to 5432) |
| Database | /mydb |
No (defaults to username) |
| Parameters | ?sslmode=require |
No |
Example Connection Strings
A basic local connection:
postgresql://postgres:secret@localhost:5432/myapp
A production connection with SSL and a specific schema:
postgresql://appuser:p%40ssw0rd@db.example.com:5432/production?sslmode=require&schema=app
Password Encoding
Special characters in passwords must be percent-encoded. Common characters that need encoding:
@becomes%40:becomes%3A/becomes%2F#becomes%23%becomes%25
SSL Parameters
PostgreSQL supports several SSL modes through the sslmode parameter:
- disable — No SSL at all
- allow — Try non-SSL first, then SSL
- prefer — Try SSL first, then non-SSL (default)
- require — Only SSL, no certificate verification
- verify-ca — SSL with CA certificate verification
- verify-full — SSL with full hostname verification
For cloud-hosted PostgreSQL (AWS RDS, Google Cloud SQL, etc.), sslmode=require is typically the minimum requirement.
Use Case
Connecting a Node.js or Python application to a PostgreSQL database, whether it is a local development instance or a managed cloud database like AWS RDS or Supabase.