2013-01-06 26 views
104

當我嘗試使用命令測試任何應用程序時(當我嘗試使用使用此命令的結構部署myproject時,我注意到它):django測試應用程序錯誤 - 創建測試數據庫時出現錯誤:拒絕創建數據庫的權限

python manage.py test appname 

我得到這個錯誤:

Creating test database for alias 'default'... 
Got an error creating the test database: permission denied to create database 

Type 'yes' if you would like to try deleting the test database 'test_finance', or 'no' to cancel 

syncdb命令似乎工作。在settings.py我的數據庫設置:

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 
     'NAME': 'finance',      # Or path to database file if using sqlite3. 
     'USER': 'django',      # Not used with sqlite3. 
     'PASSWORD': 'mydb123',     # Not used with sqlite3. 
     'HOST': '127.0.0.1',      # Set to empty string for localhost. Not used with sqlite3. 
     'PORT': '',      # Set to empty string for default. Not used with sqlite3. 
    } 
} 

回答

236

當Django運行測試套件,它會創建一個新的數據庫,你的情況test_finance。具有用戶名django的postgres用戶沒有創建數據庫的權限,因此是錯誤消息。

當您運行syncdb時,Django不會嘗試創建財務數據庫,因此您不會收到任何錯誤。

您可以通過在postgress shell中以超級用戶身份運行以下命令(hat tips to this stack overflow answer),將createdb權限添加到django用戶。

=> ALTER USER django CREATEDB; 

注:ALTER USER <username> CREATEDB;命令需要使用的用戶名到數據庫用戶匹配Django配置文件。在這種情況下,原始海報,用戶爲django以上答案。

+1

抱歉,我不能早期測試。感謝它的工作。 – Andrius

+1

儘管OP使用了postgresql,但如果您使用的是mysql,則會出現相同的錯誤。您可以使用以下命令修復它:mysql => GRANT ALL ON * *。\ * to django @ localhost;我最初試圖只授予創建...但後來不能選擇或刪除創建的數據庫。這基本上使你的用戶成爲超級用戶,所以要小心。 – mightypile

+1

我嘗試了一下,發現最小的全局特權是 - 數據:SELECT,INSERT,UPDATE,DELETE,結構:CREATE,ALTER,INDEX,DROP,Admin:REFERENCES。不是超級用戶,但仍然非常強大,所以請謹慎使用。 – Scott

6

我發現你的問題有趣的解決方案。
實際上,對於MySQL,您可以授予對於不存在的數據庫的權限。
所以,你可以在你的設置中添加名稱「test_finance」爲您的測試數據庫:

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 
     'NAME': 'finance',      # Or path to database file if using sqlite3. 
     'USER': 'django',      # Not used with sqlite3. 
     'PASSWORD': 'mydb123',     # Not used with sqlite3. 
     'HOST': '127.0.0.1',      # Set to empty string for localhost. Not used with sqlite3. 
     'PORT': '',      # Set to empty string for default. Not used with sqlite3. 
     'TEST': { 
      'NAME': 'test_finance', 
     }, 
    } 
} 

啓動mysql shell作爲根用戶:

mysql -u root -p 

現在授予所有特權,這不存在在MySQL中的數據庫:

GRANT ALL PRIVILEGES ON test_finance.* TO 'django'@'localhost'; 

現在Django將開始測試沒有任何問題。

1

如果數據庫是mysql那麼這兩個更改將完成任務。

1.Open mysite的/ mysite的/ settings.py

你的數據庫設置應該有一個附加的測試塊作爲所示與projectname_test

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.mysql', 
     'NAME': 'myproject', 
     'USER': 'chandan', 
     'PASSWORD': 'root', 
     'HOST': 'localhost', 
     'PORT': '3306', 
     'TEST': { 
      'NAME': 'myproject_test', 
     }, 
    } 
} 

2,型號以下命令使用MySQL命令提示MySQL工作臺給所有的privilages在設置中指定的用戶。py

GRANT ALL PRIVILEGES ON myproject_test.* TO 'chandan'@'localhost'; 

現在您可以運行python manage.py test polls

+0

這個問題顯然使用Postgres作爲DBMS。 * projectname_test *涉及什麼? –

+0

@ code-kobold 7,是的,但我可以在mysql中看到同樣的錯誤。值projectname_test是指項目名稱ex.in我的答案是myproject。 – Chandan

相關問題