Store certificate and key
sudo mkdir -p /etc/postgresql/ssl sudo cp server.crt server.key /etc/postgresql/ssl/ sudo chown postgres:postgres /etc/postgresql/ssl/server.key sudo chmod 600 /etc/postgresql/ssl/server.key
PostgreSQL does not start if the key is readable by others. Allowed are 0600 owned by postgres or 0640 owned by root with group postgres. server.crt contains the certificate followed by the intermediates. If you have a PFX, the certificate converter splits it into PEM files.
postgresql.conf
ssl = on ssl_cert_file = '/etc/postgresql/ssl/server.crt' ssl_key_file = '/etc/postgresql/ssl/server.key' ssl_min_protocol_version = 'TLSv1.2' #ssl_ca_file = '/etc/postgresql/ssl/ca.crt' # only for client certificates
The settings are applied without a restart:
sudo -u postgres psql -c "SELECT pg_reload_conf();"
Enforce TLS in pg_hba.conf
host allows encrypted and unencrypted connections. hostssl only allows TLS:
# TYPE DATABASE USER ADDRESS METHOD hostssl all all 10.0.0.0/8 scram-sha-256 hostnossl all all 0.0.0.0/0 reject
With the method cert instead of scram-sha-256, clients log in with their certificate. The user name is then the CN of the client certificate, and ssl_ca_file is required.
Configure clients properly: sslmode
The default prefer encrypts if the server offers it, but checks neither CA nor name. Only verify-full is secure:
psql "host=db.example.com dbname=app user=app sslmode=verify-full sslrootcert=/etc/ssl/certs/corp-ca.pem" # certificate from a public CA (PostgreSQL 16 and later): use the system store psql "host=db.example.com dbname=app user=app sslmode=verify-full sslrootcert=system"
Without sslrootcert, libpq looks for the CA in ~/.postgresql/root.crt. Most drivers use the same names, e.g. JDBC with sslmode=verify-full&sslrootcert=….
Check
-- in psql: which connections use TLS? SELECT a.usename, a.client_addr, s.ssl, s.version, s.cipher FROM pg_stat_ssl s JOIN pg_stat_activity a USING (pid); # from outside (OpenSSL 1.1.1 or later) openssl s_client -starttls postgres -connect db.example.com:5432 -showcerts
When connecting, psql also shows a line like SSL connection (protocol: TLSv1.3, …).
Renew, also with Let's Encrypt
Copy the new files to the same place, set owner and permissions, then SELECT pg_reload_conf(); or systemctl reload postgresql. With certbot exactly this belongs in the deploy hook, because PostgreSQL may not read the files under /etc/letsencrypt directly due to their permissions. From PostgreSQL 17, clients with sslnegotiation=direct can start TLS without the preceding negotiation, which saves a round trip when connecting.
FAQ
Why does the connection work even though the certificate has expired?
Because the client uses sslmode=prefer or require and doesn’t check the certificate at all. With verify-full this is noticed immediately.
Debian already configured a certificate. Is that enough?
That is the system’s snakeoil certificate, self-signed and with the host name. For verify-full you need a certificate with the name clients use to reach the server.
Which sslmode is right for applications?
verify-full, with the right CA. verify-ca only checks the CA, not the name; require only encrypts.