Django测试 - 无法删除并重新创建测试数据库

fia*_*cre 7 python django postgresql postgis

我在Mac OS X上运行Django 1.9,Postgres 9.5.1

我跑的时候 /manage.py test --settings=myproj.settings.local

我明白了:

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.
Run Code Online (Sandbox Code Playgroud)

所以,根据这篇文章,我运行:

SELECT 
    pg_terminate_backend(pid) 
FROM 
    pg_stat_activity 
WHERE 

    pid <> pg_backend_pid()

    AND datname = 'test_myproj'
    ;
Run Code Online (Sandbox Code Playgroud)

继续DROP DATABASE test_myproj并尝试再次运行测试只是为了得到错误DETAIL: There is 1 other session using the database.

作为进程列表,数据库中没有附加任何内容.我终止服务器并重新启动,但运行测试的管理命令仍然给我一个错误,即有另一个会话连接到数据库.

这是一个真正令人头疼的问题,我以前从未见过这个 - 还有其他人吗?

我的设置:

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',
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

sudo /etc/init.d/postgresql restart 可能是重启会解决这个问题


mca*_*tle 5

在终端中,运行:

ps aux | grep postgres

这将输出正在使用 postgres 运行的不同进程,例如:

my_user 5983 0.0 0.0 7325299 4583 ?? Ss 3:15PM 0:00.02 postgres: my_user test_myproj [local] idle

找到正在运行的进程test_myproj,以及该进程的对应 ID,如5983本例所示。通过运行以下命令终止该进程:

kill -9 5983