Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get some unit tests working in Django/PostGis. When the test database is formed, it doesn't have the postgis and postgis_topology extensions installed. Is there a way to issue commands to the database before the creation of tables happens? Or could you somehow mess with the postgres template to inherit from? Or something else that wouldn't require me to either hack the django code or not destroy the database? Thanks!

share|improve this question
psql -d template_postgis2 -c "CREATE EXTENSION postgis;"

should create a new database with the PostGIS spatial tables enabled.

Be careful, too, if you are using different SRID's.

Good guidelines are on:

share|improve this answer

Actually @Oberoc, when you define in your settings the database engine, Django is smart enough to create the correct database type when running unit tests.

So you if you have something like:

    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'database_name',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'localhost',
        'PORT': '5432',

and you run your tests, Django will create a database name test_database_name, with PostGIS installed.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.