PostgreSQL TLS
sslmode=requiresslmode=verify-full
sslrootcert=/path/to/ca.pemSetting up Verify-Full TLS for PostgreSQL (single or multiple machine)
1
2
Issue a Server Certificate for Each PostgreSQL Host
HOST="db1.example.com" # the DNS name clients will use
IP1="1.0.1.1" # if clients will connect by IPopenssl genrsa -out server.key 4096
cat > server.cnf <<EOF
[ req ]
distinguished_name = dn
req_extensions = v3_req
prompt = no
[ dn ]
CN = ${HOST}
[ v3_req ]
extendedKeyUsage = serverAuth
subjectAltName = @alt
[ alt ]
DNS.1 = ${HOST}
$( [ -n "$IP1" ] && echo "IP.1 = ${IP1}" )
EOFopenssl req -new -key server.key -out server.csr -config server.cnf
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
-out server.crt -days 825 -sha256 -extfile server.cnf -extensions v3_req3
Install Certificates on the PostgreSQL Host
# Copy leaf + key into the Postgres data dir
cp server.crt /PATH/TO/DATADIR/server.crt
cp server.key /PATH/TO/DATADIR/server.key# key must be restricted
chown postgres:postgres /PATH/TO/DATADIR/server.{crt,key}
chmod 600 /PATH/TO/DATADIR/server.key
chmod 644 /PATH/TO/DATADIR/server.crtsudo -u postgres psql -tAc "SHOW data_directory;"sudo -u postgres ${EDITOR:-nano} "$PGDATA/postgresql.conf"ssl = on
ssl_cert_file = 'server.crt' # or an absolute path
ssl_key_file = 'server.key' # key must be chmod 600, owned by postgres
listen_addresses = '*' # or a comma list like '10.0.1.15,127.0.0.1'sudo -u postgres ${EDITOR:-nano} "$PGDATA/pg_hba.conf"# Tighten the CIDRs to your environment
hostssl fsdb fsuser 10.0.0.0/16 scram-sha-256
hostssl fsdb fsuser 192.168.0.0/24 scram-sha-256Run PostgreSQL with TLS in Docker
Init script (runs on first boot)
Bash Script
Notes
Was this helpful?