AI Hints
Home›PostgreSQL Tutorial›Backups and Replication
PostgreSQL · Lesson 95 of 120

Restore and Point-in-Time Recovery

Get it back.

⏱ 2 min read▶ Video at 1:25:41🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

Someone drops the reviews table by accident. With a custom format dump, pg restore can bring back just that table.

  • Oops, dropped
  • pg_restore
  • Just that table

Restore one table

We take a dump, then drop the reviews table. Pg restore with dash t restores only reviews from the dump. Run it. All two hundred and forty reviews are back.

backup.shbash
docker exec pg pg_dump -U postgres -d riverside -Fc -f /tmp/riverside.dump
docker exec pg psql -U postgres -d riverside -c "DROP TABLE reviews"

docker exec pg pg_restore -U postgres -d riverside -t reviews /tmp/riverside.dump
docker exec pg psql -U postgres -d riverside -c "SELECT count(*) FROM reviews"
Output
DROP TABLE
 count
-------
   240
(1 row)

Point-in-time recovery

A dump only saves one moment. Point in time recovery keeps a base backup plus the write ahead log, so you can restore to any second, like just before the mistake. Tools like pgBackRest manage it. And always practise restores.

keyvalue
Dumpone moment
WAL archiveevery change
PITRany second
pgBackRestmanages it

Summary

Pg restore brings data back, and point in time recovery can rewind to any moment. Next, replication.

Key takeaways
  • pg_restore
  • -t table
  • PITR
Next up: Replication — copies that stay in sync