Sub topic 10
Running a Point-in-Time Recovery End to End
This chapter assumes you can start and stop PostgreSQL, edit postgresql.conf, use the Linux shell, and identify the PostgreSQL data directory. It also assumes that WAL archiving was enabled before the incident and that you have both a physical base backup and every archived WAL segment needed after that backup.
Point-in-time recovery (PITR) rebuilds a cluster from a base backup, then replays archived write-ahead log (WAL) records. Recovery stops at a target such as a timestamp, transaction ID, log sequence number (LSN), or named restore point.
Prepare the recovery inputs
Before changing the damaged server, record:
- The PostgreSQL major version.
- The location and ownership of the data directory.
- The base backup directory.
- The archive directory or archive storage location.
- The target time, LSN, transaction, or restore-point name.
- The time zone used when recording the target.
A timestamp target is interpreted against WAL records, so it may not match the exact time shown by an application. Choose a target just before the unwanted change when possible.
Stop the server and preserve the damaged data directory. Moving it aside is safer than deleting it because it keeps evidence and provides a fallback copy.
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 chown -R postgres:postgres /var/lib/postgresql/16/main
sudo -u postgres touch /var/lib/postgresql/16/main/recovery.signalThe recovery.signal file tells PostgreSQL to enter archive recovery when it starts. Do not create it in the old, damaged directory. Create it in the restored base backup.
Configure the recovery target
Add recovery settings to the restored cluster's postgresql.conf or postgresql.auto.conf. The restore_command must copy one requested WAL segment from the archive to the destination path represented by %p. The %f placeholder is the requested WAL filename.
restore_command = 'cp /mnt/archive/%f "%p"'
recovery_target_time = '2026-11-27 14:30:00+00'
recovery_target_action = 'promote'PostgreSQL calls restore_command repeatedly. A successful command must exit with status zero and create the requested destination file. When the requested segment is not available, the command should return a nonzero status. PostgreSQL then tries other recovery sources or ends recovery when no more WAL is available.
recovery_target_action = 'promote' makes the recovered cluster available for normal read/write connections when the target is reached. Use pause when you want to inspect the recovered state before promotion. Use shutdown when the server must stop at the target and another controlled step will start it.
Start PostgreSQL and watch its logs.
sudo systemctl start postgresql
sudo journalctl -u postgresql -fThe logs should show the server reading archived WAL and reaching the requested recovery target. If a required WAL file is missing, recovery cannot pass that point. Restore the missing segment to the archive or choose a target before the gap.
Use a named restore point
A named restore point gives an operational event a recognizable recovery target. Create one before a risky migration or maintenance operation.
SELECT pg_create_restore_point('before_schema_migration');
SELECT pg_switch_wal();To recover to this marker, replace the timestamp setting with:
recovery_target_name = 'before_schema_migration'
recovery_target_action = 'promote'The pg_switch_wal() call completes the current WAL segment, helping the archiver make recent records available. It does not replace a reliable archive process.
Verify the recovered database
After promotion, connect to the server and check:
- The target data is present and unwanted changes are absent.
- Applications can connect with the expected permissions.
- Critical tables and indexes are usable.
- The recovered server has enough disk space.
- New backups and WAL archiving are configured for the new primary.
Exercise
You restored a base backup and PostgreSQL reports that a WAL segment required to reach the target is missing. What is the correct response?
Show answer
Do not promote the server. Find and restore the missing archived segment, or choose a recovery target before the missing segment.