Installing PostGIS on Ubuntu 10.04 Server

Update Jan 19, 2012

I have an existing django app I have been working on. I want to now move from the sqlite dev database I have been using to a postGIS data. I am on the same machine as I used to preform the steps below so the libraries should already be installed. Also I do not need to rerun the template script.

I first create a database for my app named dev_bb

createdb -T template_postgis dev_bb

Then I source my vertualenv and try to syncdb and get the following error.

File "/home/wilblack/virtualenvs/bb_env/lib/python2.6/site-packages/django/db/backends/postgresql_psycopg2/", line 24, in 
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2

It looks like python cannot see psycopg2 because it is installed outside the virtualenv. Pip to the rescue. A simple

pip install psycopg2

and I’m back in business.


I just got done installing PostGIS 8.4 on my Ubuntu 10.04 Server. apt-get makes this process very simple.

Step 1: Install postgresql, psycopg2, and postgis

PostGIS runs on top of PostgreSQL so you need to install it first. Psycops is how Python and PostGres talk.

sudo apt-get install postgresql
sudo apt-get install python-psycopg2 # only do this is you are NOT use a virtualenv
sudo apt-get install postgresql-8.4-postgis


If you get an error that looks like:

Downloading/unpacking psycopg2==2.4.4 (from -r requirements/project.txt (line 19))
  Running egg_info for package psycopg2
    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 build_ext --pg-config /path/to/pg_config build ...
    or with the pg_config option in 'setup.cfg'.
    Complete output from command python egg_info:
    running egg_info
writing pip-egg-info/psycopg2.egg-info/PKG-INFO
writing top-level names to pip-egg-info/psycopg2.egg-info/top_level.txt
writing dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
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 build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
Command python egg_info failed with error code 1

You just need to install libpq-dev and python-dev (see this blog post from goshawk’s digital nest.)

sudo apt-get install libpq-dev python-dev

If you can’t find psycops2:

Note: when doing this in a virtualenv I was unable to see pyscopg2 because I had already installed it in my main python dist-apackages. So I had to use

export PYTHONPATH=/usr/lib/python2.6/dist-packages/

in order for

python ogrinspect

to work with geodjango.

I used this to add a user with my local user name

sudo -u postgres createuser --superuser $USER

Then I assigned my user a password

sudo -u postgres psql
postgres=# \password USERNAME

Set the postgres user password

Log in to the template1 database off PostgreSQL as the postgres user.

sudo -u postgres psql template1

Once you are in psql set the password with the following line of code

ALTER USER postgres with encrypted password 'your_password';

Step 2: Make a User and Database for your application

I used a user with the same name as the database. To create a postgres user and database using the same name.

createuser myuser createdb myuser

Now the problem is that Django cannot use this account to access the database becuase by default PostgreSQL authentication system is set to “ident”. You need to edit pg_hba.conf to change this. To get around this added a line like

local all all trust

I tried it with the last column as “password” and “md5″ but neither one worked. You can read more about the authentication settings here. With that fixed I was able to run a syncdb once my Django file was properly configured.

Create the “template_postgis” template

From here I got the script to create the template_postgis specific to Ubuntu. I downloaded this to a directory on the same machine as postgres server. I gave the postgres user executable permissions to the script and ran it.


After that, create a geo-spatial database using the postgres account

createdb -T template_postgis deleteme1

Set-up and configuration of pgAdmin III

In order to pgadmin III working on my local machine to me remote postgresql server I had to edit two fiels on the server, /etc/postgresql/8.4/main/postgresql.conf and pg_hba.conf.

In postgresql.conf I had to change the line

#listen_addresses = 'localhost'


listen_addresses = '*'

Then in pg_hba.conf I added the line

host all all trust

to the end of the file, here my is the IP address of the client machine.

11/12/2011 Update

I had some problems creating the postGIS template. Namely when I ran

./ createdb: database creation failed: ERROR: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_AASCII) HINT: Use the same encoding as in the template database, or use template0 as template.

Then based on this blog I recreated the cluster with the UTF-* encoding

pg_dropcluster --stop 8.4 main pg_createcluster --start -e UTF-8 8.4 main

Then to get this running in Django I made an psql user named after my system username

createuser --createdb wilblack

Page 1 of 2 | Next page