Installing Django, PostgreSQL 9, PostGIS in Arch Linux Django 04.07.2014

postgis-logo.png This post is small how-to install and setup bundle of GeoDjango, PostgreSQL 9, PostGIS in Arch Linux.

Python VirtualEnv

VirtualEnv is a Python tool used to create isolated environments for Python in which you can install packages without interfering with the other virtualenvs nor with the system Python's packages.

Also I'll use VirtualEnvWrapper for simplifying create, activate and remove virtualenvs.

yaourt -S python2 python2-pip python2-virtualenv python-virtualenvwrapper

VirtualEnvWrapper requires some initial setup

# vim ~/.zshrc

export WORKON_HOME=$HOME/.virtualenvs
source /usr/bin/virtualenvwrapper.sh

Re-open your console and create the WORKON_HOME folder

mkdir $WORKON_HOME

Create the virtualenv

mkvirtualenv -p /usr/bin/python2.7 myenv

Activate the virtualenv

workon myenv

Install some package inside the virtualenv

(myenv)$ pip install django pyproj

Install PostgreSQL, GDAL, GEOS, PostGIS

GDAL (Geospatial Data Abstraction Library) is a library for reading and writing raster geospatial data formats.
GEOS is a C++ library for performing geometric operations, and is the default internal geometry representation used by GeoDjango
PostGIS is a spatial database extender for PostgreSQL object-relational database. It adds support for geographic objects allowing location queries to be run in SQL.

Install PostgreSQL, PostGIS

sudo pacman -Sy postgresql postgresql-libs postgis

Set password for postgres user

sudo passwd postgres

Init DB

su - postgres -c "initdb --locale en_US.UTF-8 -D '/var/lib/postgres/data'"

Run PostgreSQL

# in current session
systemctl start postgresql

# after reboot
systemctl enable postgresql

Install GDAL, GEOS

yaourt -S gdal geos

Setup and test PostGIS, GeoDjango

Test GEOS, GDAL, PyProj

# python manage.py shell

#GDAL

>>> from django.contrib.gis import gdal
>>> gdal.HAS_GDAL
True

#GEOS
>>> from django.contrib.gis import geos
>>> geos.geos_version()

#Proj
>>> import pyproj
>>> pyproj.__version__
>>> pyproj.test()

Create DB (for PostgreSQL 9.1+)

# su - postgres

createdb dbname
psql dbname

> CREATE EXTENSION postgis;
> CREATE EXTENSION postgis_topology;

Configure db in settings.py and add django.contrib.gis to INSTALLED_APPS

DATABASES = {
    'default': {
         'ENGINE': 'django.contrib.gis.db.backends.postgis',
         'NAME': 'dbname',
         'USER': 'user',
         'PASSWORD': 'password',
     }
}

INSTALLED_APPS = (
    'django.contrib.admin',
    ...
    'django.contrib.gis',
    ...
)