Overview
A backup you have not restored is a hope, not a backup. PostgreSQL offers logical backups (SQL or custom-format dumps of objects) and physical backups (a copy of the data directory plus archived WAL). Physical backup with WAL archiving enables point-in-time recovery (PITR): restore a base backup, then replay WAL to a chosen moment.
Core concepts
Logical (pg_dump) | Physical (pg_basebackup, pgBackRest, Barman) | |
|---|---|---|
| Unit | One database or objects | Whole cluster |
| Version portability | Restores to newer major versions | Same major version and platform |
| Speed on large data | Slow (rebuilds indexes) | Fast |
| PITR | No | Yes, with WAL archive |
| Use for | Migrations, small databases, single-table recovery | Production disaster recovery |
- WAL archiving:
archive_mode = onandarchive_command(orarchive_library) copy each completed WAL segment to durable storage. A failing archive command makes WAL pile up on the primary. - Recovery: restore the base backup, create
recovery.signal, setrestore_commandand a target (recovery_target_time,recovery_target_lsn,recovery_target_name), start the server, thenpg_wal_replay_resume()/promote. - Targets: RPO (how much data you can lose) is set by WAL archive frequency or synchronous replication; RTO (how long recovery takes) by backup size, restore speed and WAL to replay.
- PostgreSQL 17 adds incremental base backups:
pg_basebackup --incrementalwithpg_combinebackupto reconstruct a full backup. - Tools such as pgBackRest and Barman add parallel backup and restore, compression, encryption, retention policies, verification and object-store targets.
Worked example
Logical backup and restore (executed commands are standard; sizes depend on your data):
pg_dump -Fc -h db -U app mydb > mydb.dump # custom format, compressed, selectable restore
pg_restore -h newdb -U app -d mydb --jobs 4 mydb.dump # parallel restore
pg_dumpall --globals-only > globals.sql # roles and tablespacesPITR outline (manual; not executed here):
# primary postgresql.conf
archive_mode = on
archive_command = 'test ! -f /archive/%f && cp %p /archive/%f'
# on the restored copy, postgresql.conf
restore_command = 'cp /archive/%f %p'
recovery_target_time = '2025-03-01 09:14:00+00'
recovery_target_action = 'promote'then touch recovery.signal in the data directory and start the server. Recovery stops at the target moment, just before the bad DROP TABLE.
Pitfalls and production notes
- Test restores on a schedule and time them; record the measured RTO.
- Keep backups and WAL archive in a different failure domain than the server; encrypt them and restrict access.
- Monitor
pg_stat_archiver(failed_count,last_failed_time) so archiving failures alert before the disk fills. pg_dumptakes a consistent snapshot but a long dump holds back vacuum; run it against a replica when possible.- After PITR to a past point a new timeline starts; take a fresh base backup.
- Backups of encrypted columns or external files (large objects stored outside) need their own handling.
Practice questions
- A developer dropped a table at 10:32. Describe the steps to recover it without losing other data.
- Why can you not use
pg_basebackupoutput to upgrade to the next major version? - How do you decide the archive frequency from an RPO of five minutes?
References
- PostgreSQL manual, Backup and Restore: https://www.postgresql.org/docs/current/backup.html
- pgBackRest documentation: https://pgbackrest.org
Related
pg-wal-checkpoints · pg-replication-failover · pg-security-roles