Tools [active]

PostgreSQL Docker Manager

A Bash script that sets up PostgreSQL 18 with Docker Compose from scratch, then manages databases, users, SSL, and backups through an interactive menu.

Tech
Bash · Docker Compose · PostgreSQL · OpenSSL
License
MIT
View on GitHub ↗

Problem

A database container is easy to start and easy to leave half-configured: no TLS, a shared superuser for every app, and backups that only exist as a plan. This script makes the safe setup the default and puts the day-to-day tasks behind one menu.

What it does

  • Sets up one PostgreSQL instance (postgres:18) with Docker Compose: data, backup, certificate, and config directories, .env, and compose.yaml.
  • Generates its own pg_hba.conf: TCP connections require TLS (on by default) and scram-sha-256 authentication, with a self-signed certificate created at setup.
  • Creates a database together with its own owner user, isolated from other databases.
  • Backs up with pg_dump to PostgreSQL custom format (.dump) (validated), plus a backup of users and grants.
  • Restores from a list of available backups, locally or from Cloudflare R2.
  • Scheduled backups (systemd timer, or cron) with retention, stored locally, on R2, or both.
  • Password rotation, status, SSL checks, and logs.
  • Backup and restore of other PostgreSQL servers (another VPS or a managed service) through connection profiles, even without a local instance.

Usage

All generated files stay in the folder that holds the script, so use one folder per instance:

sudo git clone https://github.com/raqolbi/postgresql-docker-manager.git /opt/postgres
cd /opt/postgres
sudo ./postgres-manager.sh

The first menu option runs the initial setup; the rest of the menu covers daily operations.

Notes

  • Docker Engine and the Compose plugin must already be installed; the script does not install them.
  • Runs as root (or with sudo).
  • R2 uploads use rclone in a container, so nothing extra needs to be installed on the host.