使用 python、sqlalchemy 和 psycopg2 创建 PostgreSQL 数据库时出错

发布于 2024-10-25 04:49:16 字数 3958 浏览 3 评论 0 原文

我使用 sqlalchemy,它使用 psycopg2 连接到 postgresql 服务器。

当我启动以下代码时:

from sqlalchemy.engine.url import URL
from sqlalchemy.engine import create_engine
url = URL(drivername='postgresql', username='myname', password='mypasswd', host='localhost', database='template1')
eng = create_engine(url)
eng.execute('CREATE DATABASE new_db;')

我总是收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1788, in execute
    return connection.execute(statement, *multiparams, **params)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1191, in execute
    params)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1287, in _execute_text
    return self.__execute_context(context)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1302, in __execute_context
    context.parameters[0], context=context)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1401, in _cursor_execute
    context)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1394, in _cursor_execute
    context)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/default.py", line 299, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.InternalError: (InternalError) CREATE DATABASE cannot run inside a transaction block
 'CREATE DATABASE new_db;' {}

当我尝试使用 url 而不指定数据库 参数时:

url = URL(drivername='postgresql', username='myname', password='mypasswd', host='localhost')

我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1787, in execute
    connection = self.contextual_connect(close_with_result=True)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1829, in contextual_connect
    self.pool.connect(), 
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/pool.py", line 182, in connect
    return _ConnectionFairy(self).checkout()
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/pool.py", line 369, in __init__
    rec = self._connection_record = pool.get()
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/pool.py", line 213, in get
    return self.do_get()
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/pool.py", line 732, in do_get
    con = self.create_connection()
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/pool.py", line 147, in create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/pool.py", line 253, in __init__
    self.connection = self.__connect()
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/pool.py", line 319, in __connect
    connection = self.__pool._creator()
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/strategies.py", line 82, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/default.py", line 249, in connect
    return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.OperationalError: (OperationalError) FATAL:  database "roma" does not exist
 None None

如何解决该问题?

I use sqlalchemy that uses psycopg2 for connecting to postgresql servers.

When I launch the following code:

from sqlalchemy.engine.url import URL
from sqlalchemy.engine import create_engine
url = URL(drivername='postgresql', username='myname', password='mypasswd', host='localhost', database='template1')
eng = create_engine(url)
eng.execute('CREATE DATABASE new_db;')

I always get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1788, in execute
    return connection.execute(statement, *multiparams, **params)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1191, in execute
    params)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1287, in _execute_text
    return self.__execute_context(context)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1302, in __execute_context
    context.parameters[0], context=context)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1401, in _cursor_execute
    context)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1394, in _cursor_execute
    context)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/default.py", line 299, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.InternalError: (InternalError) CREATE DATABASE cannot run inside a transaction block
 'CREATE DATABASE new_db;' {}

When I try to use a url without specifying a database argument:

url = URL(drivername='postgresql', username='myname', password='mypasswd', host='localhost')

I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1787, in execute
    connection = self.contextual_connect(close_with_result=True)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/base.py", line 1829, in contextual_connect
    self.pool.connect(), 
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/pool.py", line 182, in connect
    return _ConnectionFairy(self).checkout()
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/pool.py", line 369, in __init__
    rec = self._connection_record = pool.get()
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/pool.py", line 213, in get
    return self.do_get()
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/pool.py", line 732, in do_get
    con = self.create_connection()
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/pool.py", line 147, in create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/pool.py", line 253, in __init__
    self.connection = self.__connect()
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/pool.py", line 319, in __connect
    connection = self.__pool._creator()
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/strategies.py", line 82, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.6.6-py2.7.egg/sqlalchemy/engine/default.py", line 249, in connect
    return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.OperationalError: (OperationalError) FATAL:  database "roma" does not exist
 None None

How can I fix the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

末骤雨初歇 2024-11-01 04:49:16

不使用 ORM 会话也是如此:

conn = eng.connect()
conn.connection.connection.set_isolation_level(0)
conn.execute('create database test')
conn.connection.connection.set_isolation_level(1)

当然没有理由使用 ORM 在普通数据库连接上设置隔离级别,对吧?

Same without using ORM Session:

conn = eng.connect()
conn.connection.connection.set_isolation_level(0)
conn.execute('create database test')
conn.connection.connection.set_isolation_level(1)

Surely there would be no reason to use ORM to set isolation level on plain DB connection, right?

讽刺将军 2024-11-01 04:49:16
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine('postgresql+psycopg2://USER:[email protected]:5432/DB_OR_TEMPLATE')
session = sessionmaker(bind=engine)()
session.connection().connection.set_isolation_level(0)
session.execute('CREATE DATABASE test')
session.connection().connection.set_isolation_level(1)

如果您没有任何数据库,则应使用 template1

"""Isolation level values."""
ISOLATION_LEVEL_AUTOCOMMIT     = 0
ISOLATION_LEVEL_READ_COMMITTED = 1
ISOLATION_LEVEL_SERIALIZABLE   = 2

http://initd.org/psycopg/docs/connection.html#connection.set_isolation_level

http://initd.org/psycopg/docs/extensions.html#isolation-level-constants

http://www.postgresql.org/docs/current/static/transaction-iso.html

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine('postgresql+psycopg2://USER:[email protected]:5432/DB_OR_TEMPLATE')
session = sessionmaker(bind=engine)()
session.connection().connection.set_isolation_level(0)
session.execute('CREATE DATABASE test')
session.connection().connection.set_isolation_level(1)

If you don't have any databases, you should use template1

"""Isolation level values."""
ISOLATION_LEVEL_AUTOCOMMIT     = 0
ISOLATION_LEVEL_READ_COMMITTED = 1
ISOLATION_LEVEL_SERIALIZABLE   = 2

http://initd.org/psycopg/docs/connection.html#connection.set_isolation_level

http://initd.org/psycopg/docs/extensions.html#isolation-level-constants

http://www.postgresql.org/docs/current/static/transaction-iso.html

爱你不解释 2024-11-01 04:49:16

POstgresql > 11 conn.autocommit = True

POstgresql >11 conn.autocommit = True

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文