这样做是行不通的:
conn = psycopg.connect(dsn)
conn.execute("CREATE DATABASE test")下面是关于psycopg3:https://www.psycopg.org/psycopg3/docs/basic/transactions.html中事务的文档
关于这一问题的最重要声明是:
与psql相比,
有一种行为似乎令人惊讶:默认情况下,任何数据库操作都将启动一个新事务。
这是一个相当长的页面,但它没有告诉任何地方如何在不启动新事务的情况下执行语句。autocommit=True为connect()提供了一个参数,但也不起作用。
不管我做什么,我总是会犯这样的错误:
psycopg.errors.ActiveSqlTransaction: CREATE DATABASE cannot run inside a transaction block如何使用psycopg3创建数据库?
发布于 2022-01-07 16:25:46
使用自动提交连接对我有效:
>>> conn = psycopg.connect(dbname='postgres', autocommit=True)
>>> cur = conn.cursor()
>>> cur.execute('drop database if exists test3')
<psycopg.Cursor [COMMAND_OK] [IDLE] (user=xxx database=postgres) at 0x7f438ef92f00>
>>> cur.execute('create database test3')
<psycopg.Cursor [COMMAND_OK] [IDLE] (user=xxx database=postgres) at 0x7f438ef92f00>
>>> xxx@host psql postgres -tl | grep test3
test3 │ xxx │ UTF8 │ en_GB.UTF-8 │ en_GB.UTF-8 │ https://stackoverflow.com/questions/70620960
复制相似问题