无法删除当前打开的数据库

无法删除当前打开的数据库

本文介绍了Django无法删除数据库:psycopg2.OperationalError:无法删除当前打开的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Whenever I try to run my Django tests via manage.py, the tests run fine however at the end when Django is destroying the database the following error occurs:

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无法删除数据库?

I've checked to ensure that nothing else is connected to the database - why can't Django drop the database?

注意:我正在使用PostgreSQL

Note: I am using PostgreSQL

推荐答案

TL; DR



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

TL;DR

If you just want the solution, here it is.Make sure that your Postgres server has a "postgres" database. You can check by connecting via psql, and running /list or /l. You can create the database by running: CREATE DATABASE postgres in psql.

Django宁愿不运行 DATABASES 设置中指定的默认数据库中的初始化查询(可能是创建测试数据库之类的事情) 。假定这是生产数据库,所以在Django的最大利益是不要乱扔在那里。

Django prefers to not run "initialization queries" (presumably things like creating the test database) from the "default" database specified in your DATABASES setting. This is presumed to be the production database, so it would be in Django's best interests to not mess around there.

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

This is why at the beginning of the tests, this is shown (ignore my filesystem):

    /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 设置,它是运行查询以创建和删除测试数据库的地方。 (注意:Django不需要在您的设置中显式指定 postgres数据库,它只是在您的设置中指定的任何数据库服务器上查找它。)

Django looks for a database named "postgres" on the host specified in your DATABASES settings, this is where it runs the queries to create and delete your test database. (Note: Django does not need the "postgres" database to be explicitly specified in your settings, it just looks for it on whatever database server is specified in your settings).

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

It appears that if this "postgres" database does not exist, Django can create the test database and run the tests, but it cannot delete the test database. This may be because Postgres does not allow you to drop the database you are currently connected to, however to me it seems that there is no reason that Django couldn't just drop the test database from the "default" (production) database specified in the settings. I presume it is using the "default" database to create the test database, so it seems there's no reason why it can't delete it as well.

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

Regardless, the solution was just to create that "postgres" database with a simple SQL statement: CREATE DATABASE postgres. After that database was created, everything worked fine.

这篇关于Django无法删除数据库:psycopg2.OperationalError:无法删除当前打开的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 23:49