متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 189Data · PostgreSQL

How to restore PostgreSQL to a point in time

At 10:15 someone ran DELETE FROM orders; without a WHERE. The nightly dump is from 02:00 — eight hours of orders would be lost. With point-in-time recovery (PITR) you restore a base backup and then replay the archived write-ahead log (WAL) up to 10:14, one minute before the mistake. This how-to turns on WAL archiving, takes a base backup, and does a full restore to a chosen time — practise it before you need it.

  • 5 الدروس
  • 8 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

189.1

The quick answer

/etc/postgresql/16/main/conf.d/archive.conf
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 -P

Restore to 10:14 (server stopped, data directory replaced by the base backup):

/etc/postgresql/16/main/conf.d/recovery.conf
restore_command = 'cp /backup/wal/%f %p'
recovery_target_time = '2026-11-20 10:14:00+03'
recovery_target_action = 'promote'
touch /var/lib/postgresql/16/main/recovery.signal
systemctl start postgresql

189.2

Step by step

  1. Archive every WAL file (quick answer) — archive_mode needs 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.
  2. 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;"
  1. 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.
  2. 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.
  3. 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
  1. Set the recovery target (quick answer, recovery.conf in conf.d) and create recovery.signal in the data directory, then start. PostgreSQL replays WAL and stops at the target.
  2. Watch the log until recovery ends:
journalctl -u postgresql@16-main -f

You 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.

  1. Clean up — remove recovery.conf from conf.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.

189.3

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.

189.4

Common problems

ProblemCauseFix
Log shows restore_command errors for a fileA 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 targetWAL after that time is not in the archive.Archive to a safe place; check with pg_stat_archiver.
Recovery runs past the mistakeTarget time without time zone, or wrong zone.Always write the time zone (+03).
Server starts as normal and ignores the targetrecovery.signal missing.Create it in the data directory before starting.
Archive disk full → primary stops archiving and pg_wal growsOld WAL never deleted.Retention in pgBackRest/WAL-G; delete WAL older than the oldest base backup you keep.
Next restart starts a recovery againRecovery settings left in the config.Remove recovery.conf from conf.d after the restore.

189.5

Go further

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.

كل الأوامر في هذا الفصل