Sub topic 11
Recovering to a Point in Time
Prerequisites and recovery goals
This chapter assumes basic SQL and psql usage, Linux command-line skills, and familiarity with PostgreSQL roles, databases, and the data directory. You also need a physical base backup and a working archive of write-ahead log (WAL) files.
Point-in-time recovery (PITR) restores a PostgreSQL cluster to a chosen moment. For example, you can recover the database to just before an accidental DROP TABLE. PITR needs both:
- A base backup, which is a complete physical copy of the cluster.
- Every required WAL segment created after that backup.
The recovery point objective (RPO) depends on how often WAL reaches the archive. The recovery time objective (RTO) depends on how quickly you can copy the base backup and replay the WAL.
| Component | Purpose |
|---|---|
| Base backup | Provides the starting physical state |
| Archived WAL | Replays changes after the base backup |
| Recovery target | Tells PostgreSQL when to stop replaying |
Configure continuous WAL archiving
Set these parameters in postgresql.conf on the source server:
wal_level = replica
archive_mode = on
archive_command = 'test ! -f /mnt/archive/%f && cp %p /mnt/archive/%f'wal_level = replica records enough information for physical recovery. archive_mode enables archiving, and archive_command copies each completed WAL segment to /mnt/archive. The %p placeholder is the source path, and %f is the WAL file name.
The archive directory must exist, be writable by the PostgreSQL server, and have enough space. archive_command must return a success status only after the file is safely stored. Protect the archive from accidental deletion and copy it to separate storage.
After changing archive_mode, restart PostgreSQL. Check archiving with:
SELECT archived_count, failed_count, last_archived_wal, last_failed_wal
FROM pg_stat_archiver;A growing failed_count means that recovery may later be missing WAL.
Take and prepare a base backup
Create a physical backup while WAL archiving is active:
pg_basebackup -h localhost -U replicator -D /backups/base -Ft -z -Xs -PThis command creates compressed tar files, streams the required WAL, and displays progress. Store the backup separately from the database server.
To recover, provision a clean PostgreSQL instance with the same major version. Restore the base backup into its data directory. Do not start the target server before the files are in place. The target must also be able to read the archived WAL files.
Set a recovery target
On PostgreSQL 12 and later, create an empty recovery.signal file in the target data directory. Then add recovery settings:
restore_command = 'cp /mnt/archive/%f %p'
recovery_target_time = '2026-11-27 10:15:00+00'
recovery_target_action = 'promote'During startup, PostgreSQL runs restore_command whenever it needs a WAL segment. %f expands to the requested file name, and %p expands to the destination path. PostgreSQL replays WAL until the target time, then promotes the server so it accepts writes.
Use an explicit time zone. The target must be later than the base backup and earlier than the last available WAL record. If the command cannot find a requested file, PostgreSQL waits and retries, so check the archive path and permissions carefully.
Check the result
Inspect the server logs for the recovery target and promotion messages. Confirm that the damaged object is present or absent as expected, depending on the chosen time. After recovery, take a new base backup before relying on the new server for future recovery.
Exercise
Why can a logical pg_dump file alone not provide point-in-time recovery?
Show answer
A logical dump represents one database state. PITR needs a physical base backup plus the sequence of WAL records that describe changes after that backup.