Backing up Postgres database rows before deleting them

Hmmm, this delete statement is taking longer than I thought it would…

If you ever have to manually run a SQL delete statement in psql, you can back-up the rows you’re about to delete:

\copy ( select * from $table where id in ( ... ) ) to '/tmp/backup.csv'

and, if you’ve got your filter clause wrong (we’ve all done it), restore them with:

\copy $table from '/tmp/backup.csv'

Moderately useful.

——————

Something wrong? Suggest an improvement or add a comment (see article history)
Tagged with: postgres
Filed in: tips

Previous: Avoiding clashing Django migrations
Next: commandlinefu.com is in new hands

Copyright © 2005-2024 David Winterbottom
Content licensed under CC BY-NC-SA 4.0.