简体   繁体   English

使用Postgis后端测试Django应用程序

[英]Testing Django app with Postgis Backend

I'm attempting to run tests on a GIS Django application running PostGIS as a database backend. 我正在尝试在运行PostGIS的GIS Django应用程序上运行测试作为数据库后端。

When I attempt to run tests, I get the following error: 当我尝试运行测试时,出现以下错误:

django.db.utils.ProgrammingError: permission denied to create extension "postgis"
HINT:  Must be superuser to create this extension.

The error makes sense. 这个错误很有意义。 Only admin database users can install extensions since this privilege allows the execution of arbitrary external code. 由于此权限允许执行任意外部代码,因此只有管理数据库用户可以安装扩展。 BUT since the test runner has to re-create the database each time the tests are run, Django's database user can't proceed. 但由于测试运行器每次运行测试时都必须重新创建数据库,因此Django的数据库用户无法继续。

Here is my database configuration. 这是我的数据库配置。

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'my_db',
        'USER': 'my_user',
        'PASSWORD': 'my_crazy_secure_password',
        'HOST': '127.0.0.1',
        'PORT': '',
        'TEST_NAME': 'test_my_db',
    },
}

My solution to this was surprisingly simple, once I figured it out. 一旦我弄明白,我对此的解决方案非常简单。

Connect to the template1 database, and run CREATE EXTENSION IF NOT EXISTS postgis; 连接到template1数据库,并运行CREATE EXTENSION IF NOT EXISTS postgis; . The template1 database is copied when a new database is created, so all new databases will already have the extension installed. 创建新数据库时会复制template1数据库,因此所有新数据库都已安装了扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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