SQLAlchemy: “create schema if not exists”

I had the same question and the answer, which I found, is:

if not engine.dialect.has_schema(engine, schema_name):
    engine.execute(sqlalchemy.schema.CreateSchema(schema_name))

We can also check schema without engine instance, but using connection

conn = engine.connect()
if conn.dialect.has_schema(conn, schema_name):
    ...

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)