Skip to content

PostgreSQL SSL/TLS Quick Reference

Quick reference for PostgreSQL SSL/TLS implementation in HeliosDB-Lite.

Quick Start

1. Generate Certificates

Terminal window
mkdir -p certs
openssl req -x509 -newkey rsa:2048 -nodes \
-keyout certs/server.key \
-out certs/server.crt \
-days 365 \
-subj "/CN=localhost"

2. Start the Server with TLS

Terminal window
export HELIOSDB_JWT_SECRET="$(openssl rand -hex 32)"
heliosdb-lite start --data-dir ./mydb \
--tls-cert certs/server.crt --tls-key certs/server.key

--tls-cert and --tls-key must be given together. The server then offers TLS to every client that asks for it.

3. Connect with psql

Terminal window
psql "sslmode=require host=127.0.0.1 port=5432 user=postgres"

Client SSL Modes

The server accepts TLS when a client requests it. Enforce TLS from the client side with sslmode:

sslmodeBehaviourUse Case
disableNever use TLSLocal development only
preferUse TLS when offered (libpq default)Migration
requireFail if TLS is not availableProduction
verify-carequire + verify the server certificate chainHigh security
verify-fullverify-ca + verify the host nameMaximum security

Client-certificate authentication is planned.

Protocol Flow

SSL Negotiation

  1. Client → Server: SSLRequest (code: 80877103)
  2. Server → Client: ‘S’ (accepted) or ‘N’ (rejected)
  3. Both: TLS handshake (if accepted)
  4. Client → Server: Startup message (over TLS)

Message Structure

SSLRequest Message:
┌─────────────┬─────────────────┐
│ Length (4B) │ SSL Code (4B) │
│ 0x00000008 │ 0x04D2162F │
└─────────────┴─────────────────┘
Server Response:
┌──────────┐
│ 'S' or 'N'│
└──────────┘

Testing

Test with psql

Terminal window
# Test SSL connection
psql "sslmode=require host=127.0.0.1 port=5432 user=postgres"
# Verify SSL in psql
\conninfo
# Should show: SSL connection (protocol: TLSv1.3, cipher: ...)

Security Checklist

Production deployment checklist:

  • Use CA-signed certificates (not self-signed)
  • Set sslmode=require or stricter in every client
  • Restrict file permissions (600 for keys, 644 for certs)
  • Use strong authentication (--auth scram-sha-256)
  • Rotate certificates before expiration
  • Test certificate chain validity
  • Configure firewall rules

Common Commands

Certificate Management

Terminal window
# Generate self-signed certificate
openssl req -x509 -newkey rsa:2048 -nodes \
-keyout server.key -out server.crt -days 365 \
-subj "/CN=localhost"
# View certificate details
openssl x509 -in server.crt -text -noout
# Check certificate expiration
openssl x509 -in server.crt -noout -dates
# Verify certificate
openssl verify -CAfile ca.crt server.crt
# Set permissions
chmod 600 server.key
chmod 644 server.crt

Testing Commands

Terminal window
# Test SSL connection
psql "sslmode=require host=127.0.0.1 port=5432 user=postgres"
# Test without SSL (should fail if required)
psql "sslmode=disable host=127.0.0.1 port=5432 user=postgres"
# Test with certificate verification
psql "sslmode=verify-ca sslrootcert=ca.crt host=127.0.0.1 port=5432 user=postgres"
# Connection string format
psql "postgresql://postgres@127.0.0.1:5432/db?sslmode=require"

Performance Considerations

  • SSL/TLS adds ~2-5ms latency per connection establishment
  • Minimal overhead for data transfer (AES-GCM hardware acceleration)
  • Connection pooling recommended for high-throughput applications

Troubleshooting

Certificate Not Found

Terminal window
# Check file exists
ls -la certs/
# Regenerate if needed
openssl req -x509 -newkey rsa:2048 -nodes \
-keyout certs/server.key -out certs/server.crt \
-days 365 -subj "/CN=localhost"

Permission Denied

Terminal window
# Fix permissions
chmod 600 certs/server.key
chmod 644 certs/server.crt

TLS Handshake Failed

Terminal window
# Check certificate validity
openssl x509 -in certs/server.crt -noout -dates
# Verify certificate format
openssl x509 -in certs/server.crt -text -noout

Enable Debug Logging

Terminal window
export HELIOSDB_JWT_SECRET="$(openssl rand -hex 32)"
RUST_LOG=debug heliosdb-lite start --data-dir ./mydb \
--tls-cert certs/server.crt --tls-key certs/server.key

References

Support

For issues or questions:

  1. Enable debug logging: RUST_LOG=debug