I am running Django 1.9, Postgres 9.5.1 on Mac OS X
When I run /manage.py test --settings=myproj.settings.local
I get :
Creating test database for alias 'default'...
Creating test database for alias 'userlocation'...
Got an error creating the test database: database "test_myproj" already exists
Type 'yes' if you would like to try deleting the test database 'test_myproj', or 'no' to cancel: yes
Destroying old test database for alias 'userlocation'...
Got an error recreating the test database: database "test_myproj" is being accessed by other users
DETAIL: There is 1 other session using the database.
So, as per This post, I run:
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
pid <> pg_backend_pid()
AND datname = 'test_myproj'
;
The proceed to DROP DATABASE test_myproj and try to run tests again only to get the error DETAIL: There is 1 other session using the database.
Looking as the process list, nothing is attached to the database. I kill the server and restart, but the management command to run tests still gives me the error that there is another session attached to the database.
This is a real head scratcher, I have never seen this before -- has anyone else?
My settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproj',
'USER': 'myuser',
'PASSWORD': 'mypass',
'HOST': 'localhost',
'PORT': '',
},
'userlocation': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'og_myproj',
'USER': 'og_myuser',
'PASSWORD': 'mypasswd',
'HOST': 'localhost',
'PORT': '5432',
}
}
test_myproj- for default it works, and than for userlocation this database is detected as existing. The existing session is the one that is still used by your current django running the tests. And after the script dies there is no session anymore ;)