Articles

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.

Updated

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.

bash
# 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:

python
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" printed DELETE 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 2h returned in 3 seconds. After attaching it, the query found its binding ready on the third try, 5 seconds apart, and printed 1000 4984869 followed by Read-only transaction, rolled back (2031 ms): every order, as it was before the DELETE.
  • Copy everything back. With every row gone, a plain COPY from the branch into the live table restored all 1,000 rows. The gregale app exec task, VM start included, took 3 seconds, and the app returned {"orders":1000,"total_cents":4984869} again.
  • Keep later writes. Then the harder case: a DELETE of 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 printed restored 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:27Z returned in 2 seconds, and the copy was ready 15 seconds after the command started, showing restored_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 2hgregale postgres restore … --name
What you getA new database that shares its source's storage until written toA new database
Counts toward the plan's database limitNo, with a TTL of 7 days or lessYes; one restore may go one database past the limit for 7 days
How many at onceHobby 2, Pro 5, Scale 20 short-lived branchesUp to the database limit
When it goes awayPurged at its TTL, with no undoWhen you delete it, with the 7-day undo
Use it forCopying rows back, checking what changedKeeping 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 DELETE does 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 with TRUNCATE … RESTART IDENTITY, the ids can collide: compare id ranges first and call setval after the copy. A column declared generated always as identity needs overriding system value in the INSERT, as above; COPY does not.
  • Foreign keys and cascades. If the DELETE cascaded, the child rows are gone too. Copy the parents first, then the children, from the same branch.
  • Updated rows. on conflict do nothing keeps the live row. For a bad UPDATE rather than a DELETE, use on 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_write credential can select, insert, update and delete in the public schema, 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.

PlanPriceDatabasesIncluded restore historyShort-lived branches at once
Hobby$10 a month11 GiB2
Pro$30 a month35 GiB5
Scale$99 a month1020 GiB20

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:

HostHow far backCheapest way to get itWhere the restore goes
Neon6 hours on Free (1 GB limit), up to 7 days on Launch, up to 30 on ScaleLaunch, billed on usage; instant restore history at $0.20 per GB-monthInstant restore overwrites the branch and saves its old state as another branch
Supabase7, 14 or 28 days with the PITR add-onPro at $25 a month plus the add-on from $100 a monthReplaces the current database; restoring to a new project is a separate option
Render3 days on a Hobby workspace, 7 on Pro or higherA paid instance, from $6 a monthA new instance
Heroku4 days on Standard, 1 week on Premium; Essential has no rollbackStandard-0, up to $50 a monthA new database
DigitalOceanAny point in the previous 7 days$15.15 a month for the smallest clusterA new cluster
RailwayOpt-in, roughly 4 weeks for Postgres, from the first backup after you turn it onUsage, as storage bucket and egressA new service next to the source
Gregale7 days on Hobby, Pro and ScaleHobby, $10 a monthA 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 get and gregale postgres doctor report, 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 orders brings it back, data and restore history included, for 7 days after the delete.
  • gregale app exec needs 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.