Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

Encrypting Database Connections (TLS)

Direct answer

Encrypting database connections with TLS protects credentials and data in transit; servers present certificates and clients verify them.

When to use this

Use it when securing database traffic or when TLS connections fail.

First 30 seconds

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.

Operational verification

client → TLS handshake → certificate verified → encrypted session

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it when securing database traffic or when TLS connections fail.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

Require TLS on the server (hostssl in pg_hba, require_secure_transport, Force Encryption) and verify certificates on clients (sslmode=verify-full).

Key checks
  • SHOW ssl; — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -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 sslmodeEncryptedServer certificate checkedHostname checked
disableNo——
prefer (libpq default)If the server offers itNoNo
requireYesNo (unless a root cert file exists)No
verify-caYesYesNo
verify-fullYesYesYes

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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

Common mistakes

  • TLS enabled but clients not verifying certificates.
ReferenceConfiguration & state1 section · depth on demand

Configuration

DatabaseServerClient verification
PostgreSQLssl = on, ssl_cert_file, hostssl in pg_hbasslmode=verify-full
MySQL/MariaDBrequire_secure_transport = ON--ssl-mode=VERIFY_IDENTITY
SQL ServerForce Encryption + certificateEncrypt=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
Terminology layer

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.

Aliases that resolve here
encrypting database connections (tls)database tls encryptionsslmode verify-full
Confirmation

Confirm action

Continue?