Trap Exception in POSTGRESQL script I want to run a script inside a transaction in postgresql. So I suround the sql with begin , commit statements. But I want to rollback on error. I dont see how to do that.
BEGIN;
UPDATE public.tablename
SET blah = 'xxx'
WHERE thing= '123';

COMMIT;
You set a savepoint and after the comand a Rollback 


CREATE tABLE tablename (blah varchar(3), thing varchar(3))




BEGIN;
SAVEPOINT my_savepoint;
   UPDATE tablename
   SET blah = 'xxx'
   WHERE thing= '123';
ROLLBACK TO my_savepoint;
COMMIT;


db&lt;&gt;fiddle here