Installing PostGIS with Homebrew

PostgreSQL    Mac OS X    2012-10-30

I learned a few things about homebrew and PostGIS today - things I probably should have learned sooner, but I don't use homebrew that often.

I am (or was) in the process of getting my local (virtual) environment set up (to start work at my new position at the Texas Tribune). One of the tools we use over there is PostGIS - all well and good, it's just not something I've ever had to install locally before now.

The 'brew info' command gives you a heckuva lot of important information - a list of dependencies, command line options you can include, and a confirmation of the install status:

$ brew info postgis15
	postgis15: stable 1.5.3
	http://postgis.refractions.net
	Depends on: postgresql9, proj, geos
	/usr/local/Cellar/postgis15/1.5.3 (20 files, 5.0M) *
	https://github.com/homebrew/homebrew-versions/commits/master/postgis15.rb
	==> Options
	--with-gui
		Build shp2pgsql-gui in addition to command line tools
	==> Caveats
	To create a spatially-enabled database, see the documentation:
	  http://postgis.refractions.net/documentation/manual-1.5/ch02.html#id2630392
	and to upgrade your existing spatial databases, see here:
	  http://postgis.refractions.net/documentation/manual-1.5/ch02.html#upgrading
	
	PostGIS SQL scripts installed to:
	  /usr/local/share/postgis
	PostGIS plugin libraries installed to:
	  /usr/local/Cellar/postgresql/9.2.1/lib

But there are a few problems - I wish I'd noticed them before I wasted half my morning trying to figure out what was wrong with my local PostgreSQL install (nothing).

If you look at the list of dependencies, you might see the problem. The postgis15 package lists postgresql9 - that's actually version 9.0.8. The command 'brew install postgis15' first installs, as a dependency, PostgreSQL 9.2.1.

==> ./configure --with-projdir=/usr/local --with-pgconfig=/usr/local/Cellar/postgresql/9.2.1/bin/pg_config

But PostGIS 1.5 doesn't work with 9.2.1 - it needs one of the 9.0 versions.

So halfway through my first (or fifteenth) attempt to install postgis15, I had two different versions of PostgreSQL running locally - the version I started out with, 9.0.3, and the one installed by homebrew, 9.2.1.

I thought that using a brew command to switch focus to one of the versions (and change all the symlinks) would help, but no - luckily, it was easy to switch back:

$ brew switch postgresql 9.2.1
$ brew switch postgresql 9.0.3

By this point, 'brew info' showed both versions - my original, plus the new one installed for postgis15:

$ brew info postgresql
	postgresql: stable 9.2.1
	http://www.postgresql.org/
	Depends on: readline, ossp-uuid
	find: /usr/local/Cellar/postgresql/9.0.3/data: Permission denied
	du: /usr/local/Cellar/postgresql/9.0.3/data: Permission denied
	/usr/local/Cellar/postgresql/9.0.3 (2570 files, 35M)
	/usr/local/Cellar/postgresql/9.2.1 (2813 files, 37M)

This homebrew bug is pretty well documented, but the fix isn't that clear:

Build errors for 1.5 with PostgreSQL 9.2.0

postgis15 fails to build on OS X 10.6.8

Django and Postgis on OS X

The trick is to edit the homebrew formula for postgis15:

$ sudo brew edit postgis15

The file will open in your default text editor - just make these two changes, then save and close the file:

    depends_on 'postgresql9'
        ...
    postgresql = Formula.factory 'postgresql9'

Then run the install again:

$ brew install postgis15

This will install PostgreSQL 9.0.8 (unless you already have it). Depending on what other versions of PostgreSQL you've got, you may still need to move symlinks:

$ brew switch postgresql9 9.0.8

$ brew link --overwrite postgresql9

But otherwise, you should be golden.