Sub topic 11
Recovering to a Point in Time
This chapter assumes you can take a base backup with pg_basebackup, and that WAL archiving is already on, as covered in the previous two chapters. You should also know where the data directory is and how to start and stop the server with your service manager.
What point-in-time recovery does
A base backup is a copy of the cluster at one moment. WAL (write-ahead log) is the record of every change made after that moment. Point-in-time recovery (PITR) restores the base backup, then replays archived WAL up to a target you choose. The target can be a timestamp, a named restore point, a transaction ID, or a WAL position (LSN).
PITR answers the failure that dumps and base backups alone cannot: someone ran DELETE without a WHERE clause at 10:16, and your last backup is from midnight. You restore to 10:15 and lose only what was written after that.
What you need before the disaster
PITR only works if these three things exist before you need them:
- A base backup taken before the target time.
- An unbroken chain of archived WAL files from the start of that base backup to the target time.
archive_mode = onand a workingarchive_commandthat was running during that whole period.
The archive configuration looks like this:
wal_level = replica
archive_mode = on
archive_command = 'test ! -f /mnt/archive/%f && cp %p /mnt/archive/%f'%p is the path of the WAL file to archive, and %f is its file name. The test ! -f guard refuses to overwrite an existing file. If archive_command returns a non-zero exit status, PostgreSQL retries and keeps the WAL segment in pg_wal, so a broken archive eventually fills the disk.
A base backup is only usable if the WAL written during the backup is also available. With -Xs (stream), pg_basebackup includes that WAL in the backup itself. WAL from after the backup finished must come from the archive.
Recovery settings
Recovery is controlled by settings in postgresql.conf (or postgresql.auto.conf) plus a signal file. Since PostgreSQL 12, the old recovery.conf file no longer exists, and the server refuses to start if it is present.
restore_command = 'cp /mnt/archive/%f %p'
recovery_target_time = '2026-11-27 10:15:00+00'
recovery_target_action = 'promote'| Setting | Purpose |
|---|---|
| `restore_command` | Shell command that copies the archived file `%f` to the path `%p`. It is the mirror of `archive_command`. |
| `recovery_target_time` | Stop replay at this timestamp. Include a time zone offset to avoid ambiguity. |
| `recovery_target_name` | Stop at a named restore point created with `pg_create_restore_point()`. |
| `recovery_target_xid` | Stop at a transaction ID. |
| `recovery_target_lsn` | Stop at a WAL location. |
| `recovery_target_inclusive` | Whether to stop just after (`on`, the default) or just before (`off`) the target. |
| `recovery_target_timeline` | Which timeline to follow. The default is `latest` since PostgreSQL 12. |
| `recovery_target_action` | What to do at the target: `pause` (default), `promote`, or `shutdown`. |
Set only one recovery_target_* option for the target itself. If you set none, recovery replays all available WAL to the end of the archive.
Step-by-step recovery
The steps below restore to 10:15 UTC on 2026-11-27. They assume the cluster is on a Debian-style path, /var/lib/postgresql/16/main. Adjust the path and service name for your system.
- Stop the server if it is running, and move the damaged data directory aside instead of deleting it. You may need it later.
``bash sudo systemctl stop postgresql sudo mv /var/lib/postgresql/16/main /var/lib/postgresql/16/main.broken ``
- Create a new empty data directory and unpack the base backup into it. For a tar-format backup made with
-Ft -z, the data is inbase.tar.gz, and the streamed WAL is inpg_wal.tar.gz.
``bash sudo install -d -o postgres -g postgres -m 700 /var/lib/postgresql/16/main sudo -u postgres tar -xzf /backups/base/base.tar.gz -C /var/lib/postgresql/16/main sudo -u postgres tar -xzf /backups/base/pg_wal.tar.gz -C /var/lib/postgresql/16/main/pg_wal ``
- Add the recovery settings to
postgresql.confas shown above.
- Create the signal file. An empty
recovery.signaltells the server to start in targeted archive recovery.
``bash sudo -u postgres touch /var/lib/postgresql/16/main/recovery.signal ``
- Start the server and watch the log.
``bash sudo systemctl start postgresql sudo tail -f /var/log/postgresql/postgresql-16-main.log ``
You should see lines such as starting point-in-time recovery to 2026-11-27 10:15:00+00, then restored log file ... from archive for each segment, and finally recovery stopping before commit of transaction ... or recovery stopping at ..., followed by archive recovery complete.
When recovery ends, the server removes recovery.signal. With recovery_target_action = 'promote', it also opens for writes right away.
Checking the result before you promote
promote is convenient, but it makes the choice final. Once the server is writable and accepts new transactions, you cannot replay further WAL from the same position. If you are unsure about the target time, use pause first.
With recovery_target_action = 'pause', the server stays in recovery and accepts read-only queries when hot_standby = on (the default). You can inspect the data:
SELECT count(*) FROM orders;
SELECT pg_is_in_recovery();
SELECT pg_last_xact_replay_timestamp();If the data is wrong, such as the deleted rows still missing, stop the server, edit recovery_target_time to an earlier value, and start again. Recovery restarts from the base backup's checkpoint, as long as you have not promoted. If the data looks right, promote:
SELECT pg_wal_replay_resume();That call ends the pause. With pause as the action, the server stays read-only at the target until you call pg_promote(), or run pg_ctl promote.
SELECT pg_promote();Named restore points
Timestamps are imprecise. If you know a risky operation is coming, such as a schema migration, mark the moment first:
SELECT pg_create_restore_point('before_migration_42');Then recover with recovery_target_name = 'before_migration_42'. The name is exact, so it removes any doubt about clock skew or time zones.
Timelines
When recovery finishes and the server is promoted, PostgreSQL starts a new timeline. A timeline is a numbered branch of WAL history. The first one is 1. After your first PITR, new writes belong to timeline 2. WAL file names start with the timeline ID in hex, for example 000000020000000000000007, and a .history file in the archive records where each branch split.
Timelines exist so that WAL from the old history cannot be mixed with the new one. Without them, the old timeline's WAL for 10:16 onward, which includes the mistake, could be replayed over your recovered cluster.
Two practical consequences follow:
- Keep archiving after the recovery. The recovered server writes timeline 2 WAL into the same archive, and the
test ! -fguard inarchive_commandwill not collide, because the file names differ. - A new base backup should follow soon after a PITR. Older base backups still work, but a fresh one avoids long replays through a timeline switch.
If you want to recover to a point on a branch other than the latest, set recovery_target_timeline to a specific ID.
Common errors
| Symptom | Likely cause |
|---|---|
| `recovery ended before configured recovery target was reached` | The archive ends before the target. WAL is missing, or the target is later than the last archived segment. |
| `could not open file ... No such file or directory` for a WAL segment, repeated | A gap in the archive. Every file between the base backup and the target must exist. |
| Server starts normally and recovers nothing | `recovery.signal` is missing, or `restore_command` is not set. |
| `FATAL: using recovery command file "recovery.conf" is not supported` | You used the pre-12 method. Move the settings to `postgresql.conf` and use `recovery.signal`. |
| Recovery stops at the wrong place | Time zone confusion. Write an explicit offset in `recovery_target_time`, or use a restore point. |
| `restore_command` fails with permission errors | The `postgres` user cannot read the archive path. |
| Target time is earlier than the base backup's end | The base backup is too new. Use an older one. |
The last row matters. The target must be later than the moment the base backup finished. When you keep several base backups, choose the newest one that ended before your target time.
Using the recovered data
Often you do not want to replace the production cluster. You only want a few rows back. In that case, restore the PITR copy on another host, promote it, and extract the rows with pg_dump or COPY, then load them into production:
pg_dump -h recovery-host -U postgres -t public.orders -Fc -f orders_before.dump mydbThis keeps production online and limits changes to the data you actually need.
Exercise
Your last base backup finished at 02:00. Archived WAL exists continuously from 01:50 to 14:00. A bad UPDATE ran at 09:30. You want the cluster as it was just before it. List the settings and files you need, and explain why you might choose recovery_target_action = 'pause' over 'promote' on the first attempt.
Show answer
Restore the 02:00 base backup into an empty data directory. Set restore_command (for example cp /mnt/archive/%f %p), set recovery_target_time to a time just before 09:30 with an explicit time zone offset, and create an empty recovery.signal file in the data directory. Then start the server. With pause, the server stops at the target in read-only mode, so you can query the data and confirm the bad update is not yet applied. If the target is wrong, you edit the time and restart recovery. After promote, the server opens for writes on a new timeline and you cannot move the target back without starting again from the base backup.
Exercise
After a PITR the log says recovery ended before configured recovery target was reached. Name two possible causes and one way to check each.
Show answer
First, the target time is later than the last archived WAL, so check the newest file in the archive and its timestamp. Second, a segment is missing from the archive, so check the log for the file name restore_command could not fetch and confirm whether it exists in the archive directory. Monitoring pg_stat_archiver on the original server for failed archive attempts can also reveal gaps.