Point-in-time recovery (PITR) lets you restore a self-managed PostgreSQL cluster to any exact moment — the instant before a bad DELETE, not just last night’s snapshot. It rests on two pieces: a physical base backup taken with pg_basebackup, and a continuous stream of write-ahead log (WAL) segments shipped off the server by archive_command. To recover, you lay down the base backup, point restore_command at the WAL archive, set recovery_target_time to the moment you want, drop a recovery.signal file, and start the server. It replays WAL forward from the backup until it reaches your target, then stops. On PostgreSQL 15, 16, and 17 the old recovery.conf is gone — recovery settings live in postgresql.auto.conf and the presence of recovery.signal is what puts the server into recovery mode. The configuration is a few lines; the part that actually saves you is rehearsing the restore before you need it.
What you’ll accomplish
- Turn on continuous WAL archiving with
archive_modeand a safe, non-overwritingarchive_command. - Confirm segments are really landing in the archive before you trust it.
- Take a self-contained base backup with
pg_basebackup -Fp -Xs -P. - Restore that base backup and replay WAL to a chosen
recovery_target_time, then promote. - Validate that the target moment is present and everything after it is gone.
Prerequisites
- PostgreSQL 15, 16, or 17, self-managed — you need shell access to the data directory and the config. On managed Postgres you cannot do this by hand.
wal_levelset toreplicaor higher.replicais the default on 15/16/17 and is enough for PITR;minimalis not.- A writable archive destination the
postgresOS user can reach: a local directory, an NFS mount, or an object store fronted by a small wrapper script (aws s3 cp,rclone, and so on). - Disk headroom in two places — the archive grows continuously between base backups, and
pg_walwill back up locally if archiving stalls. - Awareness that
recovery.confno longer exists. It was removed in PostgreSQL 12. Recovery parameters are ordinary GUCs now, and arecovery.signalfile replaces the oldstandby_mode/trigger dance.
How it works
A base backup is a physical, block-level copy of the whole cluster as of one checkpoint — the consistent floor you start replay from. On its own it recovers you to roughly the time the backup finished.
WAL is PostgreSQL’s change log. Every insert, update, delete, and DDL is written to WAL before the data files change. Once a 16 MB WAL segment fills, archive_command copies it to your archive destination. Recovery is just the reverse: PostgreSQL takes the base backup and replays those archived segments forward, transaction by transaction, until it reaches recovery_target_time.
Two rules make or break this. First, archive_command must return non-zero when the copy fails — PostgreSQL only marks a segment as safely archived (and eligible for recycling) when the command exits 0. A command that swallows errors and returns 0 will tell the server to throw away WAL you never actually saved. Second, archive_command must never overwrite an existing file — if it clobbers a segment, your archive is silently corrupt. The standard pattern tests for the file first, then copies.
Step-by-step
1. Set wal_level and turn archiving on. Edit postgresql.conf. %p is the full path of the segment PostgreSQL wants archived; %f is just its filename. The test ! -f guard refuses to overwrite, and && means the whole command returns non-zero if either half fails.
wal_level = replica
archive_mode = on
archive_command = 'test ! -f /wal_archive/%f && cp %p /wal_archive/%f'
2. Create the archive directory and restart. archive_mode only takes effect on a full restart, not a reload.
sudo -u postgres mkdir -p /wal_archive
sudo systemctl restart postgresql
These print nothing on success; the prompt returns once the server is back up.
3. Force a segment switch and confirm it lands. Do not wait for organic traffic to fill a segment — force one and check the archive directly.
SELECT pg_switch_wal();
Expected output (an LSN, exact value varies):
pg_switch_wal
---------------
0/3000180
(1 row)
Then look in the archive:
ls -l /wal_archive
Expected output — at least one 16 MB segment file with a 24-character name:
-rw------- 1 postgres postgres 16777216 Jul 21 14:02 000000010000000000000003
If the directory is empty, stop and fix archiving now — everything downstream depends on it. Check SELECT * FROM pg_stat_archiver; for a rising failed_count.
4. Take the base backup. -Fp writes a plain (unpacked) directory, -Xs streams the WAL needed to reach consistency, and -P shows progress. Give it its own dated directory.
pg_basebackup -h 127.0.0.1 -U repl -D /backup/base-20260721 -Fp -Xs -P
Expected output ends on a completed progress line:
Password:
425331/425331 kB (100%), 1/1 tablespace
Keep this backup and every WAL segment archived after it. Together they define how far forward you can recover.
5. Simulate a disaster and note the target. For the drill, insert a known row, record the time, then do damage after it. In production this step is the accident you did not plan — but rehearse it so the mechanics are muscle memory.
INSERT INTO audit(note) VALUES ('before-target');
SELECT now();
-- 2026-07-21 14:19:55.812-04
-- ... then the bad change happens a minute later ...
DELETE FROM audit;
Your recovery target is 2026-07-21 14:19:55 — after the good insert, before the delete.
6. Stop the server and move the data directory aside. Never restore over a live cluster; keep the old directory until you have verified the restore.
sudo systemctl stop postgresql
sudo mv /var/lib/postgresql/16/main /var/lib/postgresql/16/main.broken
Both return to the prompt on success.
7. Restore the base backup into a fresh data directory. Copy the plain base backup into place, then fix ownership and permissions — PostgreSQL refuses to start on a data directory that is not mode 0700 and owned by postgres.
sudo cp -a /backup/base-20260721 /var/lib/postgresql/16/main
sudo chown -R postgres:postgres /var/lib/postgresql/16/main
sudo chmod 700 /var/lib/postgresql/16/main
8. Set the recovery parameters and drop recovery.signal. Append the recovery settings to postgresql.auto.conf (this is where the removed recovery.conf settings live now), then create the signal file that puts the server into archive recovery.
sudo -u postgres tee -a /var/lib/postgresql/16/main/postgresql.auto.conf <<'EOF'
restore_command = 'cp /wal_archive/%f %p'
recovery_target_time = '2026-07-21 14:19:55'
recovery_target_action = 'pause'
EOF
sudo -u postgres touch /var/lib/postgresql/16/main/recovery.signal
recovery_target_action = 'pause' stops replay at the target and holds so you can look before committing. Without the recovery.signal file, the server ignores every one of these settings and starts normally.
9. Start the server and watch it reach the target. Bring it up and tail the log.
sudo systemctl start postgresql
sudo tail -f /var/log/postgresql/postgresql-16-main.log
Expected output shows replay stopping at the target and pausing:
LOG: starting point-in-time recovery to 2026-07-21 14:19:55-04
LOG: restored log file "000000010000000000000004" from archive
LOG: recovery stopping before commit of transaction 1234, time 2026-07-21 14:19:56.02-04
LOG: recovery has paused
HINT: Execute pg_wal_replay_resume() to promote.
10. Inspect, then promote. Connect and check the data (see the next section). When satisfied, end recovery and open the cluster read-write.
SELECT pg_wal_replay_resume();
The server finishes on a new timeline and begins accepting writes. pg_promote() is the equivalent when you want to force the end immediately.
Validate it works
Confirm the server is out of recovery and open for writes:
SELECT pg_is_in_recovery();
Expected output after promotion:
pg_is_in_recovery
-------------------
f
(1 row)
Now confirm the target moment is captured correctly — the pre-target row is present and the post-target damage is absent:
SELECT count(*) FROM audit WHERE note = 'before-target';
Expected output — the row survived:
count
-------
1
(1 row)
The DELETE happened after recovery_target_time, so it was never replayed: the table still has its rows. That is the whole point — you stopped the clock one moment before the mistake. Cross-check against the log line recovery stopping before commit of transaction ..., which names the exact transaction replay halted on.
Troubleshooting
Archiving silently fails but pg_stat_archiver looks fine. An archive_command that ends in something returning 0 on failure (a piped command, a script that ignores its exit code) makes PostgreSQL believe segments are saved when they are not. Test the command by hand as the postgres user and echo $?. The test ! -f ... && cp ... form returns non-zero if the copy fails or the target already exists.
pg_wal fills the disk. When archiving stalls, PostgreSQL will not recycle segments it has not archived, so pg_wal grows without bound and can take the server down. Watch SELECT last_failed_wal, failed_count FROM pg_stat_archiver;. Fix the destination (permissions, mount, disk on the far end), and the backlog drains on its own.
Recovery overshoots or lands in the wrong place. recovery_target_time is interpreted in the server’s time zone. A timestamp copied from a log in UTC while the server runs in America/Toronto lands hours off. Pin it explicitly — write the value with an offset, or set log_timezone and timezone deliberately so you know which zone you are naming.
The server starts normally and ignores the target. No recovery.signal file, or it was not readable by postgres. Without that file the recovery GUCs in postgresql.auto.conf are inert. Recreate it as the postgres user and restart.
Permission denied writing to the archive. archive_command runs as the postgres OS user. If /wal_archive (or the NFS/object mount) is not writable by postgres, every archive attempt fails and pg_wal backs up. chown postgres the directory and re-test with pg_switch_wal().
Confused about timelines after promotion. Promotion forks a new timeline (the second segment of the WAL filename increments). If you later need to recover past that fork, you must keep the pre-promotion segments too, and may need recovery_target_timeline. For a one-shot restore this does not bite you, but it is why you never delete old archive files casually.
When this is the wrong approach
- You are on managed Postgres. RDS, Cloud SQL, and most PaaS offerings run PITR for you and hide the data directory — you cannot set
archive_commandby hand. Use their console’s restore-to-timestamp feature; it wraps this same mechanism. - The database is small and low-churn. For a few gigabytes that change slowly, a nightly
pg_dumpis simpler to run and restore, and the recovery-point gap may be acceptable. PITR earns its complexity when losing hours of writes is not an option. See our note on database point-in-time recovery across engines for where that line sits. - You actually need high availability, not recovery. PITR gets you back after a mistake, but it is not fast and it is not automatic. If the requirement is surviving a node failure with seconds of downtime, you want streaming replication and failover, not a restore drill. The base-backup mechanics here also underpin physical backups with pg_basebackup.
For the authoritative reference on every parameter, see the PostgreSQL manual on Continuous Archiving and Point-in-Time Recovery and the recovery target settings.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.