How to check if PostgreSQL schema exists using SQLAlchemy?

后端 未结 4 1272
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-12 15:07

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

4条回答
  •  独厮守ぢ
    2021-01-12 15:47

    @javax's answer is almost correct; the following is a little clarification:

    q = exists(select([("schema_name")]).select_from("information_schema.schemata")
        .where("schema_name = 'foo'"))
    if not session.query(q).scalar():
        session.execute('CREATE SCHEMA foo;')
    

提交回复
热议问题