PostgreSQL Database Backup with pg_dump
Export and backup PostgreSQL databases using pg_dump, custom compressed archives, multi-threaded parallel dumps, and pg_dumpall.
Overview
pg_dump is PostgreSQL's official logical backup utility. It creates consistent point-in-time snapshots of a database without locking out concurrent read or write transactions.
1. Custom Compressed Format (-F c) (Recommended for Production)
The custom archive format compresses data, supports selective object restoration, and enables parallel multi-core extraction:
pg_dump -h localhost -p 5432 -U postgres -d production_db \
-F c -b -v -f "backup_production_$(date +%Y%m%d_%H%M%S).dump"Key Flags:
-F c: Custom binary archive format.-b: Include large objects (BLOBs).-v: Verbose logging output.-f <file>: Destination output filepath.
2. Plain SQL Text Backup
Generates a standard human-readable SQL file:
pg_dump -U postgres -d production_db > backup.sql
# Compressed with gzip on-the-fly
pg_dump -U postgres -d production_db | gzip > backup.sql.gz3. High-Speed Multi-Threaded Parallel Backup (-F d -j)
For multi-gigabyte or terabyte databases, dump data using multiple CPU cores into a directory:
pg_dump -U postgres -d production_db \
-F d -j 4 -v -f ./production_backup_dir4. Backing Up All Databases & Global Roles (pg_dumpall)
To export all databases, user accounts, roles, and global configuration settings:
pg_dumpall -U postgres -h localhost > all_databases_and_roles.sql
# Dump only roles and global permissions
pg_dumpall -U postgres --globals-only > global_roles.sqlPostgreSQL Roles, Permissions & Row-Level Security (RLS)
Manage database permissions with GRANT/REVOKE, schema ownership, and secure multi-tenant data using Row-Level Security (RLS).
Restoring PostgreSQL Databases with pg_restore
Restore PostgreSQL databases from custom dumps, directory archives, and SQL scripts using pg_restore and psql.