python - Django 测试 : error creating the test database: permission denied to copy database "template_postgis"

标签 python django postgis django-testing geodjango

我正在为 运行测试 设置 Django 项目。但是我遇到以下错误:

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

注意:我的默认应用程序数据库运行良好。 运行测试时出现问题

完整的堆栈跟踪如下:

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;'

注意:您必须是 super 用户才能创建此扩展。

关于python - Django 测试 : error creating the test database: permission denied to copy database "template_postgis",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39556719/

相关文章:

django - 如何使用 django 以 ascii 编码将图像保存在 postgres bytea 字段中

python - 您如何在开发环境中配置 Sentry raven 客户端以不发送异常并且仍然​​可以工作?

django - GeoDjango 中新 GeometryField 的通用输入元素

python - 从随机矩阵生成样本

python - django save() 禁止,以防止因未保存相关对象而导致数据丢失

python - struct.pack 的任何替代品?

sql - 需要 SQL 优化(也许 DISTINCT ON 是原因?)

sql-server - 那里有什么好的空间数据库教程吗?

python - 从具有许多键的字典列表创建 Pandas 时间序列

python - 获取索引范围内行的最大值