Error when creating a PostgreSQL database using python, sqlalchemy and psycopg2

Same without using ORM Session:

conn = eng.connect()
conn.connection.connection.set_isolation_level(0)
conn.execute('create database test')
conn.connection.connection.set_isolation_level(1)

Surely there would be no reason to use ORM to set isolation level on plain DB connection, right?


from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine('postgresql+psycopg2://USER:[email protected]:5432/DB_OR_TEMPLATE')
session = sessionmaker(bind=engine)()
session.connection().connection.set_isolation_level(0)
session.execute('CREATE DATABASE test')
session.connection().connection.set_isolation_level(1)

If you don't have any databases, you should use template1

"""Isolation level values."""
ISOLATION_LEVEL_AUTOCOMMIT     = 0
ISOLATION_LEVEL_READ_COMMITTED = 1
ISOLATION_LEVEL_SERIALIZABLE   = 2

http://initd.org/psycopg/docs/connection.html#connection.set_isolation_level

http://initd.org/psycopg/docs/extensions.html#isolation-level-constants

http://www.postgresql.org/docs/current/static/transaction-iso.html