![](/img/trans.png)
[英]django test app error - Got an error creating the test database: permission denied to create database
[英]Django Test Error “Permission denied to create database” - Using Heroku Postgres
我正在嘗試使用Django App目錄中的tests.py文件(mysite / polls / tests.py)進行簡單的Django測試,但每次運行'python manage.py test polls'時,我都會收到錯誤消息:
C:\Python27\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 3.0\helpers\pycharm\django_test_manage.py" test polls "C:\Users\<myname>\PycharmProjects\mysite"
Testing started at 8:40 PM ...
Creating test database for alias 'default'...
Type 'yes' if you would like to try deleting the test database 'test_<database>', or 'no' to cancel: Got an error creating the test database: permission denied to create database
根據我的閱讀,顯然Heroku PG使用共享數據庫,因此我沒有創建/銷毀數據庫的權限,這是測試所必需的。 這有明顯的解決方案嗎? 我仍然在我的本地驅動器上開發,所以任何變通辦法都會受到贊賞。 我知道測試是編程的一個重要部分,所以我希望能夠盡快實現測試方法。
我正在嘗試使用TestCase
django類進行測試。
我在用什么:
1)Heroku Postgres愛好開發計划
2)Postgres 9.3.3
3)Python 2.7.6
4)Django 1.6.1
編輯:所以在做了一些研究之后,我發現我可以在settings.py中覆蓋我的DATABASES
dict變量來使用SQLite在本地測試(當'test'是shell中的參數時),但我仍然更喜歡PostgreSQL實現,從我讀到的,PostgreSQL更嚴格(我是粉絲)。
對於任何對我發現的半解決方案感興趣的人(由Stackoverflow的另一個成員提供):
if 'test' in sys.argv:
DATABASES['default'] = {'ENGINE': 'django.db.backends.sqlite3'}
不要忘記導入sys
。
您沒有理由需要使用測試創建數據庫。 相反,如果未定義DATABASE_URL環境變量,請更改測試以訪問本地數據庫。 這是我在Node.js中所做的,我有本地測試和開發數據庫,以及Heroku提供的生產數據庫:
if (typeof(process.env.DATABASE_URL) !== 'undefined') {
dbUrl = url.parse(process.env.DATABASE_URL);
}
else if (process.env.NODE_ENV === 'test') {
dbUrl = url.parse('tcp://postgres:postgres@127.0.0.1:5432/test');
}
else {
dbUrl = url.parse('tcp://postgres:postgres@127.0.0.1:5432/db');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.