Undo a bad DELETE in Postgres without losing later writes
Branch the database from before the mistake and copy the missing rows back while the app keeps serving. Tested on Gregale, with how other hosts restore.
You can get back rows that a mistaken DELETE removed from a Gregale
managed PostgreSQL database at any time in the last 7 days, without losing
the writes that came after it. Branch the database from a moment before the
mistake, then copy the missing rows back into the live database. The live
database stays where it is and keeps serving, and orders, sign-ups or edits
made since the mistake stay too. In the production run below, the branch
command returned in 3 seconds and the copy of 1,000 rows took 3 more.
Recover the rows in four commands, then read what to watch for when copying rows back.
Why not just restore the database
A point-in-time restore gives you the database as it was at one moment. If you move your app onto that copy, everything written after that moment is gone from what the app sees: the orders that came in after the mistake, the users who signed up, the rows your jobs updated. That is the right trade when the mistake ruined everything after it, such as a migration that rewrote a table; for that case, roll back the code and the database together.
A DELETE with a wrong or missing WHERE is different: the damage is a set
of rows that are missing, and everything else is fine. So read the missing
rows from a copy and put them back, and leave the live database serving.
Recover the rows in four commands
Pick the restore point: a moment before the DELETE ran. Your app's logs,
gregale postgres get (which shows the range you can restore from) and the
time the alerts started are usually enough. Pick a point as close before it as
you can: rows created between the restore point and the DELETE are not in
the copy, and rows deleted on purpose in that gap would come back.
# 1. A short-lived branch of the database as it was before the mistake
gregale postgres branch orders orders-before-delete --from 2026-12-05T15:09:27Z --ttl 2h
# 2. Give the app a second connection string that points at the branch
gregale postgres attach orders-before-delete my-api --env RESTORED_DATABASE_URL
# 3. Check that the rows are there
gregale postgres query orders-before-delete --app my-api --env RESTORED_DATABASE_URL \
"select count(*), sum(amount_cents) from orders"
# 4. Copy back the rows the live database is missing
gregale app exec my-api -- python merge.py
gregale app exec runs the script in a fresh VM built from the app's live
deployment, with both connection strings in its environment, so no password
leaves the platform. merge.py reads the branch and inserts each row that
is missing from the live database, skipping rows that still exist:
import os, psycopg
src = psycopg.connect(os.environ["RESTORED_DATABASE_URL"])
dst = psycopg.connect(os.environ["DATABASE_URL"])
rows = src.execute("select id, amount_cents, created_at from orders").fetchall()
with dst.cursor() as cur:
cur.executemany(
"insert into orders (id, amount_cents, created_at) overriding system value"
" values (%s, %s, %s) on conflict (id) do nothing",
rows,
)
added = cur.rowcount
dst.commit()
print("restored", added, "rows; now", dst.execute("select count(*) from orders").fetchone()[0])
When you are done, gregale postgres branch delete orders orders-before-delete --yes
purges the branch, or its --ttl does it for you after 2 hours.
Verified on Gregale, December 5, 2026
The test app, pitr-demo-app, was FastAPI with psycopg, bound to a managed
PostgreSQL 17 database, pitr-demo-app-postgres. A release command created an orders table and filled it with
1,000 orders, and the app's / returned the count and the sum:
{"orders":1000,"total_cents":4984869}.
- The mistake. 30 seconds after noting the restore point,
gregale postgres query pitr-demo-app-postgres --write "delete from orders"printedDELETE 1000, and the app returned{"orders":0,"total_cents":0}. - Branch.
gregale postgres branch pitr-demo-app-postgres pitr-check --from 2026-12-05T15:09:27Z --ttl 2hreturned in 3 seconds. After attaching it, the query found its binding ready on the third try, 5 seconds apart, and printed1000 4984869followed byRead-only transaction, rolled back (2031 ms): every order, as it was before theDELETE. - Copy everything back. With every row gone, a plain
COPYfrom the branch into the live table restored all 1,000 rows. Thegregale app exectask, VM start included, took 3 seconds, and the app returned{"orders":1000,"total_cents":4984869}again. - Keep later writes. Then the harder case: a
DELETEof every other order (500 rows), followed by a new order of 4,242 cents, as if a customer had kept buying after the mistake. The app showed{"orders":501,"total_cents":2523365}. The merge script above printedrestored 500 rows; now 1001, and the app returned{"orders":1001,"total_cents":4989111}: the original 4,984,869 cents plus the new order. - Restore to a new database. For comparison,
gregale postgres restore pitr-demo-app-postgres --name pitr-demo-restored --point-in-time 2026-12-05T15:09:27Zreturned in 2 seconds, and the copy wasready15 seconds after the command started, showingrestored_from: … at 2026-12-05T15:09:27Z.
Branch or restore
Both read from the same restore history; they differ in how long the copy lives and whether it counts against your plan.
gregale postgres branch … --ttl 2h | gregale postgres restore … --name | |
|---|---|---|
| What you get | A new database that shares its source's storage until written to | A new database |
| Counts toward the plan's database limit | No, with a TTL of 7 days or less | Yes; one restore may go one database past the limit for 7 days |
| How many at once | Hobby 2, Pro 5, Scale 20 short-lived branches | Up to the database limit |
| When it goes away | Purged at its TTL, with no undo | When you delete it, with the 7-day undo |
| Use it for | Copying rows back, checking what changed | Keeping the copy, or moving an app onto it |
The branch's compute and anything written to it count toward your plan's usage like any database. The source database and its apps are never changed by either command.
What to watch for when copying rows back
- Identity columns. A
DELETEdoes not reset a sequence, so rows written after the mistake get new ids and never collide with the ones you put back. If the table was emptied withTRUNCATE … RESTART IDENTITY, the ids can collide: compare id ranges first and callsetvalafter the copy. A column declaredgenerated always as identityneedsoverriding system valuein theINSERT, as above;COPYdoes not. - Foreign keys and cascades. If the
DELETEcascaded, the child rows are gone too. Copy the parents first, then the children, from the same branch. - Updated rows.
on conflict do nothingkeeps the live row. For a badUPDATErather than aDELETE, useon conflict (id) do update set …with the columns to take back from the copy, and limit it to the rows the mistake touched. - Large tables. The script above holds every row in memory. Past a few hundred thousand rows, copy in batches by id range, or select only the ids that are missing.
- Permissions. The app's usual
read_writecredential can select, insert, update and delete in thepublicschema, and no more: in the run, creating a temporary table with it failed. Do the comparison in the script, as above, rather than in a scratch table on the live database.
What it costs
Point-in-time restore is part of managed PostgreSQL on Hobby, Pro and Scale, with a 7-day restore window on each. Gregale Free has no managed PostgreSQL.
| Plan | Price | Databases | Included restore history | Short-lived branches at once |
|---|---|---|---|---|
| Hobby | $10 a month | 1 | 1 GiB | 2 |
| Pro | $30 a month | 3 | 5 GiB | 5 |
| Scale | $99 a month | 10 | 20 GiB | 20 |
Restore history grows with how much your databases write, not with their size: rewriting 1 GiB a day keeps about 7 GiB. With overage on, history past the included amount, averaged over the month, costs $0.21 per GB-month. With overage off it is bounded instead: once a month's history and snapshots pass twice the included amount, your databases keep only the last 24 hours until the 1st of the next month.
How other hosts restore Postgres
Checked on each host's own pages on December 5, 2026:
| Host | How far back | Cheapest way to get it | Where the restore goes |
|---|---|---|---|
| Neon | 6 hours on Free (1 GB limit), up to 7 days on Launch, up to 30 on Scale | Launch, billed on usage; instant restore history at $0.20 per GB-month | Instant restore overwrites the branch and saves its old state as another branch |
| Supabase | 7, 14 or 28 days with the PITR add-on | Pro at $25 a month plus the add-on from $100 a month | Replaces the current database; restoring to a new project is a separate option |
| Render | 3 days on a Hobby workspace, 7 on Pro or higher | A paid instance, from $6 a month | A new instance |
| Heroku | 4 days on Standard, 1 week on Premium; Essential has no rollback | Standard-0, up to $50 a month | A new database |
| DigitalOcean | Any point in the previous 7 days | $15.15 a month for the smallest cluster | A new cluster |
| Railway | Opt-in, roughly 4 weeks for Postgres, from the first backup after you turn it on | Usage, as storage bucket and egress | A new service next to the source |
| Gregale | 7 days on Hobby, Pro and Scale | Hobby, $10 a month | A new database or a short-lived branch |
Render, Heroku, DigitalOcean and Railway restore to a new database, as Gregale does, so the copy-back recipe above works there too; only the commands that make the copy differ.
Choose another host when:
- You need more than 7 days. Neon's Scale plan keeps up to 30 days, Supabase's add-on up to 28 and Railway roughly 4 weeks.
- You want the database put back in place. Neon's instant restore and Supabase's PITR rewind the database you already connect to; on Gregale you copy rows back or move the app to the copy.
- You need a standby. A Gregale database runs in one zone with no high-availability promise; point-in-time restore and the 7-day undo of a delete are the recovery paths. Heroku Premium and DigitalOcean offer standby nodes.
- You are on a free plan. Neon Free keeps 6 hours of history; Gregale's managed PostgreSQL starts at Hobby.
Limits
- The restore point must be inside the database's restore range, which
gregale postgres getandgregale postgres doctorreport, along with any shorter policy in force. - A branch past its TTL is purged with no undo, so give it enough time to finish the copy.
- A database deleted by mistake does not need a restore:
gregale postgres recover ordersbrings it back, data and restore history included, for 7 days after the delete. gregale app execneeds app tasks, which every plan has.
Sources
Gregale facts come from the production run above, the managed PostgreSQL docs and plans. Other hosts, checked December 5, 2026: Neon pricing, Neon branch restore, Supabase pricing, Supabase backups, Render Postgres recovery and backups, Render pricing, Heroku Postgres plans, Heroku Postgres rollback, DigitalOcean restore from backups, DigitalOcean managed database pricing, Railway point-in-time recovery.
Questions
- How do I recover rows deleted by mistake in managed Postgres?
Make a copy of the database as it was before the DELETE, then insert the rows the live database is missing. On Gregale: gregale postgres branch orders orders-before-delete --from <time> --ttl 2h, attach the branch to the app as RESTORED_DATABASE_URL, and run a script with gregale app exec that inserts each row with ON CONFLICT DO NOTHING.
- Why not just restore the whole database to before the mistake?
Because everything written after that moment would disappear from what the app sees: new orders, sign-ups and job updates. Copying back only the missing rows keeps those writes. Restore the whole database when the mistake ruined everything after it, such as a migration that rewrote a table.
- How far back can Gregale restore a Postgres database?
Any moment in the last 7 days, on Hobby, Pro and Scale. gregale postgres get shows the range you can restore from. Hobby includes 1 GiB of restore history, Pro 5 GiB and Scale 20 GiB; history grows with how much the database writes, not with its size.
- Does a Postgres branch count toward my database limit?
Not if its TTL is 7 days or less. Hobby can have 2 such branches at once, Pro 5 and Scale 20, and each is purged at its TTL with no undo. A branch with a longer TTL, or a gregale postgres restore, is an ordinary database and counts toward the limit.
- How long does a point-in-time restore take on Gregale?
In a production run on December 5, 2026, the branch command returned in 3 seconds and its connection was ready within about 15 seconds; a restore to a new database was ready 15 seconds after the command started. Copying 1,000 rows back with gregale app exec took 3 seconds, VM start included.