Goal: recover three post-backup orders to before their deletion, then verify and promote.
PITR replays a journal forward from a base copy; it cannot rewind files without that base. Make the archive owned by the postgres Linux account, which runs archive_command. Mode 755 permits student inspection without world-write access. Compression keeps the small lab’s retained WAL compact. We will keep this archive throughout recovery and promotion.
Goal: record requested settings before applying restart-only changes.
ALTER SYSTEM writes postgresql.auto.conf. Enable archive_mode, replica WAL, and WAL senders. In archive_command, %p is the source WAL path and %f its filename. gzip writes a temporary compressed file; mv publishes it only after compression succeeds. The existing-file test avoids overwriting an archived name. This local command is a teaching example, not a durable off-host archive design.
Goal: prove requested archiving configuration became active.
archive_mode, wal_level, and max_wal_senders require restart when changed. as-postgres changes Linux identity to operate the source directory; it does not change the student-owned recovery directory. Expect server stopped/started, then active archive_mode=on, wal_level=replica, max_wal_senders=10. A configuration file or restart message alone is insufficient.
Goal: observe successful archival rather than assuming a segment switch is enough.
pg_switch_wal closes a WAL segment so the archiver can process it. Wait for that background work. Read successful/failed counts and confirm the last archived name is a readable compressed file. Counts and 24-character hexadecimal names vary; expect at least one success and zero failures. A switch itself is not proof the archive command worked.
WAL_TO_WAIT captures the active segment filename before switching. The for loop checks for its completed gzip file up to 30 times with one-second waits; break stops when it arrives. The following test/gzip -t must succeed before ARCHIVE_READY prints. This waits for the required file, rather than assuming a fixed sleep or an older success count proves it is ready.
In the filename query, -At requests unaligned, tuples-only output so the shell variable contains just the name. seq 1 30 supplies the attempt numbers. gzip -t tests compressed-file integrity without extracting it. No ARCHIVE_READY means this step has not proved readiness.
Goal: establish the starting files before creating the orders.
-Fp is plain physical format; -X none deliberately omits WAL from the copy because this experiment supplies it through the archive. Such a base is not self-contained: retained complete WAL is mandatory for recovery. -c fast requests a checkpoint and -P displays progress. The orders do not exist in this base; their later creation must be recovered from WAL.
Goal: choose a time after committed good data and before the destructive transaction.
Create the separate orders fixture and commit its three rows: amounts 412.50, 89.00, and 1205.75, total 1707.25. Shell TS=$(...) captures server clock_timestamp; -At prints only the scalar and ON_ERROR_STOP rejects query errors. export keeps TS in this shell for later commands; echo makes it visible. Do not invent a timestamp or start a new shell. This is not the backup time: WAL must replay the newly created table and rows.
Goal: make the destructive commit available so recovery can stop before it.
DELETE removes three fixture rows. Verify the source is empty, then force a segment switch and wait for this specific segment to be archived. WAL containing both the good data and later DELETE must be available: reaching the time target uses transaction commit records, not a text search for DELETE. Expect archived=true and no_failures=true. The captured target remains before this committed deletion.
WAL_TO_WAIT captures the active segment filename before switching. The for loop checks for its completed gzip file up to 30 times with one-second waits; break stops when it arrives. The following test/gzip -t must succeed before ARCHIVE_READY prints. This waits for the required file, rather than assuming a fixed sleep or an older success count proves it is ready.
Goal: tell the base where to fetch WAL and when to pause.
Move the base to its recovery directory (mv renames it without duplicating files). recovery.signal selects archive recovery. printf appends restore_command and the saved target: %% emits a literal percent for PostgreSQL’s %f/%p substitution; the shell expands $TS now. gzip -dc reads the retained compressed archive into the requested WAL path. Set target action pause explicitly so we can inspect before promotion. The final grep displays the effective written target and command.
Goal: prove the server reached the requested recovery stop.
Start the student-owned recovery directory on 5434, separate from the source on 5432. Wait for readiness, then inspect recovery and replay state: expect recovering=true and replay_state=paused. This is stronger than merely seeing server started. A target before the base can reach consistency or a missing WAL segment would prevent the intended recovery.
Goal: verify the good transactions survived while the later delete did not.
Read source_orders on 5432: zero. On 5434, inspect all three recovered customer/amount rows and total 1707.25. Then SHOW transaction_read_only must be on. Keep the archive: it is recovery evidence and can be needed for retries. Counts alone are insufficient, and recovery=true alone would not prove the target was reached.
Goal: make the verified recovery an independent writable primary.
Only after verifying the data, promote the recovered directory. Promotion leaves recovery and starts a new timeline; it does not change the original server or redirect applications. Expect recovering=false and read-only off. Update a row inside a transaction and roll it back: RETURNING demonstrates writing while the final amounts remain unchanged. The WAL archive is retained.
Goal: finish after verified recovery while retaining the recovery artifacts.
Stop only /tmp/pitr_recover and confirm its PID file is gone. The source remains on 5432 with zero orders, and archived WAL remains for inspection. Source configuration resets, off-host retention, failover routing, and a new backup of the promoted timeline are operational follow-up topics, not demonstrated here.