GUIDE

How to restore a Postgres database to an earlier point in time

Updated

Open the database's Settings tab in Kaiva Cloud, turn off deletion protection, and restore it to any moment in its history, up to a few seconds ago. The whole database goes back as it was at that moment, the state just before the restore is kept as a backup, and its agent tools follow the restored tables.

With Kaiva Cloud

A Kaiva Cloud database can be restored to an earlier time from its Settings tab. First turn off deletion protection on the same tab: every new database starts with it on, and a restore is refused while it is on. Then pick a moment in your own time zone, from the earliest the page shows up to a few seconds ago, type the database's name and confirm it's you. Owners and admins can restore.

Everything written after that moment is undone, and the state just before the restore is kept as a backup, named in the list of restores. Agent tools are read again, so Claude, ChatGPT, Cursor and any other AI client see the restored tables.

The steps below are the same by hand, for a Postgres database you run somewhere else.

Step by step

  1. Stop the writesPause the agent, job or deploy that made the change, so nothing more is written while you work.
  2. Find the momentUse your app's logs, the migration's time or the agent's transcript, and choose a time just before the change, with its time zone.
  3. Keep what is there nowTake a dump of the current database, or a branch or snapshot at your host, so rows written since the mistake can be copied back later.
  4. RestoreAt a managed host, choose the time in its console, often into a new database or branch next to the current one. On your own server, use the recovery settings below.
  5. Check, then switchCompare the tables and row counts with what you expect. Then point your app at the restored database, or copy only the lost rows back into the current one.

A copy of the current state, before you restore:

Terminal
pg_dump --format=custom --file=before-restore.dump "$DATABASE_URL"

On your own server

Point-in-time recovery needs a base backup taken before the target time and the write-ahead log archived since, kept with a tool such as pgBackRest, Barman or WAL-G. On PostgreSQL 12 and later, restore the base backup into an empty data directory, add the recovery settings, create recovery.signal and start the server. It replays the log up to the target, then opens for writes. pgBackRest and Barman can write these settings for you.

postgresql.conf, then the shell
restore_command = 'cp /mnt/wal_archive/%f %p'
recovery_target_time = '2026-11-30 14:05:00+00'
recovery_target_action = 'promote'

touch "$PGDATA/recovery.signal"
pg_ctl start -D "$PGDATA"

Restore everything, or copy rows back

A restore takes back everything written after the moment you choose, wanted or not.

What happenedWhat to do
A migration or a script changed many tablesRestore the whole database to before it
Some rows were deleted or overwrittenRestore a copy next to the current database and copy those rows back
People kept using the app after the mistakeCopy the lost rows back, so their newer writes stay

Before it happens again

Check how far back your host's history goes now, before you need it. Keep schema changes in migration files you review, and keep your production connection string out of the environment an AI agent works in.

When an agent only needs to read, give it a login that can only read. Kaiva Postgres MCP runs each call as one read-only statement in a transaction that is always rolled back.

Other ways to do it

  • Your host's point-in-time restoreMost managed Postgres hosts offer it, among them Amazon RDS, Google Cloud SQL, Neon and Supabase. On some plans it is an add-on, and how far back it reaches depends on the plan.
  • A scheduled dumppg_dump on a schedule restores to the moment of the dump only: anything written after it is lost. Useful as a second copy kept away from the database's own host.
  • A branch to compareHosts with branching, such as Neon, can open the database as it was at a past moment next to the current one, so you can copy rows back without restoring everything.

Questions

Does a restore keep what was written after the mistake?
No. Everything after the moment you choose is undone. Keep a copy first, and copy back the rows you need.
How far back can I go?
As far as the write-ahead log is kept: on a managed host, the history your plan keeps; on your own server, back to your oldest base backup with the log archived since.
Can I restore one table?
Not with point-in-time restore, which works on the whole database. Restore a copy next to it and copy that table's rows back.
My AI agent deleted data. Is restoring different?
No. Stop the agent first, then restore as for any other change. If the agent only needs to read, give it a read-only login before it connects again.
Is the state before the restore lost?
No. It is kept as a backup, named in the list of restores on the database's Settings tab.