I am attempting to create a PostgreSQL 12.1 database (not a schema) using the CREATE DATABASE statement from a Python 3.7.5 program connecting to a PostgreSQL 12.1 instance as user postgres over UnixODBC and using the PyODBC module, all running on Ubuntu 19.10. My program runs a loop that reads lines from a SQL DDL script, splitting SQL statements on the semicolon character. It logs the following errors:
16:15:20 ERROR Error executing statement 'CREATE DATABASE "TestV1" WITH OWNER = postgres': ('25001', '[25001] ERROR: CREATE DATABASE cannot run inside a transaction block;\nError while executing the query (1) (SQLExecDirectW)')
16:15:20 CRITICAL Unable to create objects: ('25001', '[25001] ERROR: CREATE DATABASE cannot run inside a transaction block;\nError while executing the query (1) (SQLExecDirectW)')
My SQL DDL script begins with the following line:
CREATE DATABASE "TestV1" WITH OWNER = postgres;
I've had success with CREATE SCHEMA in the same context, but not with CREATE DATABASE. As well, I cannot seem to find any answers here related to CREATE DATABASE on PostgreSQL specifically. I can create a database from the shell command line using createdb. I have tried setting AUTOCOMMIT on within the SQL DDL script, but that does not work.
autocommit=Trueinpyodbc.connect()?AutoCommit=Truein the connection string and it turns out to be the default, so the result is the same as if I left it out.autocommit=False.