Pages

Wednesday, January 14, 2015

How to install Postgis on Ubuntu 12.04

If you are trying to install postgis extensions for postgresql on Ubuntu 12.04 LTS, found lot of instructions on stackexchange and other sites, but do not like them, because you do not want to manually download, build and do staff hardcore linuxoids do, then this post might be for you.
It's about Ubuntu 12.04 (precise), postgresql 9.1 and postgis 2.1

Versions

I think main problem with other instructions is that they mess up with versions. Some try to install new postgresql (9.3 for example), which is not in Ubuntu 12.04 repository, some use wrong postgis versions or add wrong repository. Others just like me have two different versions of postgresql installed and just like me have no idea which tools are working when they type psql.
So remember:

  1. Latest version of postgresql for Ubuntu 12.04 currently is postgresql-9.1. If you are using postgresql-8.4, then first upgrade to 9.1 because not sure you will find postgis for 8.4. 
  2. If you need upgrade, don't install both versions at same time. Of course if you are not postgresql and Linux pro. But i guess you are not - you are reading this ;) . Yes you can have two different versions running at same time - this is feature, not a bug. Check /usr/lib/postgresql/ directory. If you see two versions in that directory try to clean everything and start over and install only new 9.1 version. You'll need to dump everything from db to file with pg_dumpall > all_dbs.sql before removing them. Or if this is not possible try to google howto upgrade postgis from 8.4 to 9.1 :)
  3. Currently for postgresql 9.1 there's postgis 2.1

So we need Ubuntu 12.04 -> postgresql 9.1 -> postgis 2.1
That's all about versions. If everything is ready then continue:

Instructions

Here's what you need to do after setting up postgresql 9.1 on your server:

1) add repository
sudo add-apt-repository ppa:ubuntugis/ppa
2) refresh package list
sudo apt-get update

3) install packages
sudo apt-get install postgis postgresql-9.1-postgis-2.1 postgresql-9.1-postgis-scripts postgresql-contrib-9.1

4) login to postgis
sudo -u postgres psql
You can switch to postgres user first and then open psql. Use whatever way you like, just get in to psql

5) switch DB
Switch to db you want to make GIS db. In psql type:
\c my_db_name
We will install extensions in this db.

6) Install extensions
Run these 3 scripts in this order.
CREATE EXTENSION postgis;CREATE EXTENSION postgis_topology;CREATE EXTENSION fuzzystrmatch;

These will install postgis 2.1 extension for selected db which includes new schema named topology, couple of views, functions, triggers and very important table named spatial_ref_sys. To see these, try installing this extensions on new empty database. 
I missed one script here - tiger geocoder - which is used for geocoding and you can read about it here. I needed postgis only for Geoserver and did not installed this extension. If you need that too, run following script but note that it will create lot of additional tables:
CREATE EXTENSION postgis_tiger_geocoder;
As I remember fuzzystrmatch extension is required for Tiger Geocoder. Though not sure 100%. That's all.
Now you can use GIS systems like Geoserver with your DB or write your own GIS applications. 
Good luck!

P.S.


  • Here's more about repository mentioned in the first step: UbuntuGis (stable). Select precise in drop-down and filter to see versions of packages n this repository. 
  • Advise: Do not store your spatial data (coordinates for points, lines and other shapes) as plain data types. Always use postgis type Geometry which can store multiple points or line or polygons and their SRID in one field. Converting from plain data fields to Geometry type is not easy because of those projections transformations.