ERROR: CREATE DATABASE cannot run inside a transaction block (SQLSTATE 25001)
means the statement was sent after a BEGIN, and PostgreSQL does not allow it
there. Run it on its own, outside any transaction. VACUUM gives the same
error with its own name in the message.
In a script, move the statement before BEGIN or after
COMMIT. In a tool or library that wraps everything in a transaction, turn that
off for this one statement.
The error
BEGIN;
BEGIN
CREATE DATABASE shop_test;
psql:/tmp/q6.sql:3: ERROR: CREATE DATABASE cannot run inside a transaction block
And VACUUM, in the same situation:
BEGIN;
BEGIN
VACUUM;
psql:/tmp/q6.sql:7: ERROR: VACUUM cannot run inside a transaction block
Why it happens
Most PostgreSQL commands, table DDL included, can run inside a transaction and be rolled
back (a DROP TABLE inside BEGIN rolled back cleanly in the same
container). A few commands refuse.
CREATE DATABASE and VACUUM are two of them: both checked for an
open transaction and stopped with code 25001 before doing anything.
The BEGIN is not always yours. A tool that wraps a whole script in one
transaction causes the same error even though the file never says BEGIN.
psql does exactly that with --single-transaction: a file containing only
CREATE DATABASE shop_test; failed with the same message when run that way.
The fix
CREATE DATABASE shop_test;
CREATE DATABASE
SELECT datname FROM pg_database WHERE datname = 'shop_test';
datname
-----------
shop_test
(1 row)
Outside the transaction the same statement succeeded at once. After the failed attempt
inside BEGIN, the transaction was aborted and needed a
ROLLBACK, which is why the script ends each failing block with one. If a
setup script must create a database and then tables in it, split it in two: create the
database first, then connect to it and run the rest, transaction and all. From .NET,
Npgsql reports this as a PostgresException with SqlState
25001.
How it was reproduced
A postgres:18 container (PostgreSQL 18.4, Debian build 18.4-1.pgdg13+1) on
Docker 29.9.2. A psql script opened a transaction with BEGIN and tried
CREATE DATABASE shop_test, rolled back, did the same with
VACUUM, then created the database outside any transaction and dropped it
again. A verbose run printed ERROR: 25001 for both statements.
Frequently asked
- How do I fix 'CREATE DATABASE cannot run inside a transaction block'?
- Run CREATE DATABASE on its own, not after BEGIN. If a migration tool or client wraps your script in a transaction, run this statement separately without that wrapper.
- Why does VACUUM say cannot run inside a transaction block?
- VACUUM is one of the PostgreSQL commands that refuse to run inside a transaction. Run it as a standalone statement with autocommit, outside BEGIN and COMMIT.
- Can I create a database and its tables in one script?
- Not in one transaction. Create the database first as a separate statement, then connect to the new database and create the tables, inside a transaction if you like.
More decoded errors in the Fixes category. If the database exists and cannot be dropped, see database is being accessed by other users (55006).