![](/img/trans.png)
[英]django test app error - Got an error creating the test database: permission denied to create database
[英]Django Test: error creating the test database: permission denied to copy database “template_postgis”
我正在設置Django Project來running tests
。 但我得到以下錯誤:
Got an error creating the test database: permission denied to copy database "template_postgis"
注意 :我的默認應用程序的數據庫工作正常。 issue is happening while running tests
。
完整的堆棧跟蹤如下:
moin@moin-pc:~/workspace/mozio$ python manage.py test --verbosity=3
nosetests --verbosity=3
nose.config: INFO: Ignoring files matching ['^\\.', '^_', '^setup\\.py$']
Creating test database for alias 'default' ('test_my_db')...
Got an error creating the test database: permission denied to copy database "template_postgis"
Type 'yes' if you would like to try deleting the test database 'test_mozio_db_test', or 'no' to cancel: yes
Destroying old test database 'default'...
Got an error recreating the test database: must be owner of database test_mozio_db_test
以下是setting.py
的DATABASE配置:
POSTGIS_VERSION = (2, 2, 2)
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'my_db',
'USER': 'my_user',
'PASSWORD': 'my_pass',
'HOST': '<HOST_IP',
'PORT': '',
'TEST': {
'NAME': 'test_my_db',
},
}
}
對此有何幫助? 以下是我嘗試的步驟:
授予用戶創建數據庫訪問權限:
ALTER USER my_user CREATEDB;
為test_my_db
數據庫授予用戶所有權限:
GRANT ALL PRIVILEGES ON DATABASE test_mozio_db_test to mozio;
編輯 :修復上述問題后,我也收到錯誤:
django.db.utils.ProgrammingError: type "geometry" does not exist
LINE 5: "polygon" geometry(POLYGON,4326) NOT NULL,
更新了我的答案以解決這兩個問題。
我終於找到了解決這個問題的方法。 問題是,當我創建template_postgis
,我沒有將它設置為template
。
你可以通過做:
SELECT * FROM pg_database;
您可以通過運行以下命令為template_postgis
設置datistemplate=true
來修復它:
update pg_database set datistemplate=true where datname='template_postgis';
之后,如果您收到與geometry
相關的錯誤,例如:
django.db.utils.ProgrammingError: type "geometry" does not exist
LINE 5: "polygon" geometry(POLYGON,4326) NOT NULL,
那是因為你需要在數據庫中添加擴展名postgix
。 為了解決這個問題,請將postgis
添加到template_postgis
如:
psql -d psql -d template_postgis -c 'create extension postgis;'
注意 :您必須是超級用戶才能創建此擴展。
首先正確安裝包。
sudo apt-get update
sudo apt-get install python-pip python-dev libpq-dev postgresql postgresql-contrib
在安裝過程中,會自動創建postgres用戶。
sudo su - postgres
您現在應該在postgres用戶的shell會話中。 鍵入以下內容登錄Postgres會話:
psql
CREATE DATABASE myproject;
在您的settings.py中。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'postgres',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.