Blueprints · Docker & Databases
PostgreSQL 17 production hardening and encrypted S3 backups
Listens on localhost, requires SCRAM-SHA-256, turns on SSL, and dumps the database through GPG before an S3 or R2 upload. A success ping is skipped until you replace the heartbeat URL.
Live config
Baked-in protections
- listen_addresses is localhost
- Passwords use scram-sha-256
- Remote TCP without SSL is rejected
- The dump is encrypted before it leaves the host
- The backup script is not run by the bundle
Step 1 · ini
Server settings
/root/portgate-blueprints/postgres-portgate.conf
Drop this file into conf.d. SSL needs a certificate. The apply script creates a localhost certificate only when PostgreSQL is already installed.
listen_addresses = 'localhost' password_encryption = scram-sha-256 ssl = on ssl_min_protocol_version = 'TLSv1.2' log_connections = on log_disconnections = on
Step 2 · conf
Client authentication
/root/portgate-blueprints/pg_hba.conf
Replace the host lines in pg_hba.conf with this set. Local sockets still need a password.
local all postgres peer local all all scram-sha-256 hostssl all all 127.0.0.1/32 scram-sha-256 hostssl all all ::1/128 scram-sha-256 host all all 0.0.0.0/0 reject host all all ::/0 reject
Step 3 · bash
Encrypted dump
/root/portgate-blueprints/postgres-backup.sh
Create /root/portgate-backup.pass with mode 600 before the first run. aws or rclone must already be able to write to the bucket.
#!/bin/sh set -eu test -s /root/portgate-backup.pass umask 077 STAMP="$(date +%Y%m%d%H%M%S)" DUMP="/var/backups/app-$STAMP.sql.gpg" mkdir -p /var/backups sudo -u postgres pg_dump --dbname "app" | gpg --batch --yes --pinentry-mode loopback --passphrase-file /root/portgate-backup.pass --symmetric --cipher-algo AES256 -o "$DUMP" aws s3 cp "$DUMP" "s3://backups/postgres/" find /var/backups -name 'app-*.sql.gpg' -mtime +14 -delete if [ "https://example.com/heartbeat" != "https://example.com/heartbeat" ]; then curl -fsS -m 15 "https://example.com/heartbeat" fi
Step 4 · bash
Install the snippets when PostgreSQL is present
/root/portgate-blueprints/postgres-apply.sh
Exits cleanly when the server package is not installed yet, so a bundle can continue.
#!/bin/sh set -eu if [ ! -f /etc/postgresql/17/main/postgresql.conf ]; then echo "PostgreSQL 17 is not installed. The conf files are under /root/portgate-blueprints for later." exit 0 fi install -d -m 755 /etc/postgresql/17/main/conf.d install -m 644 /root/portgate-blueprints/postgres-portgate.conf /etc/postgresql/17/main/conf.d/portgate.conf echo "pg_hba.conf is staged at /root/portgate-blueprints/pg_hba.conf. Review it before you replace the live file." if [ ! -s /etc/postgresql/17/main/server.crt ]; then openssl req -new -x509 -days 3650 -nodes -subj "/CN=localhost" -keyout /etc/postgresql/17/main/server.key -out /etc/postgresql/17/main/server.crt chown postgres:postgres /etc/postgresql/17/main/server.key /etc/postgresql/17/main/server.crt chmod 600 /etc/postgresql/17/main/server.key fi systemctl reload postgresql || true