Database Connection String Formats Cheat Sheet
Connection string and URI formats across major databases covering PostgreSQL, MySQL, MongoDB, Redis, and driver-specific parameters.
PostgreSQL & MySQL URIs
Standard connection URI formats for the two most common relational databases.
# PostgreSQLpostgresql://user:password@host:5432/dbname?sslmode=requirepostgres://user:password@host:5432/dbname # short scheme also accepted# Postgres with connection pooling paramspostgresql://user:pass@host:5432/dbname?sslmode=require&connect_timeout=10&application_name=api# MySQLmysql://user:password@host:3306/dbname?ssl-mode=REQUIRED# Unix socket (no host/port)postgresql://user:password@/dbname?host=/var/run/postgresql
MongoDB & Redis URIs
Connection formats for common NoSQL / in-memory stores.
# MongoDB standardmongodb://user:password@host1:27017,host2:27017/dbname?replicaSet=rs0&authSource=admin# MongoDB Atlas (SRV record, resolves hosts automatically)mongodb+srv://user:[email protected]/dbname?retryWrites=true&w=majority# Redisredis://:password@host:6379/0# Redis with TLSrediss://user:password@host:6380/0# Redis Sentinel (driver-specific, not a single URI in most clients)# sentinels: [{host: 's1', port: 26379}], name: 'mymaster'
Env Var & ORM Config Patterns
How connection strings typically flow into application config.
# .envDATABASE_URL=postgresql://app:[email protected]:5432/app_prod?sslmode=requireREDIS_URL=redis://cache.internal:6379/0# Prisma (schema.prisma)# datasource db {# provider = "postgresql"# url = env("DATABASE_URL")# }# SQLAlchemy# engine = create_engine(os.environ["DATABASE_URL"])# Node pg# const pool = new Pool({ connectionString: process.env.DATABASE_URL })
Anatomy of a Connection URI
The generic components shared across most database URI schemes.
- scheme- protocol identifier, e.g. postgresql://, mysql://, mongodb+srv://
- userinfo- user:password@ segment; must be URL-encoded if it contains special characters
- host:port- can be a comma-separated list for replica sets / clusters
- path- the database/schema name, e.g. /dbname
- query params- driver options like sslmode, authSource, replicaSet, connect_timeout
- sslmode=require vs verify-full- require encrypts but doesn't verify cert identity; verify-full also checks the CA chain and hostname
JDBC & ODBC Connection Formats
Java and ODBC drivers use different schemes and parameter syntax than the plain URI style.
# JDBC (Java) — jdbc: prefix, query-string paramsjdbc:postgresql://host:5432/dbname?ssl=true&sslmode=require&user=app&password=secretjdbc:mysql://host:3306/dbname?useSSL=true&serverTimezone=UTCjdbc:sqlserver://host:1433;databaseName=dbname;encrypt=true;trustServerCertificate=false# ODBC (DSN-less, semicolon key=value pairs)Driver={PostgreSQL Unicode};Server=host;Port=5432;Database=dbname;Uid=app;Pwd=secret;SSLmode=require;
Cloud-Managed Auth Patterns
Connection patterns specific to managed database services: IAM tokens and proxy sockets instead of static passwords.
# AWS RDS IAM auth — short-lived token instead of a static passwordaws rds generate-db-auth-token --hostname db.abc123.us-east-1.rds.amazonaws.com \ --port 5432 --username app_iam_user > /tmp/tokenpostgresql://app_iam_user:$(cat /tmp/token)@db.abc123.us-east-1.rds.amazonaws.com:5432/appdb?sslmode=verify-full# GCP Cloud SQL via Unix socket, through the Cloud SQL Auth Proxy sidecarpostgresql://app:secret@/appdb?host=/cloudsql/project:region:instance# Azure Database for PostgreSQL flexible serverpostgresql://app:[email protected]:5432/appdb?sslmode=require
Connection Pooler Strings & Caveats
PgBouncer and similar poolers front the real database; transaction-pooling mode breaks some driver features.
# App connects to the pooler, not directly to Postgrespostgresql://app:[email protected]:6432/appdb?sslmode=disable# Transaction-pooling mode caveat: server-prepared statements and# session-level SET/LISTEN don't survive across pooled connections —# disable driver-side prepared statement caching explicitlypostgresql://app:secret@pgbouncer:6432/appdb?prepareThreshold=0 # pgjdbc# Managed platforms often ship both a pooled and a direct URLDATABASE_URL=postgresql://user:[email protected]:6543/postgres?pgbouncer=trueDIRECT_URL=postgresql://user:[email protected]:5432/postgres
Advanced Connection Parameters
Driver and protocol-level options that matter once you move past a happy-path local connection.
- sslmode variants- disable/allow/prefer/require/verify-ca/verify-full form a strictness ladder; only verify-full is immune to a man-in-the-middle swap
- application_name- tags the connection so it's identifiable in pg_stat_activity and slow-query logs when tracing which service opened it
- statement_timeout / connect_timeout- server-side and client-side guards against a runaway query or a hung TCP dial to a dead host
- pool_min / pool_max (driver-side)- bounds on the application driver's own connection pool, distinct from and layered on top of a pooler like PgBouncer
- tcp_keepalives_idle- detects a connection silently dropped by a NAT gateway or load balancer that never sent a TCP RST
- options=-c search_path=...- Postgres-specific trick to set arbitrary session GUCs at connect time directly from the URI
URL-encode special characters in passwords (`@`, `:`, `/`, `#`) before putting them in a connection string — an unescaped `@` in a password is silently parsed as the host separator, causing confusing 'could not connect to host' errors instead of an auth failure.