This Docker Compose setup provides a PostgreSQL database with SSL/TLS encryption and automatic certificate monitoring.
- ✅ PostgreSQL 16 with SSL/TLS enabled
- ✅ Automatic certificate file monitoring
- ✅ Automatic database restart when certificates change
- ✅ Dokploy-compatible configuration
- Docker and Docker Compose installed
- SSL certificate files:
certs/fullchain.pem- Full certificate chaincerts/privkey.pem- Private key
-
Create certificate directory and add your certificates:
mkdir -p certs # Copy your fullchain.pem and privkey.pem to the certs/ directory cp /path/to/your/fullchain.pem certs/ cp /path/to/your/privkey.pem certs/ -
Set proper permissions for certificate files:
chmod 644 certs/fullchain.pem chmod 600 certs/privkey.pem
-
Configure environment variables:
cp .env.example .env # Edit .env with your desired PostgreSQL credentials -
Start the services:
docker compose up -d
PostgreSQL can reload SSL certificates without a full restart using pg_reload_conf() or by sending a SIGHUP signal. When certificate files are replaced and PostgreSQL is signaled to reload, the new certificates will take effect.
The docker-compose.yml includes a cert-watcher service that monitors certificate files (fullchain.pem and privkey.pem) for changes. When either file is modified, it automatically calls pg_reload_conf() to reload the SSL certificates without restarting the container.
Benefits:
- No container restart required (zero downtime)
- No Docker socket mounting needed (more secure)
- Automatic certificate reloading
- Compatible with Dokploy and restricted environments
If you prefer to manually reload certificates, you can use docker-compose.simple.yml and run:
# Reload configuration (including SSL certificates)
docker exec postgres-ssl psql -U postgres -c "SELECT pg_reload_conf();"
# Or send SIGHUP signal
docker kill -s SIGHUP postgres-sslPostgreSQL is configured to:
- Require SSL connections
- Use TLS 1.2 or higher
- Use the mounted certificate files for SSL/TLS
psql "postgresql://user:password@localhost:5432/dbname?sslmode=require"postgresql://POSTGRES_USER:POSTGRES_PASSWORD@localhost:POSTGRES_PORT/POSTGRES_DB?sslmode=require
This setup is compatible with Dokploy. When deploying:
- Ensure the certificate files are available in the
certs/directory - Set environment variables in Dokploy's environment configuration
- Deploy using the
docker-compose.ymlfile
docker exec postgres-ssl psql -U postgres -c "SHOW ssl;"docker logs postgres-ssldocker logs postgres-cert-watcherdocker exec postgres-ssl psql -U postgres -c "SELECT version();"- Keep your
.envfile secure and never commit it to version control - Ensure certificate files have proper permissions (600 for private key, 644 for certificate)
- Use strong passwords for PostgreSQL
- Consider using environment variables or secrets management in production