هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
archive_mode = on
archive_command = 'test ! -f /backup/wal/%f && cp %p /backup/wal/%f'systemctl restart postgresql
sudo -u postgres pg_basebackup -D /backup/base/2026-11-20 -Ft -z -X stream -PRestore to 10:14 (server stopped, data directory replaced by the base backup):
restore_command = 'cp /backup/wal/%f %p'
recovery_target_time = '2026-11-20 10:14:00+03'
recovery_target_action = 'promote'Step by step
- Archive every WAL file (quick answer) —
archive_modeneeds a restart. The example copies to a local folder for clarity; in real life the archive must be on another machine or object storage — use pgBackRest or WAL-G, which also compress, encrypt and check. - Check the archiver works:
sudo -u postgres psql -c "SELECT pg_switch_wal();" -c "SELECT archived_count, last_archived_wal, failed_count FROM pg_stat_archiver;"- Take base backups regularly (quick answer,
pg_basebackup) — for example daily. You need the last base backup before the target time plus all WAL after it. - Find the target time — from app logs, audit logs or the slow log: the time just before the mistake. Write it with a time zone.
- Restore on a separate server first if you can (safer: you copy the lost rows back). On the same server:
systemctl stop postgresql
mv /var/lib/postgresql/16/main /var/lib/postgresql/16/main.broken
install -d -o postgres -g postgres -m 700 /var/lib/postgresql/16/main
tar xzf /backup/base/2026-11-20/base.tar.gz -C /var/lib/postgresql/16/main
tar xzf /backup/base/2026-11-20/pg_wal.tar.gz -C /var/lib/postgresql/16/main/pg_wal
chown -R postgres:postgres /var/lib/postgresql/16/main- Set the recovery target (quick answer,
recovery.confinconf.d) and createrecovery.signalin the data directory, then start. PostgreSQL replays WAL and stops at the target. - Watch the log until recovery ends:
journalctl -u postgresql@16-main -fYou see starting point-in-time recovery to 2026-11-20 10:14:00+03, then recovery stopping before commit of transaction … and database system is ready to accept connections.
- Clean up — remove
recovery.conffromconf.d(otherwise the next restart tries again), check the data, and take a new base backup at once: the server is on a new timeline.
Check that it worked
SELECT count(*) FROM orders; shows the rows from before 10:14; SELECT pg_is_in_recovery(); returns f (promoted); new writes work. The rows created between 10:14 and the restore are not there — recover them from app logs if needed.
Common problems
| Problem | Cause | Fix |
|---|---|---|
Log shows restore_command errors for a file | A WAL file is missing or unreadable in the archive. | Check failed_count in pg_stat_archiver; file rights for postgres; monitor the archiver. |
| Recovery ends before the target | WAL after that time is not in the archive. | Archive to a safe place; check with pg_stat_archiver. |
| Recovery runs past the mistake | Target time without time zone, or wrong zone. | Always write the time zone (+03). |
| Server starts as normal and ignores the target | recovery.signal missing. | Create it in the data directory before starting. |
| Archive disk full → primary stops archiving and pg_wal grows | Old WAL never deleted. | Retention in pgBackRest/WAL-G; delete WAL older than the oldest base backup you keep. |
| Next restart starts a recovery again | Recovery settings left in the config. | Remove recovery.conf from conf.d after the restore. |
Go further
- Command pages: psql, tar, journalctl.
- Next how-tos: Back up and restore PostgreSQL, Set up PostgreSQL replication, Back up a server with restic, Find slow PostgreSQL queries.
- Learn it: Data services — backups, Data services at work — PostgreSQL. Full guide: PostgreSQL.
1What two things does a point-in-time restore need?
A base backup taken before the target time, and every archived WAL file from that backup up to the target time.
2Why take a new base backup after a point-in-time restore?
The promoted server starts a new timeline; the old backups and WAL belong to the old history.