Sub topic 3
Running a Point-in-Time Recovery End to End
This chapter assumes you can start and stop PostgreSQL, edit postgresql.conf, and already have a base backup and archived WAL segments in place, as described in earlier chapters. A recovery stops at the target you choose, so you should know your target before you begin.
Running the recovery
Stop the server, move the damaged data directory aside, restore the base backup, and create recovery.signal so PostgreSQL starts in archive recovery mode.
sudo systemctl stop postgresql
sudo mv /var/lib/postgresql/16/main /var/lib/postgresql/16/main.broken
sudo cp -a /backups/base/20261127 /var/lib/postgresql/16/main
sudo chmod 700 /var/lib/postgresql/16/main
sudo -u postgres touch /var/lib/postgresql/16/main/recovery.signalThen add the recovery settings to postgresql.conf and start the server.
restore_command = 'cp /mnt/archive/%f "%p"'
recovery_target_time = '2026-11-27 14:30:00+00'
recovery_target_action = 'promote'What happens when the target is reached
recovery_target_action controls what the server does at the target. pause stops replay so you can inspect the data, and promote opens the database for writes. shutdown stops the server. With pause, run SELECT pg_wal_replay_resume(); to continue, or pg_wal_replay_resume() followed by a promotion to finish. Check the state with SELECT pg_is_in_recovery();, which returns false once the server accepts writes.
Timelines after recovery
Each promotion starts a new timeline, which is a numbered branch of WAL history. Archived files for the new timeline get their own prefix, so 00000002 appears where 00000001 was before. This keeps the abandoned future of the old timeline separate. Recovery can later target a specific timeline with recovery_target_timeline.
Retention: keeping the WAL your backups need
Keep every WAL segment from the start of your oldest retained base backup onward. Removing earlier segments breaks any restore that starts from that backup. Use pg_archivecleanup with the backup label file to remove only what is safe:
pg_archivecleanup /mnt/archive 000000010000000000000005.00000028.backupProving the backups work with test restores
Restore to a scratch directory on a separate port at least once a month. Confirm that the server starts, that the target data appears, and that the row counts look right. An untested backup is only a guess.
When to use pgBackRest, Barman or WAL-G
Manual scripts work for a single small server. Use pgBackRest, Barman, or WAL-G when you need automatic retention, parallel backups, verified archives, or restores across many servers.
flowchart TD
A[Restore base backup] --> B[Replay archived WAL]
B --> C{Target reached?}
C -->|promote| D[New timeline begins]
C -->|pause| E[Inspect, then resume or promote]Exercise
After a recovery with recoverytargetaction = 'promote', which new object shows that a timeline switch happened?
Show answer
A new timeline ID appears in the WAL file names, such as 00000002 in place of 00000001, and a .history file is written to the archive.