I am using SQLAlchemy to generate tables in a specific schema in a PostgreSQL database. If the schema does not exist, I want to create it. I know the PostgreSQL query to che
This worked for me. Finds if a schema exists using pure SqlAlchemy:
from sqlalchemy import create_engine
from sqlalchemy import event
from sqlalchemy.schema import CreateSchema, DropSchema
...
engine = create_engine(dbstr)
meta = ModelBase()
...
if not engine.dialect.has_schema(engine, schema=schema_name):
event.listen(meta.metadata, 'before_create', CreateSchema(schema_name))
meta.metadata.reflect(bind=engine, schema=schema_name)
meta.metadata.create_all(engine, checkfirst=True)