Dumps the entire PostgreSQL cluster, including roles, to one compressed file.
3 views
Share
This script captures an entire PostgreSQL cluster in a single timestamped, compressed file. Unlike pg_dump, pg_dumpall exports not just every database but also cluster-wide objects such as roles, tablespaces, and grants, making it the right tool for a complete disaster-recovery dump. After set -euo pipefail and argument handling, it verifies that pg_dumpall is installed, builds a timestamped output path, and pipes the dump through gzip. Authentication deliberately relies on ~/.pgpass or peer authentication rather than embedding a password, which keeps secrets out of the script and process list. The connecting role defaults to postgres. Run it as ./pg_backup.sh /backups/postgres or ./pg_backup.sh /backups/postgres admin. Example:
Backing up PostgreSQL cluster as user 'postgres' -> /backups/postgres/pg_cluster-20260604-101530.sql.gz
Done. Size: 88M
Restore with gunzip -c file.sql.gz | psql -U postgres.