【问题标题】:Django Test -- Unable to drop and recreate test databaseDjango Test - 无法删除和重新创建测试数据库
【发布时间】:2016-12-24 06:19:03
【问题描述】:

我在 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.

所以,按照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',
    }
}

【问题讨论】:

  • 我没有解决方案,但您的问题似乎是,在测试的情况下,两个数据库都将被命名为 test_myproj - 默认情况下它有效,而不是用户定位,检测到此数据库作为现有的。现有会话是当前运行测试的 django 仍在使用的会话。脚本死后,不再有会话;)
  • 看看那些docs
  • @dahrens,谢谢。稍微修正了设置文件,就像一个魅力。

标签: python django postgresql postgis


【解决方案1】:

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

【讨论】:

  • 我在 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 给出同样的错误:Destroying old test database for alias 'userlocation'... Got an error recreating the test database: database "test_og_myproj" does not exist
  • “我杀死了服务器并重新启动,但是运行测试的管理命令仍然给我一个错误,即数据库附加了另一个会话。” -- 我试过这个
  • @cs_stackX 告诉我们“这不是正确的答案”并没有帮助。这对我来说似乎是一种解决方法,但至少它可以解决问题。如果您有更永久的解决方案发布它会非常有帮助!
【解决方案2】:

我找到了一个有用的答案hereCHUCKCMARTIN 移植

综上所述,您需要运行此代码才能使其再次可用。

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])

【讨论】:

    【解决方案3】:

    在终端中,运行:

    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

    【讨论】:

      【解决方案4】:

      还有 1 个会话正在使用该数据库。

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

      【讨论】:

      • 在此之后如果测试运行正常但显示 django.db.utils.OperationalError: cannot drop the current open database: then Django 需要连接到 postgres 数据库并且需要在 pg_hba 中定义.conf.
      【解决方案5】:

      我在尝试从 pycharm 运行一些单元测试时遇到了同样的问题。如前所述,重新启动我的 postgres 为我解决了这个问题。

      如果有人在使用 docker 容器,您需要使用的命令是,

      docker-compose restart postgresql-11
      

      postgresql-11 是我的 PostgreSQL 数据库服务器名称。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-24
        • 1970-01-01
        • 2014-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-20
        • 1970-01-01
        相关资源
        最近更新 更多