简体   繁体   English

Django测试:创建测试数据库时出错:权限被拒绝复制数据库“template_postgis”

[英]Django Test: error creating the test database: permission denied to copy database “template_postgis”

I am am working on setting up Django Project for running tests . 我正在设置Django Project来running tests But I am getting below error: 但我得到以下错误:

Got an error creating the test database: permission denied to copy database "template_postgis"

Note : My default application's database is working fine. 注意 :我的默认应用程序的数据库工作正常。 The issue is happening while running tests . issue is happening while running tests

Complete stack trace is as: 完整的堆栈跟踪如下:

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

Below is the DATABASE configuration of setting.py : 以下是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',
        },
    }
}

Any help regarding this? 对此有何帮助? Below are the steps I tried: 以下是我尝试的步骤:

  • Grant create DB access to user: 授予用户创建数据库访问权限:

     ALTER USER my_user CREATEDB; 
  • Grant all privileges to user for test_my_db database: test_my_db数据库授予用户所有权限:

     GRANT ALL PRIVILEGES ON DATABASE test_mozio_db_test to mozio; 

Edit : After fixing above issue, I was also getting error as: 编辑 :修复上述问题后,我也收到错误:

django.db.utils.ProgrammingError: type "geometry" does not exist
LINE 5:     "polygon" geometry(POLYGON,4326) NOT NULL,

Updated my answer to fix both the issues. 更新了我的答案以解决这两个问题。

I finally found how to fix this issue. 我终于找到了解决这个问题的方法。 The problem is that when I created template_postgis , I didn't set it to be a template . 问题是,当我创建template_postgis ,我没有将它设置为template

You can check it via doing: 你可以通过做:

SELECT * FROM pg_database;

You can fix it by setting datistemplate=true for template_postgis by running: 您可以通过运行以下命令为template_postgis设置datistemplate=true来修复它:

update pg_database set datistemplate=true where datname='template_postgis';

After that in case you are getting error related to geometry like: 之后,如果您收到与geometry相关的错误,例如:

django.db.utils.ProgrammingError: type "geometry" does not exist
LINE 5:     "polygon" geometry(POLYGON,4326) NOT NULL,

That is because you need to add extension postgix with database. 那是因为你需要在数据库中添加扩展名postgix In order to fix this, add postgis to template_postgis like: 为了解决这个问题,请将postgis添加到template_postgis如:

psql -d psql -d template_postgis -c 'create extension postgis;'

Note : You must be superuser to create this extension. 注意 :您必须是超级用户才能创建此扩展。

Install packages first correctly. 首先正确安装包。

sudo apt-get update
sudo apt-get install python-pip python-dev libpq-dev postgresql postgresql-contrib

During installation postgres user is created automatically. 在安装过程中,会自动创建postgres用户。

 sudo su - postgres

You should now be in a shell session for the postgres user. 您现在应该在postgres用户的shell会话中。 Log into a Postgres session by typing: 键入以下内容登录Postgres会话:

 psql

 CREATE DATABASE myproject;

In your settings.py. 在您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM