简体   繁体   中英

Testing Django app with Postgis Backend

I'm attempting to run tests on a GIS Django application running PostGIS as a database backend.

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.

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; . The template1 database is copied when a new database is created, so all new databases will already have the extension installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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