Configure SSL/TLS for PostgreSQL
PostgreSQL supports TLS, but it is off in many installs and clients default to sslmode=prefer (PostgreSQL's own docs call that default not recommended for secure deployments, PostgreSQL libpq SSL docs), which silently falls back to plain text. Encrypting database traffic matters whenever queries cross a network you do not fully control, including cloud VPCs shared with other services. This guide covers the server, access rules, client verification and testing.
Step 1: Get a server certificate
Use a certificate for the hostname clients will connect to. A public CA certificate works if the database has a real DNS name, and clients can verify it with the system trust store. For internal-only names, use a private CA and distribute its certificate to clients. To issue a free certificate, see Certbot basics. Remember that Let's Encrypt validation needs the name to be reachable, or DNS validation.
Step 2: enable SSL on the PostgreSQL server
# postgresql.conf
ssl = on
ssl_cert_file = '/etc/ssl/pg/fullchain.pem'
ssl_key_file = '/etc/ssl/pg/privkey.pem'
ssl_min_protocol_version = 'TLSv1.2'PostgreSQL refuses to start if the key file has permissions that are too open. The file must be owned by the postgres user with mode 0600, or owned by root with mode 0640 and a group the server belongs to. Changing the certificate files does not need a restart; pg_ctl reload or SELECT pg_reload_conf(); is enough, though changing ssl itself may need a restart on older versions.
Step 3: Force TLS in pg_hba.conf
Turning SSL on only makes it available. To require it, use hostssl rules and remove or reject plain host rules:
# TYPE DATABASE USER ADDRESS METHOD
hostssl all all 10.0.0.0/8 scram-sha-256
hostnossl all all 0.0.0.0/0 rejectRules are read top to bottom, so the order matters. Use scram-sha-256 for password authentication, not md5, where your clients allow it. Always keep a rule that lets you connect locally, or a mistake can lock you out.
Step 4: Choose the right sslmode on clients
| sslmode | Encrypts | Verifies certificate | Verifies hostname |
|---|---|---|---|
| disable | No | No | No |
| prefer (default) | If server offers | No | No |
| require | Yes | No | No |
| verify-ca | Yes | Yes | No |
| verify-full | Yes | Yes | Yes |
Only verify-full protects against someone impersonating the server. require encrypts but would accept any certificate, including an attacker's. Connect like this:
psql "host=db.example.com dbname=app user=app_user \
sslmode=verify-full sslrootcert=/etc/ssl/pg/ca.pem"
# PostgreSQL 16 or later, public CA in the system store
psql "host=db.example.com dbname=app user=app_user \
sslmode=verify-full sslrootcert=system"The sslrootcert=system value exists from PostgreSQL 16; older clients need a path to a CA file. Check what your driver and version support before relying on it.
Step 5: confirm the PostgreSQL connection is encrypted with SSL
SELECT ssl, version, cipher
FROM pg_stat_ssl
WHERE pid = pg_backend_pid();This returns t and the protocol version when your session is encrypted. For a view across all clients, join pg_stat_ssl to pg_stat_activity and look for sessions with ssl = f. You can also inspect the certificate from outside with OpenSSL, using -starttls postgres.
Managed databases and replication
Managed PostgreSQL services usually supply their own CA certificate and enable TLS for you; check the provider's current documentation for the exact CA download and whether SSL is enforced. Replication connections need the same treatment: set sslmode=verify-full in the standby's primary_conninfo.
Renewals
After renewal copy the new files to the paths PostgreSQL uses, with correct ownership, then reload. A deploy hook handles this; see automating renewal with Certbot. Also read protecting SSL private keys, since a database key deserves the same care as a web key.
Common mistakes
- Enabling
ssl = onand assuming connections are now encrypted. Clients default topreferand nothing forces them. - Using
sslmode=requireand believing it prevents impersonation. - Key file permissions that are too open, which makes the server refuse to start.
- Connecting by IP address, which fails
verify-fullunless the IP is in the certificate.
Related reading: protecting SSL private keys, SSL for MySQL and SSL for FTP servers.
Frequently Asked Questions
What is the difference between sslmode=require and verify-full?
require encrypts but may not check the certificate name. verify-full checks the chain and the hostname, and PostgreSQL recommends it for security-sensitive deployments.
Does PostgreSQL need a public certificate?
Only if clients verify it with the system trust store and the database has a public name. Internal names normally use a private CA whose certificate you distribute.
Does replication use the same settings?
No. Replication connections are configured separately on the primary, so set sslmode there as well if you want the stream encrypted.
✅ Post-Installation Verification & SEO Checklist
After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:
- Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
- Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
- Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
- Confirm both root and
wwwversions resolve cleanly to one canonical HTTPS URL. - Verify your HTTPS property and
sitemap.xmlinside Google Search Console and Bing Webmaster Tools.