Question

I'm using pgbouncer with Django. I've added test_foo database to its config to be able to run tests, because apparently Django can't use a different port for the test DB. Now the test run but at the end, when Django tries to drop the test DB, I receive

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

I suppose that is caused by the open connection stored by pgbouncer. What can I do?

Was it helpful?

Solution

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

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'
        },
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top