How to Get Started with PostGIS 2.0 on Ubuntu 12.04 (precise)

(minor updates as of 12/18/12) (This installation may generate some errors related to jvm - please see:  http://www.jaloonz.com/2012/08/installing-postgis-201-on-ubuntu-1204.html if you run into any problems.)

This page is targeted at new users and will be written as simply as possible, with plenty of confirmation information. It is also expected to be a temporary page until someone creates a PPA with all associated build information.

Install PostGIS 2.0/2.0.1

Installation requirements can be found at PostGIS Support Matrix

Key libraries to focus on: GEOS, GDAL. Issues related to these two libraries will dictate the method you choose to use to install PostGIS 2.0.

Successful package-based installation of version 2.0.1 using the existing stable UbuntuGIS PPA (tested 2013-04-24)

sudo apt-get install python-software-properties
sudo apt-add-repository ppa:ubuntugis/ppa
sudo apt-get update
sudo apt-get install postgresql-9.1-postgis

Partially-successful Package-Based Installation using the existing Sharpie Oneiric PPA (Personal Package Archive)

The good news: following these steps *will* install PostGIS 2.0 successfully. Thanks, Sharpie!!! : )

sudo apt-get install python-software-properties
sudo apt-add-repository ppa:sharpie/for-science
sudo apt-add-repository ppa:sharpie/postgis-stable
sudo apt-add-repository ppa:ubuntugis/ubuntugis-unstable
sudo apt-get update
sudo apt-get install postgresql-9.1-postgis2 

The bad news: well... it's not really that bad, but this depends on the unstable repository for ubuntugis (gdal 1.9 etc).

Complete installation using a combination of packages and sources

1/4 Install PostgreSQL and other foundations

Install prerequisite packages for PostGIS installation, including PostgreSQL, pgAdminIII, and doc-related libraries using:

sudo apt-get install build-essential postgresql-9.1 postgresql-server-dev-9.1 libxml2-dev proj libjson0-dev xsltproc docbook-xsl docbook-mathml gettext postgresql-contrib-9.1 pgadmin3

2/4 Build libGDAL1.9

This PPA provides up-to-date GDAL support for Precise. Thanks, Olivier!

NOTE: If you are running server, you'll need to do this step first:

sudo apt-get install python-software-properties

Next, for both server and desktop:

sudo apt-add-repository ppa:olivier-berten/geo
sudo apt-get update
sudo apt-get install libgdal-dev

Verify you have the latest version of libGDAL:

$ gdal-config --version
1.9.0

Looks good... what's up with libGEOS?

$ geos-config --version
3.3.2

Crud... not quite 3.3.3. On to the next step.

3/4 Build GEOS 3.3.x

NOTE: This step may be out of date & unnecessary as of 12/18/12.

These are the instructions for building geos 3.3.3, based on instructions found elsewhere on trac.osgeo.org with some additions determined through trial and error:

sudo apt-get install g++ ruby ruby1.8-dev swig swig2.0   ''--- added to other instructions, not installed by default in 12.04 & required for this make
wget http://download.osgeo.org/geos/geos-3.3.3.tar.bz2
tar xvfj geos-3.3.3.tar.bz2
cd geos-3.3.3
./configure --enable-ruby --prefix=/usr

The end of this configure process should look something like this:

Swig: true
Python bindings: false
Ruby bindings: true
PHP bindings: false

If it's so, then move on to finishing the installation:

make
sudo make install
cd ..

To confirm this works, do the following:

$ geos-config --version
3.3.3

4/4 Build PostGIS

wget http://postgis.refractions.net/download/postgis-2.0.6.tar.gz
tar xfvz postgis-2.0.6.tar.gz
cd postgis-2.0.6
./configure --with-gui     <--- not sure if the with-gui is required

The end of the configure processing should leave you with something that looks like this:

  PostGIS is now configured for i686-pc-linux-gnu

 -------------- Compiler Info ------------- 
  C compiler:           gcc -g -O2
  C++ compiler:         g++ -g -O2

 -------------- Dependencies -------------- 
  GEOS config:          /usr/bin/geos-config
  GEOS version:         3.3.3
  GDAL config:          /usr/bin/gdal-config
  GDAL version:         1.9.0
  PostgreSQL config:    /usr/bin/pg_config
  PostgreSQL version:   PostgreSQL 9.1.3
  PROJ4 version:        47
  Libxml2 config:       /usr/bin/xml2-config
  Libxml2 version:      2.7.8
  JSON-C support:       yes
  PostGIS debug level:  0
  Perl:                 /usr/bin/perl

 --------------- Extensions --------------- 
  PostGIS Raster:       enabled
  PostGIS Topology:     enabled

 -------- Documentation Generation -------- 
  xsltproc:             /usr/bin/xsltproc
  xsl style sheets:     /usr/share/xml/docbook/stylesheet/nwalsh
  dblatex:              
  convert:              
  mathml2.dtd:          /usr/share/xml/schema/w3c/mathml/dtd/mathml2.dtd

If this works, you should be ready to ready to finish your installation:

make
sudo make install
sudo ldconfig
sudo make comments-install

Lastly, enable the command-line tools to work from your shell:

sudo ln -sf /usr/share/postgresql-common/pg_wrapper /usr/local/bin/shp2pgsql
sudo ln -sf /usr/share/postgresql-common/pg_wrapper /usr/local/bin/pgsql2shp
sudo ln -sf /usr/share/postgresql-common/pg_wrapper /usr/local/bin/raster2pgsql

Connecting to your new database

There are two primary ways to connect to your new database from the localhost, one is to use the pgadmin tool, the other is to use the psql command line tool. Remote connections via IP are left as exercises for the more advanced user.

In either case, you'll need to connect to the database using your 'postgres' user account, which was created during installation of PostgreSQL. What in the world is the password for this account? Who knows? I haven't been able to figure it out. Luckily, there are instructions on what to do at:  https://help.ubuntu.com/community/PostgreSQL.

An important distinction to make when getting started is that there are both a system user account named 'postgres' and a database account named 'postgres'. These are two separate accounts. The database allows psql-based command line connections from the localhost by system user accounts with the same username as the database account. Make sense? Bottom line is that you won't be able to do much at all until you set up the password for both of these accounts, separately.

Note that some online documentation recommends setting up a system user account called 'postgres' with superuser privileges, but this is not a requirement for getting started.

Next steps - start these steps logged in as your user account: NOTE: you do not have to use the same password for the system and PostgreSQL user accounts that share the same name.

$ sudo passwd postgres
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
$ sudo -s -u postgres
postgres$ psql
psql (9.1.3)
Type "help" for help.

postgres=# \password postgres
Enter new password: 
Enter it again: 
postgres=# \q
postgres$ 

If you're going to want to connect to the PostgreSQL database using your own account (so you don't have to fool around with 'postgres'), you may want to do this:

$USER$ sudo -s -u postgres
postgres$ createuser --superuser $USER     ---- note: createuser is a command line tool to create a PostgreSQL user, not a system account  
postgres$ createdb $USER
postgres$ psql
psql (9.1.3)
Type "help" for help.

postgres=# \password $USER
Enter new password: 
Enter it again: 
postgres=# \q
postgres$ exit
$USER$ psql
psql (9.1.3)
Type "help" for help.

$USER=#                        ---- voila! 

As noted above, this is a great resource:  https://help.ubuntu.com/community/PostgreSQL.

Connecting to your database using pgAdmin III

If you connect through pgadmin, use the following attributes:

  • Name: this is an arbitrary name for your database server
  • Host: localhost
  • Port: 5432 [pre-filled as default]
  • Service: [leave this blank]
  • Maintenance DB: postgres [default]
  • Username: postgres
  • Password: whatever you set in the prior step - note that this is the PostgreSQL password and not the system password
  • Store password: uncheck this (or not, based on your security paranoia level)
  • Colour: whatever suits your fancy
  • Group: Servers [default]

Spatially-enabling a database

With PostgreSQL 9.1, there are two methods to add PostGIS functionality to a database: using extensions, or using enabler scripts.

PostGIS Extension for PostgreSQL

Spatially enabling a database using extensions is a new feature of PostgreSQL 9.1. As usual, there are two methods for doing this, through psql or pgAdmin III:

Adding extensions using psql

Connect to a database using pgAdmin or psql, and run the following commands. For example, if you have created a database called 'GIStest', to add postgis with raster support capability to 'GIStest' do the following:

GIStest=# CREATE EXTENSION postgis;
CREATE EXTENSION
GIStest=#

To add topology support, a second extension can be created on the database:

GIStest=# CREATE EXTENSION postgis_topology;
CREATE EXTENSION
GIStest=#

Adding extensions using pgAdmin III

  • Open pgAdmin III
  • Click on the database symbol for the database to be spatially enabled
  • Right click on the "Extensions" symbol and select "New Extension"
  • In name, type "postgis"
  • Then, click "OK"
  • Repeat, using "postgis_topology"

Enabler Scripts / Template

Enabler scripts can be used to either build a template, or directly spatially enable a database. This method is older than the extension method, but is required if the raster support is not built.

The following example creates a template, which can be re-used for creating multiple spatially-enabled databases. Or if you just want to make one spatially enabled database, you can modify the commands for your needs.

PostGIS:

sudo -u postgres createdb template_postgis
sudo -u postgres psql -d template_postgis -f /usr/share/postgresql/9.1/contrib/postgis-2.0/postgis.sql
sudo -u postgres psql -d template_postgis -f /usr/share/postgresql/9.1/contrib/postgis-2.0/spatial_ref_sys.sql
sudo -u postgres psql -d template_postgis -f /usr/share/postgresql/9.1/contrib/postgis-2.0/postgis_comments.sql

with raster support:

sudo -u postgres psql -d template_postgis -f /usr/share/postgresql/9.1/contrib/postgis-2.0/rtpostgis.sql
sudo -u postgres psql -d template_postgis -f /usr/share/postgresql/9.1/contrib/postgis-2.0/raster_comments.sql

with topology support:

sudo -u postgres psql -d template_postgis -f /usr/share/postgresql/9.1/contrib/postgis-2.0/topology.sql
sudo -u postgres psql -d template_postgis -f /usr/share/postgresql/9.1/contrib/postgis-2.0/topology_comments.sql

See also