Monday, October 15, 2012

POSTGIS Spacial Database Installation on a Mac OS X Lion System


Background

Presently I am working on a hobby project of mine where I want to develop a web application with Ruby on Rails which shall visualize data on maps. I essence I want to build a GIS application. After reading a couple of sources in the internet, it became obvious that it would be best to have a spacially enabled database - meaning a relational database with an extension so it can process queries for spacially arranged data. An example would be a simple query where you want to know the gas stations in a certain radius around your current location.
The resulting architecture I want to use is the following:

  • The system database I want to use will be PostgreSQL
  • To spacially enable the database, I want to use the PostGIS addon for PostgreSQL
  • To enable the communication between Rails and the PostGIS server, the activerecord-postgis-adapter is required
  • To be able to write "geospacial" ruby code, the GeoRuby gem is required
  • Furthermore, I want to do the map visualization with OpenLayers to be independent of commercial map services. 

Step 1: Installing the PostgreSQL

So far, I used the Postgres.app (download on postgresapp.com) for my development (to be honest, my development is currently at chapter 9 of Michael Hartl's excellent tutorial of a micropost app). But I found out that Postgres.app is insufficient for the installation of the PostGIS enhancement, but a full PostgreSQL installation is required (see README.postgis in the PostGIS distribution files).
The latest PostgreSQL EnterpriseDB installation files for the common operation systems are available at enterprisedb.com.

PostgreSQL Installation Problem

During the installation I experienced a problem which has been seen by other users as well (see for instance this discussion thread): the installation starts without problems, runs all through, but hangs in the end with a message

  • "Loading additional SQL modules"

You can only click on "cancel" (what I did once...) which rolls back the whole installation. The discussion above gave the helpful hint:

  • hard stop the installation via the Activity Monitor
  • stop any processes called "postgres" running in the background
  • restart the installation once again with the same directories

This procedure makes the installation run once again and this time ending without any problems.

Step 2: Installation of PostGIS

I found a good description "installing PostGIS on Mac OS X and Ubuntu" by "juniorz" which I followed. Still, I ran into a couple of issues and obstacles which I will describe here.

First installation step ($ brew install postgis)


Firstly, I encountered the following error:
==> ./configure --prefix=/usr/local/Cellar/proj/4.8.0
==> make install
Error: The linking step did not complete successfully
The formula built, but is not symlinked into /usr/local
You can try again using `brew link proj'
The advice to run "brew link proj" actually did not fix the issue, I got another error message:
$ brew link proj
Linking /usr/local/Cellar/proj/4.8.0...
Error: Could not symlink file: /usr/local/Cellar/proj/4.8.0/bin/proj
Target /usr/local/bin/proj already exists. You may need to delete it.
To force the link and delete this file, do:
  brew link -f formula_name
What helped in the end was a forced deletion of proj:
$ brew link -f proj
A second error appeared for the GEOS library:
==> ./configure --prefix=/usr/local/Cellar/geos/3.3.5
==> make install
Error: The linking step did not complete successfully
The formula built, but is not symlinked into /usr/local
You can try again using `brew link geos'
Here the situation was a bit more difficult than for "proj". Neither worked "$ brew link geos" nor "$ brew link -f geos" as the system refused to delete it due to missing privileges. As well "sudo brew link -f geos" failed.
What helped here in the end was to use Finder and to manually delete the directories "geos" located in
/usr/local/Cellar/
/usr/local/include/
 Another issue appeared, when the system tried to download the required json library from
http://oss.metaparadigm.com/json-c/json-c-0.9.tar.gz
here the download process repeatedly stopped at a couple of downloaded percent. I was able to fix this by downloading the file json-c-0.9.tar.gz from github and to move it to the Homebrew installation directory
/Library/Caches/Howbrew/
With these little tricks I was able (Hooorray!) to install PostGIS.


Second installation step (initdb /usr/local/var/postgres)


No Issues.


Third installation step (pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start)


When performing the command above, I ran into the following error:
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log startserver startingsh: /usr/local/var/postgres/server.log: No such file or directory
This could be fixed simply by adding the required "postgres" directory to /usr/local/var/ with the help of the Finder.


Fourth installation step (createdb postgis_template)


This installation step needed as well some adjustment. Trying to execute the command as noted by juniorz, I got a password error, because the command was executed for my user who was unavailable in the DB. After a bit of reading I executed the command with the "-U" option, which allows to execute the command with explicitly giving a user name. I used the postgres default user, whic was the only one available in my local tes DB. With the -U option the command looks like this:
createdb -U postgres postgis_template
you will be asked to give the password after hitting enter

... Here I am stuck in the moment...