8

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?

1
  • This is such a frustrating problem. Commented Aug 21, 2013 at 0:31

1 Answer 1

9

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'
        },
    }
Sign up to request clarification or add additional context in comments.

1 Comment

great answer, works fine. Small tweak: write the code like this to just change port number, assuming this is after your normal DATABASES setting: if 'test' in sys.argv or 'test_coverage' in sys.argv: DATABASES['default']['PORT'] = 5432

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.