2016-08-17 76 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。 –