A guide to setting up Postgres and testing that it accepts TLS connections, enforces authentication, and allows secure read/write operations from a test client
Use this file to discover all available pages before exploring further.
The following guide will help you deploy the Minimus Postgres image with self-signed, locally issued certificates to help you get started. Run the code to try it for yourself.
For production purposes, we recommend using publicly trusted certificates issued by a Certificate Authority (CA).
.├── certgen.sh # Certificate generation script├── create-certs.yml # Compose file to run certgen container├── entrypoint.sh # Custom PostgreSQL entrypoint to set permissions├── pg_hba.conf # Custom pg_hba config to require SSL and client certs└── docker-compose.yml # Compose file to run PostgresDB
Save the following script to a file named certgen.sh. The script is used to generate the TLS certificates and store them in a certs folder on the host.
Save the following YAML file to run with Docker Compose. It uses the Minimus minidebug image to generate the certificates with the certgen.sh shell script. Minidebug is a Minimus dev toolkit that provides a shell, OpenSSL, and other utilities. The certificates will be persisted in the certs volume on the host.
Congrats! You have just generated the following self-signed certificates:
Self-signed CA certificate (ca.pem)
Server certificates (server-cert.pem, server-key.pem) with SANs: postgres, localhost, 127.0.0.1, and 192.168.30.3
Client certificates for testuser(client.pem, client-key.pem)
Client private key permissions are set to 0600 and owned by UID 1000. Certificate permissions are adjusted to support non-root containers. In the next steps, you will mount these certificates into the Postgres container.
#!/bin/bashset -e# Fix ownership so postgres can use the private keyecho "[INFO] Fixing ownership and permissions of /certs..."chown postgres:postgres /certs/server-key.pemecho "[INFO] Fixing file permissions..."# Restrict private key accesschmod 600 /certs/server-key.pemecho "[INFO] Launching PostgreSQL..."exec docker-entrypoint.sh "$@"
Make sure the entrypoint script is executable on your host. If necessary, give it execute permissions:
chmod +x ./entrypoint.sh
2
Save custom HBA
PostgreSQL’s default access rules are defined in the file pg_hba.conf. Save the following configuration to mount it and customize the Host-Based Authentication rules.
pg_hba.conf
# Allow local socket connections without SSL (for internal psql commands)local all all trust# Allow readonly_user to connect using password over SSLhostssl all readonly_user 0.0.0.0/0 scram-sha-256# Allow remote SSL connections with client cert validationhostssl all all 0.0.0.0/0 cert clientcert=verify-full
3
Save Docker Compose script
Save the following Docker Compose script to a file named docker-compose.yml. This script sets up the Postgres service with a healthcheck, mounts a volume with the certificates, the custom entrypoint and the custom HBA config, and maps port 5432.