pgbun supports SSL/TLS encryption for both client and server connections, providing secure communication between clients, pgbun, and PostgreSQL servers. The implementation supports multiple TLS modes and certificate-based authentication.
pgbun supports the following TLS modes for client connections:
disable: SSL/TLS is disabled (default)allow: SSL/TLS is optional, fallback to plain connection if SSL failsprefer: SSL/TLS is preferred, fallback to plain connection if SSL failsrequire: SSL/TLS is required, reject connections without SSLverify-ca: SSL/TLS is required and server certificate must be verified against CAverify-full: SSL/TLS is required and server certificate must be verified against CA with hostname check
For connections to PostgreSQL servers, pgbun supports:
disable: SSL/TLS is disabledallow: SSL/TLS is optional, fallback to plain connection if SSL failsprefer: SSL/TLS is preferred, fallback to plain connection if SSL fails (default)require: SSL/TLS is required, reject connections without SSLverify-ca: SSL/TLS is required and server certificate must be verified against CAverify-full: SSL/TLS is required and server certificate must be verified against CA with hostname check
# Enable client TLS
pgbun --client-tls-mode require
# Enable server TLS with certificate files
pgbun --server-tls-mode verify-ca --server-tls-ca-file /path/to/ca.crt
# Full TLS configuration
pgbun \
--client-tls-mode require \
--client-tls-key-file /path/to/client.key \
--client-tls-cert-file /path/to/client.crt \
--server-tls-mode prefer \
--server-tls-ca-file /path/to/ca.crt[tls]
# Client TLS settings
client_tls_mode = "require"
client_tls_key_file = "/path/to/client.key"
client_tls_cert_file = "/path/to/client.crt"
client_tls_ca_file = "/path/to/ca.crt"
# Server TLS settings
server_tls_mode = "prefer"
server_tls_key_file = "/path/to/server.key"
server_tls_cert_file = "/path/to/server.crt"
server_tls_ca_file = "/path/to/ca.crt"export PGBUN_CLIENT_TLS_MODE=require
export PGBUN_CLIENT_TLS_KEY_FILE=/path/to/client.key
export PGBUN_CLIENT_TLS_CERT_FILE=/path/to/client.crt
export PGBUN_CLIENT_TLS_CA_FILE=/path/to/ca.crt
export PGBUN_SERVER_TLS_MODE=prefer
export PGBUN_SERVER_TLS_CA_FILE=/path/to/ca.crtWhen client_tls_mode is set to require, verify-ca, or verify-full, you must provide:
client_tls_key_file: Private key file (PEM format)client_tls_cert_file: Certificate file (PEM format)client_tls_ca_file: CA certificate file (PEM format, required for verify modes)
When server_tls_mode is set to require, verify-ca, or verify-full, you must provide:
server_tls_ca_file: CA certificate file (PEM format)server_tls_key_file: Private key file (PEM format, optional)server_tls_cert_file: Certificate file (PEM format, optional)
pgbun implements the PostgreSQL SSL request protocol:
- Client connects to pgbun
- Client sends SSL request (if TLS is enabled)
- pgbun responds with 'S' (SSL allowed) or 'N' (SSL denied)
- TLS handshake occurs if SSL is allowed
- Regular PostgreSQL protocol continues over encrypted connection
# Enable TLS for both client and server connections
pgbun \
--client-tls-mode prefer \
--server-tls-mode prefer \
--server-tls-ca-file /etc/ssl/certs/ca-certificates.crt[tls]
# Require TLS for client connections
client_tls_mode = "require"
client_tls_key_file = "/etc/pgbun/client.key"
client_tls_cert_file = "/etc/pgbun/client.crt"
client_tls_ca_file = "/etc/pgbun/ca.crt"
# Prefer TLS for server connections
server_tls_mode = "prefer"
server_tls_ca_file = "/etc/pgbun/ca.crt"# Generate self-signed certificates
openssl req -x509 -newkey rsa:4096 -keyout client.key -out client.crt -days 365 -nodes
openssl req -x509 -newkey rsa:4096 -keyout server.key -out server.crt -days 365 -nodes
# Configure pgbun with self-signed certificates
pgbun \
--client-tls-mode require \
--client-tls-key-file client.key \
--client-tls-cert-file client.crt \
--server-tls-mode prefer \
--server-tls-ca-file server.crt# Connect with TLS
psql "host=localhost port=6432 dbname=mydb user=myuser sslmode=require"
# Connect with TLS and client certificate
psql "host=localhost port=6432 dbname=mydb user=myuser sslmode=require sslcert=client.crt sslkey=client.key sslrootcert=ca.crt"const { Client } = require('pg');
const client = new Client({
host: 'localhost',
port: 6432,
database: 'mydb',
user: 'myuser',
ssl: {
rejectUnauthorized: true,
ca: fs.readFileSync('ca.crt'),
cert: fs.readFileSync('client.crt'),
key: fs.readFileSync('client.key')
}
});import psycopg2
import ssl
conn = psycopg2.connect(
host='localhost',
port=6432,
database='mydb',
user='myuser',
sslmode='require',
sslcert='client.crt',
sslkey='client.key',
sslrootcert='ca.crt'
)-
Certificate not found
Error: TLS mode is enabled, but key/cert files are not configured.Solution: Ensure certificate files exist and paths are correct.
-
SSL handshake failed
Error: Failed to upgrade to TLSSolution: Check certificate validity and compatibility.
-
CA verification failed
Error: Server certificate verification failedSolution: Ensure CA certificate is correct and server certificate is signed by the CA.
Enable verbose logging to troubleshoot TLS issues:
pgbun --verbose --client-tls-mode requireTest certificate validity:
# Check certificate
openssl x509 -in client.crt -text -noout
# Verify certificate chain
openssl verify -CAfile ca.crt client.crt
# Test TLS connection
openssl s_client -connect localhost:6432 -cert client.crt -key client.key -CAfile ca.crt- Certificate Management: Store private keys securely and use proper file permissions (600)
- CA Validation: Use
verify-caorverify-fullin production environments - Certificate Rotation: Implement certificate rotation procedures
- Network Security: TLS provides encryption but consider additional network security measures
- Key Storage: Consider using hardware security modules (HSMs) for key storage in high-security environments
TLS adds computational overhead:
- CPU: Encryption/decryption operations
- Memory: TLS session state
- Latency: Additional handshake round-trips
For high-performance scenarios, consider:
- Using
prefermode to allow fallback to plain connections - Monitoring TLS performance metrics
- Using hardware acceleration when available
pgbun TLS implementation is compatible with:
- PostgreSQL 12+ (SSL support)
- Standard PostgreSQL clients (psql, libpq, etc.)
- Popular database drivers (Node.js pg, Python psycopg2, etc.)
- Standard TLS 1.2+ protocols