【问题标题】:Django can't drop database: psycopg2.OperationalError: cannot drop the currently open databaseDjango can't drop database: psycopg2.OperationalError: cannot drop the current open database
【发布时间】:2016-09-18 23:01:03
【问题描述】:

每当我尝试通过 manage.py 运行我的 Django 测试时,测试运行良好,但最后当 Django 销毁数据库时,会出现以下错误:

Destroying test database for alias 'default'...
Traceback (most recent call last):
  File "/Users/dcgoss/Desktop/Pickle/PickleBackend/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
psycopg2.OperationalError: cannot drop the currently open database


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py", line 129, in <module>
    utility.execute()
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py", line 104, in execute
    PycharmTestCommand().run_from_argv(self.argv)
  File "/Users/dcgoss/Desktop/Pickle/PickleBackend/venv/lib/python3.4/site-packages/django/core/management/commands/test.py", line 30, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/Users/dcgoss/Desktop/Pickle/PickleBackend/venv/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/dcgoss/Desktop/Pickle/PickleBackend/venv/lib/python3.4/site-packages/django/core/management/commands/test.py", line 74, in execute
    super(Command, self).execute(*args, **options)
  File "/Users/dcgoss/Desktop/Pickle/PickleBackend/venv/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py", line 91, in handle
    failures = TestRunner(test_labels, verbosity=verbosity, interactive=interactive, failfast=failfast, keepdb='--keepdb' in sys.argv)
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_runner.py", line 256, in run_tests
    extra_tests=extra_tests, **options)
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_runner.py", line 156, in run_tests
    return super(DjangoTeamcityTestRunner, self).run_tests(test_labels, extra_tests, **kwargs)
  File "/Users/dcgoss/Desktop/Pickle/PickleBackend/venv/lib/python3.4/site-packages/django/test/runner.py", line 534, in run_tests
    self.teardown_databases(old_config)
  File "/Users/dcgoss/Desktop/Pickle/PickleBackend/venv/lib/python3.4/site-packages/django/test/runner.py", line 509, in teardown_databases
    connection.creation.destroy_test_db(old_name, self.verbosity, self.keepdb)
  File "/Users/dcgoss/Desktop/Pickle/PickleBackend/venv/lib/python3.4/site-packages/django/db/backends/base/creation.py", line 264, in destroy_test_db
    self._destroy_test_db(test_database_name, verbosity)
  File "/Users/dcgoss/Desktop/Pickle/PickleBackend/venv/lib/python3.4/site-packages/django/db/backends/base/creation.py", line 283, in _destroy_test_db
    % self.connection.ops.quote_name(test_database_name))
  File "/Users/dcgoss/Desktop/Pickle/PickleBackend/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/dcgoss/Desktop/Pickle/PickleBackend/venv/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/dcgoss/Desktop/Pickle/PickleBackend/venv/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/dcgoss/Desktop/Pickle/PickleBackend/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
django.db.utils.OperationalError: cannot drop the currently open database

我已经检查以确保没有其他任何东西连接到数据库 - 为什么 Django 不能删除数据库?

注意:我使用的是 PostgreSQL

【问题讨论】:

  • 我遇到了同样的问题。我提交了一张包含我设置的许多详细信息的票,但由于我不清楚的原因而“无法重现”。 code.djangoproject.com/ticket/27751

标签: django postgresql psycopg2


【解决方案1】:

TL;DR

如果您只是想要解决方案,这里就是。 确保您的 Postgres 服务器具有“postgres”数据库。您可以通过psql 连接并运行/list/l 来检查。您可以通过在 psql 中运行:CREATE DATABASE postgres 来创建数据库。

扩展

Django 不喜欢从DATABASES 设置中指定的“默认”数据库运行“初始化查询”(可能是创建测试数据库之类的事情)。这被认为是生产数据库,所以不要乱来,这符合 Django 的最佳利益。

这就是为什么在测试开始时会显示(忽略我的文件系统):

    /Users/dcgoss/Desktop/Pickle/PickleBackend/venv/lib/python3.4/site-packages/django/db/backends/postgresql/base.py:247: 
    RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed 
    (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the default database instead.
    RuntimeWarning

Django 在您的DATABASES 设置中指定的主机上查找名为“postgres”的数据库,这是它运行查询以创建和删除您的测试数据库的地方。 (注意:Django 不需要在您的设置中明确指定“postgres”数据库,它只是在您的设置中指定的任何数据库服务器上查找它)。

如果这个“postgres”数据库不存在,Django 可以创建测试数据库并运行测试,但它不能删除测试数据库。这可能是因为 Postgres 不允许您删除当前连接的数据库,但是对我来说,似乎没有理由 Django 不能从指定的“默认”(生产)数据库中删除测试数据库设置。我认为它正在使用“默认”数据库来创建测试数据库,所以它似乎没有理由不能删除它。

无论如何,解决方案只是使用简单的 SQL 语句创建“postgres”数据库:CREATE DATABASE postgres。创建该数据库后,一切正常。

【讨论】:

  • 对于psql 命令,正斜杠应该是反斜杠:\list\l 以列出数据库和表。
  • 或者正如其他答案指出的那样,您可能拥有数据库postgres,但无法访问它。
  • 感谢您的回答,但我的问题是它根本没有运行。我收到了警告,然后在self._execute_create_test_db 中收到了RuntimeError: generator didn't stop。不明白这是从哪里来的!
【解决方案2】:

如果 postgres 数据库存在,请尝试在 pg_hba.conf 中添加对“postgres”数据库的访问权限。参考:https://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html

【讨论】:

  • 谢谢!这解决了我的问题。我在服务器上的pg_hba.conf 文件中有过于严格的规则。将数据库postgres 的访问权限授予django app 用户解决了这个问题。
【解决方案3】:

如果您将 pgbouncer 与 postgresql 一起使用,您可能希望将 postgres 数据库添加到 pgbouncer.ini 文件中的数据库列表中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 2022-01-16
    • 2016-07-24
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    相关资源
    最近更新 更多