DevOps Zone is brought to you in partnership with:

I work at Tangent Labs, a digital agency, writing applications in python and django. I spend most of my free time hacking. I run commandlinefu.com and am the author of the e-commerce framework django-oscar amongst other things. I used to be a mathematician; I have a PhD in Mathematics from the University of Nottingham and an associated interest in cryptic crosswords, chess and playing devil's advocate. David is a DZone MVB and is not an employee of DZone and has posted 27 posts at DZone. You can read more from them at their website. View Full User Profile

How to Install PostGIS and GeoDjango on Ubuntu

11.26.2013
| 2536 views |
  • submit to reddit

Despite its extensive documentation, getting GeoDjango installed and configured can be a pain. Here are my notes for future reference:

Installation on Ubuntu 12.04

First, ensure your system locale is UTF8 as PostgreSQL uses it to determine its default encoding during installation:

$ export LANGUAGE="en_US.UTF-8"
$ export LANG="en_US.UTF-8"
$ export LC_ALL="en_US.UTF-8"

Now install dependencies:

$ sudo apt-get update
$ sudo apt-get install postgresql-server-dev-9.1 postgresql-9.1-postgis

PostgreSQL should now be installed and running with UTF8 encodings. Verify this with:

$ sudo -u postgres psql -l
                                     List of databases
       Name       |  Owner   | Encoding |   Collate   |    Ctype    |
------------------+----------+----------+-------------+-------------+-...
 postgres         | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0        | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
                  |          |          |             |             |
 template1        | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
                  |          |          |             |             |

Now create a spatial database. The above commands will have installed PostGIS 1.5.3 hence, according to Django's docs, we need to download and install a shell script that executes the appropriate commands:

$ wget https://docs.djangoproject.com/en/dev/_downloads/create_template_postgis-1.5.sh
$ chmod +x create_template_postgis-1.5.sh

and run it as a privileged user:

$ sudo -u postgres ./create_template_postgis-1.5.sh

If this is successful, a deluge of SQL output will echo to the console. This is normal. PostGIS is now correctly installed and we have a template database we can use to create a database for our GeoDjango project.

$ sudo -u postgres psql
psql (9.1.9)
Type "help" for help.

postgres=# CREATE DATABASE sample_db TEMPLATE template_postgis;
CREATE DATABASE
postgres=# CREATE ROLE sameple_role WITH PASSWORD 'sample_password' LOGIN;
CREATE ROLE

This is the tricky bit over: you can now pip install psycopg2 and you're basically done.

Common errors

If your system locale is not UTF8 when attempting to create a spatial database, you'll see something like this:

$ sudo -u postgres ./create_template_postgis-debian.sh
createdb: database creation failed: ERROR: encoding UTF8 does not match locale en_US
DETAIL:  The chosen LC_CTYPE setting requires encoding LATIN1
FATAL:  database "template_postgis" does not exist

If PostgreSQL is not installed when trying to install psycopg2, you see something that ends with this:

warning: manifest_maker: standard file '-c' not found

Error: pg_config executable not found.

Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:

    python setup.py build_ext --pg-config /path/to/pg_config build ...

or with the pg_config option in 'setup.cfg'.

I'll add more error symptoms when I see them in the wild.


Learn more about PostGIS - Download the PostGIS Refcard. 

Published at DZone with permission of David Winterbottom, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)