Symptoms
- Many applications fail to connect at the same moment, often at a round hour (the certificate's
notAftertime). - libpq clients (
psql, Pythonpsycopg) reportSSL error: certificate verify failedorcertificate has expired. - Java clients report
PKIX path validation failedorCertificateExpiredException. .NET clients report that the remote certificate is invalid. - Some clients keep working. These are usually clients using
sslmode=requireorprefer, which encrypt but do not check the certificate. - The database itself is up. Local socket connections and existing sessions work.
1. Confirm the certificate is the problem
From a client host, read the certificate the server is actually serving. OpenSSL 1.1.1 and later can do the PostgreSQL STARTTLS handshake:
openssl s_client -starttls postgres -connect db01.example.com:5432 \
-servername db01.example.com </dev/null 2>/dev/null \
| openssl x509 -noout -subject -issuer -dates -ext subjectAltName
Check notAfter. Also check subjectAltName. sslmode=verify-full fails if the host name the client uses is not in the SAN list, even when the date is fine.
On the database server, find the files PostgreSQL loads and check them directly:
SHOW ssl;
SHOW ssl_cert_file;
SHOW ssl_key_file;
SHOW ssl_ca_file;
sudo -u postgres openssl x509 -in /var/lib/pgsql/data/server.crt -noout -enddate -subject
Relative paths are relative to the data directory.
| What you see | Cause | Fix |
|---|---|---|
Served certificate notAfter is in the past | Server certificate expired | Step 2 |
| File on disk is renewed but the served one is old | Renewal ran but PostgreSQL was not reloaded, or the reload failed | Step 3 |
| Server certificate is valid, clients still fail | Intermediate or root CA expired, or client trust store is out of date | Step 4 |
| Only clients using a client certificate fail | Client certificate expired (clientcert=verify-full in pg_hba.conf) | Reissue client certificates |
| Error mentions host name mismatch | SAN does not contain the name clients use | Reissue with the correct SAN |
2. Install the renewed certificate
Get the new certificate from your CA (internal PKI, ACME client or your certificate team). Put the server certificate first in the file, followed by any intermediate certificates, so clients can build the chain:
cd /var/lib/pgsql/data
sudo cp server.crt server.crt.expired.$(date +%F)
sudo cp server.key server.key.old.$(date +%F)
cat new-server.crt intermediate.crt | sudo tee server.crt >/dev/null
sudo cp new-server.key server.key
sudo chown postgres:postgres server.crt server.key
sudo chmod 600 server.key
Check that the key matches the certificate before you reload. The two hashes must be identical:
sudo openssl x509 -in server.crt -noout -pubkey | openssl sha256
sudo openssl pkey -in server.key -pubout | openssl sha256
PostgreSQL refuses a private key that other users can read. The key must be owned by the database user with mode 0600, or owned by root with mode 0640 and group-readable by the database user.
3. Reload, do not restart
PostgreSQL rereads the SSL files on a configuration reload:
SELECT pg_reload_conf();
or sudo systemctl reload postgresql (service names vary by distribution and version, such as postgresql-16).
If the new files have an error at reload time, PostgreSQL ignores them, keeps the old SSL configuration and writes the error to the server log. Check the log right after the reload:
sudo tail -n 50 /var/lib/pgsql/data/log/postgresql-*.log | grep -iE 'ssl|certificate|private key'
Warning
Do not restart PostgreSQL to "make sure" the certificate loads. If the files are wrong, a reload keeps the old configuration, but a restart refuses to start at all and turns a partial outage into a full one. Fix the files until the reload is clean first.
Run the openssl s_client check from step 1 again and confirm the new notAfter.
Existing connections keep the certificate they started with. That does not matter for recovery, because failing clients are making new connections.
4. When the server certificate is fine but clients still fail
The chain or the client trust store is the problem.
- Expired intermediate: your CA reissued the intermediate. Rebuild
server.crtwith the current intermediate and reload. - Client trust store: clients with
verify-caorverify-fullcheck against their own root file,~/.postgresql/root.crtorsslrootcert=for libpq, or the JVM or OS trust store. Deploy the new root to the clients. - Managed services: on Amazon RDS / Aurora and Azure Database for PostgreSQL, the provider rotates the server certificate. Outages there are usually caused by an old CA bundle on the client. Download the current bundle from the provider documentation and update
sslrootcert.
For RDS, check which CA the instance uses:
aws rds describe-db-instances --db-instance-identifier prod-pg \
--query 'DBInstances[0].[CACertificateIdentifier,CertificateDetails.ValidTill]'
Emergency workaround
If a renewed certificate is hours away, an application owner can temporarily change the connection string from sslmode=verify-full to sslmode=require. Traffic stays encrypted but the server's identity is not checked.
Caution
sslmode=requireallows a man-in-the-middle to impersonate the database. Use it only with security approval, only inside a private network, and log a change to revert it once the certificate is fixed. Never switch tosslmode=disableor allowhost(non-SSL) entries inpg_hba.confas a workaround.
5. Verify recovery
Check that applications are reconnecting over SSL:
SELECT a.usename, a.application_name, a.client_addr, s.version, s.cipher
FROM pg_stat_ssl s
JOIN pg_stat_activity a ON a.pid = s.pid
WHERE s.ssl
ORDER BY a.backend_start DESC
LIMIT 20;
- New connections appear with recent
backend_starttimes. - Connection pools (PgBouncer, application pools) have recovered. Restart PgBouncer only if it terminates TLS itself with its own
client_tls_cert_file, which has a separate expiry date. - The server log has no new SSL errors.
Prevent it next time
- Monitor the certificate PostgreSQL actually serves, not the file on disk. Run the
openssl s_clientcheck from your monitoring system and alert 30 and 7 days beforenotAfter. - Automate renewal with an ACME client or your PKI agent, and add a deploy hook that copies the files, checks the key matches, runs
pg_reload_conf()and checks the log. - Track client certificates, PgBouncer certificates and CA bundles in the same inventory, with their expiry dates.
- Keep the
pg_hba.confside correct as well. See no pg_hba.conf entry authentication failures.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get