How do you enable and verify TLS for database connections?

IntermediateConceptDatabase connections in apps
Answer

Short answer

Configure the client to require TLS and, ideally, verify the server certificate against the CA (for example the AWS RDS or Azure CA bundle). Then confirm on the server side that sessions are actually encrypted.

Client settings

Stack Setting
PostgreSQL URL / psql sslmode=require (encrypt) or sslmode=verify-full&sslrootcert=/certs/rds-ca.pem (encrypt + verify)
MySQL JDBC useSSL=true&requireSSL=true&verifyServerCertificate=true (or sslMode=VERIFY_IDENTITY)
.NET Npgsql SSL Mode=VerifyFull;Root Certificate=/certs/ca.pem
MongoDB tls=true&tlsCAFile=/certs/ca.pem

Verify it's encrypted

-- PostgreSQL
SELECT ssl, version, cipher FROM pg_stat_ssl WHERE pid = pg_backend_pid();
-- MySQL
SHOW STATUS LIKE 'Ssl_cipher';

Enforce on the server

  • RDS PostgreSQL: parameter rds.force_ssl = 1; MySQL: require_secure_transport = ON.
  • Azure Database: "Enforce SSL connection" is on by default.

Why verify-full matters

require encrypts but would accept an impostor's certificate; verify-full also checks the certificate is signed by the trusted CA and matches the hostname.

What to say in the interview

"Clients use sslmode verify-full or the equivalent with the provider's CA bundle mounted into the pod; the server enforces TLS with force_ssl or require_secure_transport; and I verify with pg_stat_ssl or Ssl_cipher."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Connecting apps to databases

More on Database connections in apps