Search code examples
postgresqlsqlalchemyalembic

Pass postgresql schema to alembic migration


I have a postgresql database and I want to pass postgresql schema name in DSN, like this: postgresql://login:password@postgreshost/dbname?schema=my_schema. I know that I can specify the schema keyword in migration operations like op.create_table. Unfortunately, upgrade() and downgrade() functions have no arguments in which I can pass postgres schema. Is there any way to pass schema name to op.create_table() without hardcoding it?


Solution

  • If that should always be the default schema, you can change the schema search path for the Postgres user:

    alter user the_user set search_path = my_schema;
    

    That will make all statements that use unqualified identifiers use my_schema as the default.