Sql – Is it possible to wrap DDL changes in a transaction in PostgreSQL

ddlpostgresqlsqltransactions

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.

Best Answer

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