Django cannot delete database: psycopg2.OperationalError: cannot delete current open database

Whenever I try to run Django tests using manage.py, the tests run fine, but in the end, when Django destroys 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 

I checked that nothing was related to the database - why can't Django delete the database?

Note. I am using PostgreSQL

+5
source share
2 answers

TL DR

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

Extended

Django prefers not to run “initialization queries” (presumably things like creating a test database) from the “default” database specified in the DATABASES setting. This is supposed to be a production database, so it’s best for Django not to interfere.

This is why it was shown at the beginning of the tests (ignore my file system):

  /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 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 searches for a database named postgres on the host specified in the DATABASES settings, where it runs queries to create and delete a test database. (Note: Django does not need the postgres database to be explicitly specified in your settings, it just looks for it on any database server in your settings).

It seems that if this postgres database does not exist, Django can create a test database and run tests, but it cannot delete the test database. This may be due to Postgres not allowing you to drop the database that you are currently connected to, however it seems to me that there is no reason Django cannot just drop the test database from the "default" database ( production) specified in the settings. I assume that it uses the default database to create the test database, so it seems that there is no reason why it also cannot delete it.

Regardless, the solution was to simply create this postgres database using a simple SQL statement: CREATE DATABASE postgres . After creating the database, everything worked fine.

+10
source

If the postgres database exists, try adding access to the postgres database in pg_hba.conf. See https://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html

+2
source

All Articles