Lesson 4 of 9 · 27m
Backups and Point-in-Time Recovery
What a physical base backup really contains, why WAL archiving is the other half of the backup, and how pg_basebackup plus recovery targets give you point-in-time recovery.
The worst backup is the one that exists, passes monitoring, and cannot be restored. I have inherited backup jobs that proudly wrote archives every night while WAL archiving had been broken for months. They were excellent copies of a database nobody could consistently recover.
A backup strategy is a chain: a base backup, the WAL generated after it, configuration files, encryption keys, and proof that the chain can be replayed.
Logical dumps and physical backups solve different problems
pg_dump --format=custom --file=/backups/appdb.dump appdbpg_dump is logical: good for copying one database, selected objects, or crossing major versions. It is not a full-cluster recovery plan because roles, tablespaces, replication state, and cluster configuration live outside it.
For whole-cluster recovery, use a physical backup:
pg_basebackup --host=db-primary.internal --username=replicator --pgdata=/backups/base/2026-11-29 --format=tar --gzip --progress --wal-method=stream--wal-method=stream streams enough WAL during the backup to make the copied data directory consistent.
WAL archiving turns a snapshot into a timeline
A base backup tells you where the database was around backup time. Archived WAL tells you what happened afterward. Together they let you stop at a chosen point in time.
archive_mode = on
archive_command = 'test ! -f /walarchive/%f && cp %p /walarchive/%f'In real environments, use a hardened tool such as pgBackRest, WAL-G, or Barman. PostgreSQL relies on accurate exit codes; a shell command that silently succeeds on failure is a data-loss machine. Monitor archive lag directly.
PITR means choosing when truth ends
restore_command = 'cp /walarchive/%f %p'
recovery_target_time = '2026-11-29 20:41:00 UTC'
recovery_target_action = 'pause'The target can also be a transaction ID, named restore point, LSN, or immediate. pause lets you inspect the database before promoting it.
The recovery flow is:
- Restore the base backup into a clean data directory.
- Configure
restore_commandand the recovery target. - Create
recovery.signal. - Start PostgreSQL and let it replay WAL.
- Inspect, then promote or continue replaying.
Protect more than data files
Copy postgresql.conf, pg_hba.conf, certificates, systemd overrides, role definitions, and tablespace mappings. Encrypt backups at rest and control keys separately; an unencrypted backup is a full copy of production with fewer access controls.
Field notes
- A successful backup job proves files were written; recovery tests prove recoverability.
pg_dumpall --globals-onlyis useful for roles and tablespaces even when the main strategy is physical.- WAL archives need retention lifecycle management; infinite retention is future disk bankruptcy.
- Restores often fail on ownership, permissions, or tablespace symlinks rather than data corruption.
- During corruption incidents, preserve WAL before touching anything. It may be the only path backward.
Was this lesson helpful?