Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rollback DML statement in pgAdmin

In pgAdmin, if I execute an insert query, I don't see any way to either commit or rollback the statement I just ran (I know it auto commits). I'm used to Oracle and SQL developer, where I could run a statement, and then rollback the last statement I ran with a press of a button. How would I achieve the same thing here?

like image 414
chrismarx Avatar asked Mar 29 '12 00:03

chrismarx


People also ask

How do I rollback data in PostgreSQL?

BEGIN TRANSACTION − To start a transaction. COMMIT − To save the changes, alternatively you can use END TRANSACTION command. ROLLBACK − To rollback the changes.

Can we rollback drop command in PostgreSQL?

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.

Does Postgres support rollback?

In PostgreSQL the transaction cannot continue when you encounter an error and the entire work done in the transaction is rolled back. Oracle or DB2 have implicit savepoint before each statement execution which allow a rollback to the state just before the statement failure.


1 Answers

Use transaction in the SQL window:

BEGIN;
DROP TABLE foo;
ROLLBACK; -- or COMMIT;

-- edit -- Another example:

BEGIN;
INSERT INTO foo(bar) VALUES ('baz') RETURNING bar; -- the results will be returned
SELECT * FROM other_table; -- some more result
UPDATE other_table SET var = 'bla' WHERE id = 1 RETURNING *; -- the results will be returned

-- and when you're done with all statements and have seen the results:
ROLLBACK; -- or COMMIT 
like image 191
Frank Heikens Avatar answered Oct 13 '22 16:10

Frank Heikens