**Approach (high level)**Restore = 1) Obtain a consistent base backup taken before target time, 2) configure recovery to replay WALs from S3 up to the timestamp, 3) start Postgres and monitor until recovery_target_time is reached, then promote.**Step-by-step**1. Confirm target and stop Postgres- Target: 2026-02-15T13:47:20Z- On the standby/restore host stop Postgres cleanly:bash
sudo systemctl stop postgresql-12
2. Obtain base backup- Find the most recent base backup whose start_time < target. If you use wal-g/wal-e, list backups:bash
wal-g backup-list
wal-g backup-fetch /var/lib/postgresql/12/main BACKUP_NAME
- If backups are stored as tarball on S3, download and extract:bash
aws s3 cp s3://my-pg-backups/basebackups/base_2026-02-14.tar.gz /tmp/
tar -xzf /tmp/base_2026-02-14.tar.gz -C /var/lib/postgresql/12/main
chown -R postgres:postgres /var/lib/postgresql/12/main
3. Prepare WAL access (restore_command)- Ensure WAL archive strategy: WAL files in s3://my-pg-wal/…- Create a script or use aws cli directly. Example restore_command in postgresql.conf (we will use recovery.signal instead of legacy recovery.conf):bash
# in /var/lib/postgresql/12/main/postgresql.conf (or include file)
restore_command = 'aws s3 cp s3://my-pg-wal/%f %p || test -f /var/lib/postgresql/12/main/%f'
- Make sure the Postgres user has AWS credentials (IAM role or ~/.aws/credentials).4. Configure recovery to stop at timestamp- Create recovery.signal file and set recovery_target_time plus restore_command in postgresql.conf (or recovery.conf old style).bash
# create recovery.signal to enable recovery mode
touch /var/lib/postgresql/12/main/recovery.signal
# add to postgresql.conf or include file:
recovery_target_time = '2026-02-15 13:47:20+00'
recovery_target_action = 'promote' # or 'pause' to inspect
restore_command = 'aws s3 cp s3://my-pg-wal/%f %p'
5. Ensure wal_level and timeline considerations- If you need a specific timeline, set recovery_target_timeline = 'latest' or a specific id.6. Start Postgres and monitorbash
sudo systemctl start postgresql-12
tail -f /var/log/postgresql/postgresql-12-main.log
- Postgres will call restore_command for each WAL segment. Watch logs for errors like missing WAL → fetch from S3 or re-run wal-g wal-fetch.7. Verify and promote- When logs show recovery completed to target time, confirm with:bash
psql -c "SELECT pg_is_in_recovery(), now();"
SELECT pg_last_xact_replay_timestamp();
- If recovery_target_action = 'promote' it will automatically switch to read-write. If 'pause', promote manually:bash
pg_ctl -D /var/lib/postgresql/12/main promote
**Edge cases & troubleshooting**- Missing WAL segments: check S3 path/permissions and that restore_command pattern matches filenames (%f).- Encryption or multipart uploads: ensure aws cli can access full objects.- Corrupt base backup: try an earlier base backup + more WALs.- Timezones: always use UTC or confirm timezone in recovery_target_time.**Why this works**- Base backup provides consistent image; WAL replay applies changes up to the requested timestamp. restore_command fetches WAL segments on demand from S3 so replay proceeds until recovery_target_time.