Setting up GeoDjango on Ubuntu

This is how I setup GeoDjango development environment on a clean install of Ubuntu.

A good place to start would be to take a quick look at the docs but don’t spend to much there. Just follow on 🙂

Hopefully you already have Django setup on your machine. If not you’ll have to do so and can follow my Django setup guide here

In a nutshell we will:

  1. Install a Geospatial Library (PostGIS)
  2. Setup a Geospatial database template (Using a script)
  3. Create a database to test

First step is to install a Geospatial Library. We will use PostGIS.

Run the following line in your terminal.

sudo apt-get install binutils gdal-bin libproj-dev postgresql-9.1-postgis postgresql-server-dev-9.1 python-psycopg2

Now we have installed PostGIS we need to setup a template database. This is where I started getting confused following the GeoDjango setup guide.

Next lets enter the postgres user shell like this:

sudo su postgres

In your web browser go download

(I had to then go to the script >> right click on it >> Properties >> Permissions >> And tick  ‘Allow executing as program’ )

Now cd to the location where you have stored the script for example:

cd ~/Downloads

and execute the script


You’ll see a lot happening on the scree. This is normal.

If everything has gone OK up until now then we should be able to create a new database using the spatial db template the script created for us.

Let us try that then. Type in:

createdb -T template_postgis geodjango

If it worked then the terminal should just go to the next line without saying anything. Let us confirm by listing the databases. You can open Pgadmin3 to see if your database is listed there or you can type in:

psql -l

If you see ‘geodjango’ database there then all is well and you have finished setting up GeoDjango on your Ubuntu machine.

Extra Information

*You can call your databases anything you want but you will have to create them this way if you want to have the geodjango functionality.

*To create your DB with a GUI like pgadmin just open your pgadmin3 and create a DB like you normally would. But before you click OK on the New Database form make sure to first click on the Definition tab. Here you must select the correct template to base your DB on which will be tempalte_postgis.

You might run into this error An error has occurred:

ERROR:  source database “template_postgis” is being accessed by other users

DETAIL:  There are 1 other session(s) using the database.

This is because your template is connected in pgadmin3.  All you need to do is right click on the template_postgis db in your DB list on the left and click disconnect.

*Add django.contrib.gis to INSTALLED_APPS
Like other Django contrib applications, you will only need to add django.contrib.gis to INSTALLED_APPS in your settings. This is the so that gis templates can be located — if not done, then features such as the geographic admin or KML sitemaps will not function properly.

From this point onwards you should be able to run through the geodjango tutorial as your spatial database has been setup.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s