Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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',
    },
}
like image 846
bbrame Avatar asked Feb 04 '16 18:02

bbrame


1 Answers

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.

like image 170
Joey Wilhelm Avatar answered Oct 04 '22 09:10

Joey Wilhelm