Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rollback an update in PostgreSQL

While editing some records in my PostgreSQL database using sql in the terminal (in ubuntu lucid), I made a wrong update.

Instead of -

update mytable set start_time='13:06:00' where id=123;

I typed -

update mytable set start_time='13:06:00';

So, all records are now having the same start_time value.

Is there a way to undo this change? There are some 500+ records in the table, and I do not know what the start_time value for each record was

Is it lost forever?

like image 643
damon Avatar asked Oct 14 '12 09:10

damon


People also ask

How do I revert changes in PostgreSQL?

Use a PostgreSQL native interface database connection to insert product data from MATLAB® into a new table in a PostgreSQL database. Then, reverse the changes made to the database. Create a PostgreSQL native interface database connection to a PostgreSQL database using the data source name, user name, and password.

How do I ROLLBACK an update on Pgadmin?

If you are using pgAdmin4, you can turn the auto commit and/or auto rollback on and off. Go to the File drop down menu and select Preferences option. In the SQL editor tab -> Options you can see the options to turn auto commit/rollback on and off.

Does Postgres have ROLLBACK?

PostgreSQL ROLLBACK command is used to undo the changes done in transactions. As we know transactions in database languages are used for purpose of large computations, for example in banks.


2 Answers

I'm assuming it was a transaction that's already committed? If so, that's what "commit" means, you can't go back.

Some data may be recoverable if you're lucky. Stop the database NOW.

Here's an answer I wrote on the same topic earlier. I hope it's helpful.

This might be too: Recoved deleted rows in postgresql .

Unless the data is absolutely critical, just restore from backups, it'll be lots easier and less painful. If you didn't have backups, consider yourself soundly thwacked.

like image 105
Craig Ringer Avatar answered Oct 13 '22 01:10

Craig Ringer


If you catch the mistake and immediately bring down any applications using the database and take it offline, you can potentially use Point-in-Time Recovery (PITR) to replay your Write Ahead Log (WAL) files up to, but not including, the moment when the errant transaction was made. This would return the database to the state it was in prior, thus effectively 'undoing' that transaction.

As an approach for a production application database it has a number of obvious limitations, but there are circumstances in which PITR may be the best option available, especially when critical data loss has occurred. However, it is of no value if archiving was not already configured before the corruption event.

https://www.postgresql.org/docs/current/static/continuous-archiving.html

Similar capabilities exist with other relational database engines.

like image 30
hemp Avatar answered Oct 13 '22 00:10

hemp