Point-in-Time Recovery (PITR) is a powerful feature in PostgreSQL that allows database administrators to restore a database to a specific moment in time—usually just before a failure, human error, or data corruption event. It’s an essential part of a solid disaster recovery plan.
DROP
or DELETE
operations.postgresql.conf
Edit the following parameters:
wal_level = replica archive_mode = on archive_command = 'cp %p /var/lib/postgresql/wal_archive/%f'
Restart PostgreSQL:
sudo systemctl restart postgresql
Using pg_basebackup
:
pg_basebackup -D /var/lib/postgresql/base_backup -F tar -X fetch -P -U postgres
Assume the table was dropped and now you need recovery.
Stop PostgreSQL:
sudo systemctl stop postgresql
cd /var/lib/postgresql/data rm -rf * tar -xvf /var/lib/postgresql/base_backup/base.tar
recovery.conf
or set recovery parameterstouch standby.signal
postgresql.conf
or create postgresql.auto.conf
:restore_command = 'cp /var/lib/postgresql/wal_archive/%f %p' recovery_target_time = '2025-06-17 12:29:59'
sudo systemctl start postgresql
PostgreSQL will apply WAL files and stop exactly at the given timestamp.
You can confirm recovery by checking the logs:
cat /var/log/postgresql/postgresql-13-main.log