Is it possible to wrap DDL changes in a transaction in PostgreSQL?

benstpierre picture benstpierre · Nov 19, 2012 · Viewed 10.8k times · Source

I know that in MySQL ddl statements such as alter table/create table/etc cause an implicit transaction commit.

As we are moving to PostgreSQL is it possible to wrap multiple DDL statments in a transaction?

This would make migration scripts a lot more robust, a failed DDL change would cause everything to rollback.

Answer

Gary picture Gary · Nov 19, 2012

DDL statements are covered by transactions. I can't find the relevant section in the official documentation, but have provided a link to the wiki which covers it.

Just remember that transactions aren't automatically opened in postgresql, you must start them with BEGIN or START TRANSACTION.

Postgresql Wiki about Transactional DDL