Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Test -- Unable to drop and recreate test database

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',
    }
}
like image 829
fiacre Avatar asked Aug 17 '16 14:08

fiacre


1 Answers

sudo /etc/init.d/postgresql restart may be a restart will solve this issue

like image 108
Aamish Baloch Avatar answered Sep 28 '22 07:09

Aamish Baloch