I'm able to create a schema via the command line using CREATE SCHEMA test_schema;.
However, running the following code doesn't create a schema:
from sqlalchemy import create_engine from sqlalchemy.schema import CreateSchema def main(): conn_str = "postgresql+psycopg2://<myusername>:<mypassword>@localhost/belgarath_test" engine = create_engine(conn_str, echo=True) connection = engine.connect() connection.execute(CreateSchema("test_schema")) if __name__ == "__main__": main() Weirdly sqlalchemy does emit the correct sql. Here's the full output:
2024-12-29 17:54:22,128 INFO sqlalchemy.engine.Engine select pg_catalog.version() 2024-12-29 17:54:22,129 INFO sqlalchemy.engine.Engine [raw sql] {} 2024-12-29 17:54:22,129 INFO sqlalchemy.engine.Engine select current_schema() 2024-12-29 17:54:22,129 INFO sqlalchemy.engine.Engine [raw sql] {} 2024-12-29 17:54:22,130 INFO sqlalchemy.engine.Engine show standard_conforming_strings 2024-12-29 17:54:22,130 INFO sqlalchemy.engine.Engine [raw sql] {} 2024-12-29 17:54:22,130 INFO sqlalchemy.engine.Engine BEGIN (implicit) 2024-12-29 17:54:22,130 INFO sqlalchemy.engine.Engine CREATE SCHEMA test_schema # <------------------ 2024-12-29 17:54:22,130 INFO sqlalchemy.engine.Engine [no key 0.00008s] {} Any ideas on why the schema isn't created?
COMMITin that sequence, in which case the schema creation would not survive the session or be seen by another session. Read Commit as you go. 2) If by ...via the command line you mean usingpsql, thenpsqlhasautocommitby default, so theCREATE SCHEMA test_schema;would be committed.