Django cannot delete test database via pgbouncer

I am using pgbouncer with Django. I added the test_foo database to its configuration in order to be able to run tests because, apparently, Django cannot use a different port for the test database. Now a test run, but in the end, when Django tries to reset the test DB, I get

 django.db.utils.DatabaseError: database "test_foo" is being accessed by other users DETAIL: There are 1 other session(s) using the database. 

I assume this is caused by the open connection stored by pgbouncer. What can I do?

+6
source share
1 answer

This is not an ideal solution, but it does the trick. You can force Django to use different database settings when performing unit tests by adding .py to your settings:

 if 'test' in sys.argv or 'test_coverage' in sys.argv: # Use 5432 as db port (avoid going through pgbouncer, can't delete test DB). DATABASES = { 'default': { 'ENGINE': 'django.contrib.gis.db.backends.postgis', 'NAME': 'xxx', 'USER': 'xxx', 'PASSWORD': 'xxx', 'HOST': '', 'PORT': '5432' }, } 
+6
source

Source: https://habr.com/ru/post/901004/


All Articles