Use it when securing database traffic or when TLS connections fail.
Encrypting Database Connections (TLS)
Encrypting database connections with TLS protects credentials and data in transit; servers present certificates and clients verify them.
1. Server TLS on: SHOW ssl; / SHOW VARIABLES LIKE 'have_ssl'; / Force Encryption setting.
2. Client connection encrypted? SELECT ssl FROM pg_stat_ssl; / \s in mysql.
3. Certificate valid and trusted.
4. Require TLS for remote connections.
5. Test with verify-full.
client → TLS handshake → certificate verified → encrypted session
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Connect with a client that requires verification and confirm success. Confirm unencrypted connections from remote hosts are refused.
Step 2 Common failures Narrow the fault
sslmode=require encrypts but does not verify the server, allowing interception. Expired certificates break all new connections. Replication links are often left unencrypted.
Require TLS on the server (hostssl in pg_hba, require_secure_transport, Force Encryption) and verify certificates on clients (sslmode=verify-full).
SHOW ssl;— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
The database negotiates TLS at connection start. PostgreSQL can require it per rule with hostssl in pg_hba.conf; MySQL with REQUIRE SSL on the account or require_secure_transport.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Encryption is not the same as authentication of the server
PostgreSQL sslmode | Encrypted | Server certificate checked | Hostname checked |
|---|---|---|---|
disable | No | — | — |
prefer (libpq default) | If the server offers it | No | No |
require | Yes | No (unless a root cert file exists) | No |
verify-ca | Yes | Yes | No |
verify-full | Yes | Yes | Yes |
Only verify-full stops a man-in-the-middle who presents their own certificate. MySQL clients have the same ladder: --ssl-mode=REQUIRED, VERIFY_CA, VERIFY_IDENTITY. For SQL Server use Encrypt=True (or Strict with TDS 8.0) and keep TrustServerCertificate=False.
Enforcing TLS on the server
# postgresql.conf
ssl = on
ssl_min_protocol_version = 'TLSv1.2'
# pg_hba.conf — hostssl only for network users, then reject the rest
hostssl all all 10.0.0.0/8 scram-sha-256
host all all 0.0.0.0/0 reject
ALTER USER 'shop_app'@'10.0.%' REQUIRE SSL; -- MySQL per account
SET PERSIST require_secure_transport = ON; -- MySQL server-wide
Verifying a live session
SELECT ssl, version, cipher FROM pg_stat_ssl WHERE pid = pg_backend_pid();
SHOW SESSION STATUS LIKE 'Ssl_cipher'; -- MySQL
openssl s_client -starttls postgres -connect db.example.com:5432 -servername db.example.com </dev/null | openssl x509 -noout -dates -subject
Watch certificate expiry like any other service; when the server certificate expires, every new connection fails at once. Replication and backup connections need TLS settings too.
Common mistakes
- TLS enabled but clients not verifying certificates.
ReferenceConfiguration & state1 section · depth on demand
Configuration
| Database | Server | Client verification |
|---|---|---|
| PostgreSQL | ssl = on, ssl_cert_file, hostssl in pg_hba | sslmode=verify-full |
| MySQL/MariaDB | require_secure_transport = ON | --ssl-mode=VERIFY_IDENTITY |
| SQL Server | Force Encryption + certificate | Encrypt=True;TrustServerCertificate=False |
Without certificate verification on the client, TLS protects against eavesdropping but not man-in-the-middle. Check connections: SELECT * FROM pg_stat_ssl;, SHOW SESSION STATUS LIKE 'Ssl_cipher';.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.