Postgres usually has three databases by default. If you can connect as a superuser (for example, the postgres role), you can connect to the postgres or template1 databases. By default, pg_hba.conf only allows a unix user named postgres use the postgres role, so itβs easiest to just become that user. In any case, create the engine, as usual, with the user who has permissions to create the database:
>>> engine = sqlalchemy.create_engine("postgres://postgres@/postgres")
You cannot use engine.execute() , however, since postgres does not allow creating databases inside transactions, and sqlalchemy always tries to run queries in a transaction. To get around this, get a basic connection to the engine:
>>> conn = engine.connect()
But the connection will still be inside the transaction, so you need to complete the open transaction with commit :
>>> conn.execute("commit")
Then you can continue to create the database using the correct PostgreSQL command.
>>> conn.execute("create database test") >>> conn.close()
SingleNegationElimination Jan 23 '12 at 19:01 2012-01-23 19:01
source share