Django Test -- Unable to drop and recreate test database
<p>I am running Django 1.9, Postgres 9.5.1 on Mac OS X</p>

<p>When I run <code>/manage.py test --settings=myproj.settings.local</code></p>

<p>I get :</p>

<pre><code>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.
</code></pre>

<p>So, as per <a href="https://stackoverflow.com/questions/5108876/kill-a-postgresql-session-connection">This post</a>, I run:</p>

<pre><code>SELECT 
    pg_terminate_backend(pid) 
FROM 
    pg_stat_activity 
WHERE 

    pid &lt;&gt; pg_backend_pid()

    AND datname = 'test_myproj'
    ;
</code></pre>

<p>The proceed to <code>DROP DATABASE test_myproj</code> and try to run tests again only to get the error <code>DETAIL:  There is 1 other session using the database.</code></p>

<p>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.</p>

<p>This is a real head scratcher, I have never seen this before -- has anyone else?</p>

<p>My settings:</p>

<pre><code>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',
    }
}
</code></pre>