Sub topic 10
Encrypting connections with TLS
This chapter assumes you have already set the server's listening address, the host-based rules in pg_hba.conf or MySQL account host patterns, and a firewall rule. It also assumes you can use openssl on the command line. Those controls decide who can reach the server. TLS protects the traffic after they reach it.
What TLS protects and what it does not
TLS (Transport Layer Security) encrypts the connection between client and server. Without it, passwords hashed with SCRAM, queries and result rows cross the network in readable form, so anyone on the path can capture them. TLS also lets the client check the server's identity, which blocks an impostor who has taken over the server's address.
TLS does not encrypt data on disk, and it does not replace authentication or grants. It also does nothing for local connections over a Unix socket, which never touch the network.
A common mistake is turning TLS on at the server and assuming clients use it. In both engines the server only offers TLS. The client's mode decides whether it is used and whether the certificate is checked.
Creating a certificate
A server certificate must be signed by an authority the client trusts. For a private network, run your own small certificate authority (CA). The client then trusts only that CA's certificate. The certificate must include the name or IP address clients connect to as a Subject Alternative Name (SAN). Modern clients check the SAN, not the common name.
mkdir -p ~/dbtls && cd ~/dbtls
# Certificate authority (keep ca.key off the database host afterwards)
openssl req -x509 -newkey rsa:4096 -nodes -keyout ca.key -out ca.crt \
-days 3650 -subj "/CN=Example DB CA"
# Server key and signing request
openssl req -newkey rsa:2048 -nodes -keyout server.key -out server.csr \
-subj "/CN=db.example.internal"
# Sign it, with the names clients will use
printf 'subjectAltName=DNS:db.example.internal,IP:10.0.0.5\n' > san.cnf
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
-out server.crt -days 365 -extfile san.cnf
# Verify the chain before deploying
openssl verify -CAfile ca.crt server.crtThe last command should print server.crt: OK. You distribute only ca.crt to clients. The private keys server.key and ca.key never leave the hosts that need them.
A public CA such as Let's Encrypt also works if the server has a public DNS name. The certificate then renews every few months, and the server must reload it each time.
Enabling TLS in PostgreSQL
Install the files where the postgres user can read them. PostgreSQL refuses to start if the key file is readable by other users. The permission must be 0600 when the file belongs to postgres.
sudo install -d -o postgres -g postgres -m 700 /etc/postgresql/16/main/tls
sudo install -o postgres -g postgres -m 600 server.key /etc/postgresql/16/main/tls/server.key
sudo install -o postgres -g postgres -m 644 server.crt /etc/postgresql/16/main/tls/server.crtThen set the parameters in postgresql.conf. The 16 in the path is the major version, so adjust it for your install.
ssl = on
ssl_cert_file = '/etc/postgresql/16/main/tls/server.crt'
ssl_key_file = '/etc/postgresql/16/main/tls/server.key'
ssl_min_protocol_version = 'TLSv1.2'A reload is enough, because these settings can change without a restart:
sudo systemctl reload postgresqlEncryption is enforced in pg_hba.conf. A host line matches both encrypted and plain connections. A hostssl line matches only encrypted ones, and a hostnossl line matches only unencrypted ones. The hostssl rule from the earlier chapter therefore rejects a client that tries to connect without TLS. Remember that rules are read top to bottom and the first match wins, so a broader host line above it would defeat the point.
Enabling TLS in MySQL
MySQL 8 generates a self-signed certificate in the data directory on first start and uses it automatically. That gives encryption but no identity check. To use your own certificate, copy the files somewhere the mysql user can read them and name them in the [mysqld] section:
[mysqld]
ssl_ca = /etc/mysql/tls/ca.crt
ssl_cert = /etc/mysql/tls/server.crt
ssl_key = /etc/mysql/tls/server.key
tls_version = TLSv1.2,TLSv1.3
require_secure_transport = ONrequire_secure_transport = ON makes the server reject every TCP connection that is not encrypted. Unix socket connections are exempt. Apply the change with sudo systemctl restart mysql. On MySQL 8.0.16 and later you can instead run ALTER INSTANCE RELOAD TLS; to reread the files without a restart.
To require TLS for one account instead of the whole server, use the account setting:
ALTER USER 'appuser'@'10.0.0.%' REQUIRE SSL;Choosing the client mode
The client's mode sets how much it trusts the server. Only the strictest modes protect against an impostor.
| Meaning | PostgreSQL `sslmode` | MySQL `--ssl-mode` | Protects against eavesdropping | Protects against impostor |
|---|---|---|---|---|
| Plain text allowed | `disable` | `DISABLED` | No | No |
| Try TLS, fall back to plain | `prefer` (psql default) | `PREFERRED` (default) | Only if no downgrade | No |
| Encrypt, skip certificate check | `require` | `REQUIRED` | Yes | No |
| Check the CA signature | `verify-ca` | `VERIFY_CA` | Yes | Partly |
| Check the CA and the hostname | `verify-full` | `VERIFY_IDENTITY` | Yes | Yes |
Use verify-full or VERIFY_IDENTITY for applications. The default modes can silently fall back to plain text.
psql "host=db.example.internal dbname=appdb user=appuser sslmode=verify-full sslrootcert=/etc/myapp/ca.crt"
mysql -h db.example.internal -u appuser -p --ssl-mode=VERIFY_IDENTITY --ssl-ca=/etc/myapp/ca.crt appdbThe hostname in the command must match the SAN. Connecting to 10.0.0.5 works only because the certificate lists that IP.
Confirming that a session is encrypted
Check from the server side, not just from the client's options. In PostgreSQL, pg_stat_ssl shows the state of every connection:
SELECT pid, ssl, version, cipher FROM pg_stat_ssl WHERE pid = pg_backend_pid();In MySQL, read the session status:
SHOW SESSION STATUS LIKE 'Ssl_version';A row with ssl = t or a non-empty Ssl_version such as TLSv1.3 confirms encryption.
Common problems
- PostgreSQL will not start after the change. The log says the private key file has group or world access, or cannot be read. Fix the owner and set mode
0600, then readjournalctl -u postgresql. certificate verify failedon the client. The client lacksca.crt, or the CA that signed the certificate is a different one. Runopenssl verifyagain.server certificate for "x" does not match host name. The name in the connection string is missing from the SAN. Reissue the certificate, or connect using a listed name.- Connections fail suddenly after months. The certificate expired. Check with
openssl x509 -in server.crt -noout -enddateand put the date in your monitoring. - Old clients cannot connect. Raising the minimum protocol to TLS 1.2 excludes very old client libraries. Upgrade them rather than lowering the setting.
Exercise
An application connects to PostgreSQL with sslmode=require. A colleague says this fully protects against someone redirecting traffic to a fake server. Is that correct, and what would you change?
Show answer
It is not correct. require encrypts the connection but does not validate the server certificate, so a fake server presenting any certificate is accepted. Change the client to sslmode=verify-full with sslrootcert pointing to the CA certificate, and make sure the server certificate's SAN contains the hostname the application uses. The MySQL equivalent is --ssl-mode=VERIFY_IDENTITY with --ssl-ca.
Exercise
You set require_secure_transport = ON in MySQL and restart. A local script that connects through the Unix socket still works without TLS. Why, and does this weaken the setting?
Show answer
Unix socket connections do not cross the network, so MySQL treats them as secure and exempts them. This does not weaken protection for network clients, because every TCP connection without TLS is rejected.