2016-08-17 75 views
4

我运行的Django 1.9,Postgres的9.5.1在Mac OS XDjango的测试 - 无法删除并重新测试数据库

当我运行/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. 

所以,按照This post,我跑:

SELECT 
    pg_terminate_backend(pid) 
FROM 
    pg_stat_activity 
WHERE 

    pid <> pg_backend_pid() 

    AND datname = 'test_myproj' 
    ; 

将继续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', 
    } 
} 
+0

我没有解决方案,但你的问题似乎是,两个数据库在测试的情况下将被命名为'test_myproj' - 默认情况下它工作,并且比userlocation这个数据库被检测为现有的。现有会话仍然是当前运行测试的django使用的会话。而脚本死后没有会话了:) – dahrens

+1

看看那些[文档](https://docs.djangoproject.com/en/1.10/topics/testing/advanced/#controlling-creation-order-for -test-databases) – dahrens

+0

@dahrens,谢谢。修正了一些设置文件,并像魅力一样工作。 – fiacre

回答

5

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

+0

我在Mac上,'$ pg_ctl -D/usr/local/var/postgres -l /usr/local/var/postgres/server.log restart'然后是'./manage.py test --settings = ohmgear。 settings.local --no-input'给出同样的错误:'销毁别名'userlocation'的旧测试数据库... 重新创建测试数据库时出错:数据库“test_og_myproj”不存在' – fiacre

+0

“我杀死了服务器并重新启动,但运行测试的管理命令仍然会给我带来另一个会话连接到数据库的错误。“ - 我试过这个 – fiacre

0

详细信息:有使用数据库1次其他会议。

当测试数据库在pg admin ui中打开并尝试在同一时间运行django测试时看到了此消息。在删除db django/psycopg2之前,检查是否有该数据库上的任何活动会话。在pg admin中关闭了与服务器的连接,它正常工作。检查你是否有shell或ui中的数据库连接。不应该需要重新启动服务器。

+0

之后如果测试运行正常,但是这显示出django.db.utils.OperationalError:不能删除当前打开的数据库:然后Django需要连接到postgres数据库,并且需要在pg_hba中定义。 CONF。 –