PostgreSQL backup — 3 levels: logical (pg_dump, portable), physical (pg_basebackup, fast restore), continuous WAL archiving (point-in-time recovery). For prod: daily pg_basebackup + WAL archiving to S3 + 30-day retention. Test restore monthly — untested backup = no backup.
Below: step-by-step, working examples, common pitfalls, FAQ.
Free online tool — HTTP header checker: instant results, no signup.
pg_dump -Fc -Z9 -f backup.dump dbname (compressed custom format)pg_basebackup -D /backup/base -Ft -z -P -U replicatorarchive_mode = on; archive_command = 'aws s3 cp %p s3://bucket/wal/%f'0 3 * * * /usr/bin/pg_dump -Fc -f /backup/daily-\$(date +\%Y\%m\%d).dump dbnamepg_restore -d new_db /backup/daily-20260401.dumppg_basebackup + apply WAL segments up to target timestamp| Scenario | Config |
|---|---|
| Simple daily pg_dump script | #!/bin/bash
DATE=$(date +%Y%m%d)
pg_dump -Fc -Z9 -f /backup/mydb-$DATE.dump mydb
aws s3 cp /backup/mydb-$DATE.dump s3://mybucket/postgres/
find /backup -name 'mydb-*.dump' -mtime +7 -delete |
| postgresql.conf (archiving) | wal_level = replica
archive_mode = on
archive_command = 'test ! -f /archive/%f && cp %p /archive/%f'
archive_timeout = 300 |
| Restore from pg_dump | createdb newdb
pg_restore -d newdb -j 4 /backup/mydb-20260401.dump |
| pg_basebackup | pg_basebackup -h localhost -U replicator -D /backup/base_$(date +%Y%m%d) -Ft -z -P |
| PITR restore | # restore_command in recovery.conf
restore_command = 'cp /archive/%f %p'
recovery_target_time = '2026-04-17 12:00:00' |
To set up PostgreSQL backups in 2026, use the pg_dump command to create a backup of your database. For a full backup, execute pg_dump -U username -F c -b -v -f /path/to/backup/file.backup dbname. Schedule backups using a cron job, ensuring they run at off-peak hours. Also, consider using tools like pgBackRest or Barman for more advanced backup strategies.
PostgreSQL offers several options for backing up your databases, each suited to different needs and environments. Understanding these options will help you choose the best method for your use case.
This command-line utility is the most common way to back up a PostgreSQL database. It allows for full, partial, or incremental backups:
pg_dump -U username -F c -b -v -f /path/to/backup/file.backup dbname for a complete backup in a custom format.pg_dump -U username -s -f /path/to/backup/schema.sql dbname.pg_dump -U username -a -f /path/to/backup/data.sql dbname.If you want to back up all databases in a PostgreSQL instance, the pg_dumpall command is the way to go. Use it as follows:
pg_dumpall -U username > /path/to/backup/all_databases.sqlThis command will dump all databases, roles, and tablespaces, making it a comprehensive solution for full server backups.
For enterprise-level applications, you might need point-in-time recovery. This method involves continuous archiving of the WAL (Write Ahead Log) files:
postgresql.conf file:archive_mode = on
archive_command = 'cp %p /path/to/archive/%f'archive_timeout to ensure logs are archived regularly:archive_timeout = 60sWith this setup, you can restore your database to any point in time by combining the base backup with the archived WAL files.
For more complex backup strategies, consider using third-party tools like:
pg_dump — logical, per-database, portable (restore on another PG version). pg_basebackup — physical, whole cluster, fast, but restore only on same PG version.
PG has no built-in incremental. Use pg_basebackup + WAL archiving (effectively incremental via WAL). Or pgBackRest.
For compliance 30-90 days is typical. For DR 7-14 days is enough. S3 lifecycle rules auto-cleanup.
<code>pg_restore --list</code> shows content. Real test: periodic full restore to staging + smoke tests.
Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.