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
. Can someone suggest how can this be fixed?