Erstelle eine Postgres-Datenbank mit python
Ich möchte eine Postgres-Datenbank mit Python erstellen.
con = psql.connect(dbname='postgres',
user=self.user_name, host='',
password=self.password)
cur = con.cursor()
cur.execute("CREATE DATABASE %s ;" % self.db_name)
Ich erhalte den folgenden Fehler:
InternalError: CREATE DATABASE cannot run inside a transaction block
Ich verwende psycopg2, um eine Verbindung herzustellen. Ich verstehe nicht, wo das Problem liegt. Ich versuche, eine Verbindung zur Datenbank (Postgres) herzustellen:
psql -postgres -U UserName
Und dann erstelle eine andere Datenbank:
create database test;
Das ist, was ich normalerweise mache und ich möchte dies automatisieren, indem ich ein Python-Skript erstelle.