bbrame
bbrame

Reputation: 18554

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',
    },
}

Upvotes: 8

Views: 2490

Answers (1)

Joey Wilhelm
Joey Wilhelm

Reputation: 5819

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.

Upvotes: 16

Related Questions