For production purposes, we recommend using publicly trusted certificates issued by a Certificate Authority (CA).
Components
- Postgres image built by Minimus: Postgres container configured with the secure configuration for client authentication.
- psql installed
- Dynamic certificate generation via OpenSSL:
- certgen.sh script: Shell script that generates a custom CA, server, and client certificates using OpenSSL.
- minidebug image: A Minimus dev toolkit that provides a shell, OpenSSL, and other utilities used to generate the certificates.
What this guide demonstrates
- TLS handshake validation
- Server/client certificate trust
- Basic auth and Postgres operations
- Image compatibility
Directory Structure
Deploy Postgres with TLS certificates
Step 1: Generate TLS certificates
1
Save script that generates TLS certificates
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.certgen.sh
2
Save Docker Compose configuration
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.create-certs.yml
3
Generate certificates
Run the following to generate the certificates:
- Self-signed CA certificate (
ca.pem) - Server certificates (
server-cert.pem,server-key.pem) with SANs:postgres,localhost,127.0.0.1, and192.168.30.3 - Client certificates for
testuser(client.pem,client-key.pem)
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.
Step 2: Deploy Postgres server
1
Save custom entrypoint
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
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.docker-compose.yml
4
Run Postgres
Start the Postgres container:
Step 3: Test your Postgres server
You can use psql to connect over TLS and run tests. For example, here are a few commands you can try out:-
Connect to the db:
-
Show all schemas in current database:
-
Show server version:
-
Check that TLS is active: