AlloyDB- CREATE DATABASE can not be run in a transaction block
ff-sdesai opened this issue · comments
Sumit Desai commented
Since AlloyDB does not support creating a database using terraform yet, I am trying to create it using Alembic and sqlAlchemy. I am using postgresql+psycopg2 as the driver.My code looks similar to the only answer on here.
from sqlalchemy import create_engine
dburl = "postgresql+psycopg2://user:pswd@myip:5432/postgres/"
engine = create_engine(dburl)
conn = engine.connect()
con.rollback() # Make sure we're not in a transaction
con.autocommit = True # Turn on autocommit
conn.execute("CREATE DATABASE qux")
con.autocommit = False # Turn autocommit back off again
However, I am getting an error sqlalchemy.exc.InternalError: (psycopg2.errors.ActiveSqlTransaction) CREATE DATABASE cannot run inside a transaction block. Is this expected when I am turning on auto-commit and rollick back transaction?
Additional metadata-
- OS: Ubuntu 22.04
- Psycopg version: 2.9.9
- Python version: 3.10
- PostgreSQL version: AlloyDB
- pip version- 3
Daniele Varrazzo commented
psycopg work no problem with CREATE DATABASE. Please get in touch with SQLAlchemy to figure out why it doesn't.