Reputation: 1180
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',
}
}
Upvotes: 15
Views: 15449
Reputation: 53
I faced the same issue while trying to run some unit tests from pycharm. As previously mentioned, restarting my postgres solved the issue for me.
If someone is using a docker container, the command you will need to use is,
docker-compose restart postgresql-11
postgresql-11
is the name of my PostgreSQL database server name.
Upvotes: 0
Reputation: 840
There is 1 other session using the database.
Have seen this message when the test database was open in pg admin ui and trying to run django tests at same time. Before dropping the db django/psycopg2 checks if there are any sessions active on that db. Closed the connection to the server in pg admin and it just works. Check if you have a connection to the db in shell or ui. Should not need to restart server.
Upvotes: 0
Reputation: 3003
In terminal, run:
ps aux | grep postgres
This will output different processes that are running using postgres, such as:
my_user 5983 0.0 0.0 7325299 4583 ?? Ss 3:15PM 0:00.02 postgres: my_user test_myproj [local] idle
Find the process running test_myproj
, and that process's correspondent ID, which is 5983
in this example. Kill this process by running:
kill -9 5983
Upvotes: 5
Reputation: 79
I found a useful answer here ported by CHUCKCMARTIN
To sum up, you need to run this code to make it available again.
from django.core.management.base import BaseCommand
from django.db import connection
from django.conf import settings
cursor = connection.cursor()
database_name = 'test_<FAILING_DB_NAME>'
cursor.execute(
"SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity "
"WHERE pg_stat_activity.datname = %s AND pid <> pg_backend_pid();", [database_name])
Upvotes: 4
Reputation: 1200
sudo /etc/init.d/postgresql restart
may be a restart will solve this issue
Upvotes: 8