Sub topic 14
Proving the Backups Work with Test Restores
Assume you understand PostgreSQL clusters, Linux file permissions, WAL archiving, pg_basebackup, and recovery settings such as restore_command. A backup is useful only if PostgreSQL can restore it and the recovered data meets your recovery objectives. A successful backup command alone does not prove either condition.
Why test restores matter
A test restore checks the complete recovery chain:
- The base backup is readable and complete.
- Required WAL segments still exist.
- The archive storage returns the correct file for each requested segment.
- Recovery settings have the expected effect.
- PostgreSQL can start and replay the backup.
- The restored data is usable, not merely present on disk.
Test restores should run on an isolated host or temporary virtual machine. Never restore over the production data directory while testing.
A repeatable restore test
Record the backup identifier, its start and end times, the intended recovery target, and the WAL archive location. Keep the test output with that record.
A typical physical restore test follows this sequence:
- Provision an isolated PostgreSQL instance with a compatible major version.
- Stop the instance and remove its empty data directory.
- Copy or extract the selected base backup into the data directory.
- Set ownership and permissions for the PostgreSQL service account.
- Create
recovery.signal. - Configure
restore_commandand a recovery target. - Start PostgreSQL and collect the server log.
- Verify the recovered state with SQL and application-level checks.
- Destroy the temporary instance after preserving the test report.
For a PostgreSQL 16 test instance, the recovery settings might look like this:
restore_command = 'cp /mnt/archive/%f "%p"'
recovery_target_time = '2026-11-27 14:30:00+00'
recovery_target_action = 'promote'The archive path must be available to the test host. If the archive uses object storage, configure the same retrieval mechanism used by production, including credentials with read-only access.
Verify the recovery result
Check the PostgreSQL log first. Look for the base backup being accepted, WAL records being replayed, the recovery target being reached, and the server becoming ready for connections. Also investigate messages about missing files, checksum failures, or unexpected timeline history.
Then connect with psql and verify recovery status:
SELECT pg_is_in_recovery();
SELECT current_timestamp,
pg_last_wal_replay_lsn(),
pg_get_wal_replay_pause_state();After recovery_target_action = 'promote', pg_is_in_recovery() should return false. The replay LSN and recovery timestamp should match the test record closely enough for your recovery objective.
Data checks should include known invariants, such as row counts, recent transaction identifiers, foreign-key relationships, and application health checks. Compare the results with an independently recorded expectation. A database that starts successfully can still contain incomplete or logically inconsistent data if the target was chosen incorrectly.
Test failure and frequency
A failed test is evidence to investigate, not a reason to silently choose an older backup. Common causes include expired WAL, incorrect archive permissions, an invalid restore_command, missing timeline history, and a base backup copied before all required WAL was retained.
Run restore tests on a schedule that matches your recovery requirements. Keep the latest successful test report, the exact commands, PostgreSQL version, backup identifier, target, and validation results. Automate the test, but require an alert when any stage fails.
Exercise
A test restore starts successfully, but a required order is missing at the selected recovery time. What should you investigate first?
Show answer
Check that the target time and timezone are correct, confirm that all WAL through the target was available, and verify the order with an independent application or transaction-level check.