Sub topic 10
Running a point-in-time recovery end to end
This chapter assumes you have already enabled WAL archiving, taken a base backup with pg_basebackup, and met restore_command and the recovery targets. It puts them together into one recovery, from the moment you decide to restore to the moment the repaired database accepts writes. The examples use PostgreSQL 16 on a Debian-style layout, with the data directory at /var/lib/postgresql/16/main, the archive at /mnt/archive and the base backup at /backups/base/20261127.
The shape of a recovery
A point-in-time recovery (PITR) has three inputs: a base backup, a stream of archived WAL segments, and a target that says where to stop. PostgreSQL copies the base backup into place, starts in archive recovery mode, fetches WAL through restore_command, replays it, and stops at the target. After that it switches to a new timeline and, depending on recovery_target_action, opens for connections.
Step 1: Choose the target before touching anything
Work out what you want to undo. Typical cases:
- A bad
DELETEorUPDATEran at a known time. Userecovery_target_timeslightly before it. - A schema migration went wrong. If you created a restore point with
pg_create_restore_point('before_schema_migration'), userecovery_target_name. - You know the exact transaction. Use
recovery_target_xid.
Pick one target type. Setting more than one of recovery_target_time, recovery_target_xid, recovery_target_lsn, recovery_target_name and recovery_target makes the server refuse to start.
A target must lie after the end of the base backup. A base backup is only consistent once replay passes the point where the backup finished. If your target is earlier, recovery stops with an error saying it ended before the configured target was reached. Choose an older base backup in that case.
Step 2: Stop the server and keep the evidence
Never restore over the damaged directory. Rename it so you can still read from it if the recovery fails or you need files from it.
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.signal
# add the recovery settings below to postgresql.conf (or postgresql.auto.conf), then:
sudo systemctl start postgresqlThree details in this block matter:
cp -apreserves ownership and permissions. The copy must be owned by thepostgresuser.chmod 700is required. PostgreSQL refuses to start on a data directory that is accessible to group or others.recovery.signalis an empty file. Its presence tells the server to perform targeted archive recovery. Without it the server runs ordinary crash recovery from the backup's own WAL and starts at the end of that WAL, ignoring your target.
Do not start the server yet if you have not written the settings. The block above shows the start command last, so do it after the next step.
If the backup was taken in tar format, extract base.tar and pg_wal.tar into the new data directory instead. If your data uses tablespaces, restore them to the paths the backup records, or remap them as pg_basebackup documents.
Step 3: Configure recovery
Add these settings to postgresql.conf or postgresql.auto.conf in the restored directory:
# recovery settings (PostgreSQL 12 and later)
restore_command = 'cp /mnt/archive/%f "%p"'
recovery_target_time = '2026-11-27 14:30:00+00'
recovery_target_action = 'promote'%f is the WAL file name the server wants and %p is the path where it must place the copy. The command has to return exit status 0 only when the file was fully copied. A non-zero status means "that file is not available", which is how recovery learns it has reached the end of the archive.
The settings above promote automatically. For a first real recovery, it is safer to pause at the target and look at the data before committing. Use this instead:
restore_command = 'cp /mnt/archive/%f "%p"'
recovery_target_time = '2026-11-27 14:30:00+00'
recovery_target_inclusive = off
recovery_target_action = 'pause'recovery_target_inclusive = off stops just before the first transaction that committed at or after the target time. The default, on, stops just after the last transaction at or before it. With a named restore point or an LSN the choice matters less, but with a time that falls in the middle of a busy second, it decides whether that second's commits are included.
Two further points about the restored configuration:
- If the restored
postgresql.confstill containsarchive_mode = onand the originalarchive_command, the recovered server will start archiving into the same directory. Thetest ! -fguard in the archive command prevents overwriting, but the new timeline's segments will then collide or mix with the old ones. Archiving after recovery is normal and wanted, since you need the new timeline's WAL, but check the destination first. Many teams point the recovered server at a fresh archive location or confirm that the old archive is no longer needed. - Remove any
primary_conninfoorstandby.signalleft over from a replica backup, unless you really want a standby.
Step 4: Start and watch the log
sudo systemctl start postgresql
sudo tail -f /var/log/postgresql/postgresql-16-main.logA healthy recovery log shows messages like these, in roughly this order:
- The server starts point-in-time recovery to your target.
- It reads the
backup_labelfile and finds the checkpoint where the backup started. - It restores archived log files one by one through your
restore_command. - It reaches a consistent recovery state. Before this point, connections are refused.
- It reports where recovery stopped, and the last completed transaction time.
- It pauses, or it promotes to a new timeline.
During replay, restore_command failing for a file that does not exist is not an error by itself. At the end of the archive you will see one failed fetch followed by the end-of-recovery messages. A failure for a file you know exists points to wrong permissions or a wrong path in the command.
If the server exits immediately, read the first FATAL line. Common causes:
| Symptom in the log | Likely cause |
|---|---|
| Data directory has invalid permissions | Missing `chmod 700` or wrong owner |
| Recovery ended before configured target was reached | Target is older than the base backup, or WAL is missing from the archive |
| Multiple recovery targets specified | Two `recovery_target_*` settings are active |
| `restore_command` required but not set | `recovery.signal` exists but the command is empty |
| Could not locate required checkpoint record | `backup_label` is missing or the backup was damaged during the copy |
Step 5: Verify while paused
With recovery_target_action = 'pause', the server accepts read-only connections once it reaches the target. Check where you are, then look at the data:
SELECT pg_is_in_recovery();
SELECT pg_get_wal_replay_pause_state();
SELECT pg_last_xact_replay_timestamp();
SELECT count(*) FROM orders WHERE deleted_at IS NULL;pg_is_in_recovery() returns true while paused. pg_last_xact_replay_timestamp() shows the commit time of the last replayed transaction, which should be just under your target.
If the data is wrong, you cannot move the target forward in a paused server by editing the setting. Stop the server and restore again with a different target, which is why Step 2 told you to keep the base backup untouched. If the data is right, promote:
SELECT pg_promote();You can also run SELECT pg_wal_replay_resume();, which ends recovery according to the configured action once replay resumes.
Step 6: Confirm the new timeline and take a new backup
After promotion the cluster is on a new timeline. Check it:
SELECT timeline_id FROM pg_control_checkpoint();The number is higher than before, and a .history file such as 00000002.history appears in the archive. That file records where the new timeline branched, so any future recovery can follow the right path. The old timeline's WAL after the target stays in the archive but is no longer on your path. This is what lets you retry from the same base backup with a different target.
Then do two things immediately:
- Take a fresh base backup of the recovered cluster. A backup from before the recovery can still restore the new timeline, but only with
recovery_target_timeline = 'latest'and a complete archive, and a new backup removes that dependency. - Remove the recovery settings (
recovery_target_*, andrestore_commandif you do not need it) so a later restart is not confused.recovery.signalis deleted automatically on promotion.
Common mistakes recap
- Restoring over the damaged directory, leaving no way to compare or retry.
- Forgetting
recovery.signal, so the target is ignored. - Choosing a target earlier than the end of the base backup.
- Restoring a base backup without the archive segments that cover the span from backup end to target.
- Promoting before checking the data, which makes the first attempt final.
- Letting the recovered server write into the old archive without checking for name collisions.
Exercise
You restored a base backup taken at 02:00 and set recovery_target_time = '2026-11-27 14:30:00+00', but forgot to create recovery.signal. The server starts normally. What state is the database in, and how do you fix it?
Show answer
Without recovery.signal the server performs only crash recovery using the WAL contained in the backup. It does not fetch archived WAL and ignores the recovery target, so the database is at roughly the state of the end of the backup (around 02:00), not 14:30, and it is already open for writes on the original timeline. Stop the server, discard this data directory, restore the base backup again, create recovery.signal, make sure restore_command and the target are set, and start again. Do not keep using the partially started copy, since writes made to it would diverge from the archive.
Exercise
Recovery paused at the target, and you find that a transaction you wanted to keep was lost because the target time was a few seconds too early. Can you set a later target and call pg_wal_replay_resume() to continue?
Show answer
No. The target is read at startup and a paused server will not move it forward. Stop the server, restore the base backup again into a clean data directory, create recovery.signal, set a later recovery_target_time (or recovery_target_inclusive = on), and start again. This works because you did not promote, so the archive still holds the WAL for the later time on the same timeline.