Saturday, January 19, 2013

Upgrading to PostGIS 2.0 on Ubuntu

Like most projects... I felt it paramount to utilize the latest bleeding edge technology to get the job done. Or at least that's how I rationalize playing with something new. Recently I needed to run an analysis on both vector and raster data in PostGIS. This meant upgrading to PostGIS 2.0. Which happened to be absent from the standard Ubuntu repository.

No worries... This is how I got it installed and running in a pre-existing database.:

First upgrade to the latest version of Ubuntu. At the moment it's 12.10 (quantal). You can find your current version by typing lsb_release  -a at the command line.

Backup whatever database you want to upgrade:
pg_dump -Fc -b -v -f fb_19jan2012.backup fb
Purge the old install of postgis
sudo aptitude purge postgis
Install the Ubuntu gis repo from the Ubuntu GIS group and update. At the moment you have to install the unstable version in order for it to work with quantal.
sudo apt-add-repository ppa:ubuntugis/ubuntugis-unstable
sudo aptitude update
Install the new postgis. It should automatically choose the new version. It actually conflicts with the older version... But doesn't seem to complain about it.
sudo aptitude install postgis
Setup a new database. I'm calling it fb2.
sudo su postgres
createdb fb2
psql -d fb2 -f /usr/share/postgresql/9.1/contrib/postgis-2.0/postgis.sql
psql -d fb2 -f /usr/share/postgresql/9.1/contrib/postgis-2.0/spatial_ref_sys.sql
psql -d fb2 -f /usr/share/postgresql/9.1/contrib/postgis-2.0/rtpostgis.sql
psql -d fb2 -f /usr/share/postgresql/9.1/contrib/postgis_comments.sql
psql -d fb2 -c "grant all privileges on database fb2 to some_user"

Now we can stuff the backup into our new database with the handy perl command, postgis_restore.pl, that came with the install. Make sure you get out of the postgres account first.
exit
/usr/share/postgresql/9.1/contrib/postgis-2.0/postgis_restore.pl fb_19jan2012.backup | psql fb2
Happy day! PostGIS 2.0 is installed on our database.

Then I was able to load a digital elevation model raster like this.
raster2pgsql -s 4326 -I -M -C -t 25x25 dem_wa.tif dem_wa > dem_wa.sql
psql fb2 < dem_wa.sql

TADA!
The first raster layer in the new PostGIS 2.0 database. Displayed with qGIS.

No comments: