Installing PostGIS with built-in Postgresql in Mac OS X Lion Server 10.7.3

Installing PostGIS with built-in Postgresql in Mac OS X Lion Server 10.7.3

We recently upgraded our Snow Leopard Mac OS X server to OS X Lion Server, 10.7.3 and were pleased to find that it comes with a version of PostgreSQL (9.0.5) built-in. We're all for having good stuff built-in so we now just had the task of adding PostGIS to it and we'd be set. However this isn't quite as simple as it could be, but luckily isn't too hard and doesn't require any tinkering with Apple or PG 'makefiles' or other scripts, just the judicious use of a few compiler parameters.


The three main problems that need to be overcome are that a) GEOS won't compile with gcc on OS X and needs the 'clang' compiler instead, and b) PostgreSQL expects all extensions to be dual-architecture (i.e i386 and x86_64) which by default PostGIS (and dependencies) don't get built as and finally c) using the correct bundle loader when linking PostgGIS.

Typically the error manifests itself when building PostGIS as :

ld: warning: ignoring file /usr/bin/postgres, file was built for unsupported file format which is not the architecture being linked (x86_64)
Undefined symbols for architecture x86_64:
  "_CurrentMemoryContext", referenced from:

So, firstly download the source for these projects :

PROJ-4.8.0

GEOS-3.3.3

and of course PostGIS

PostGIS 1.5.3

Uncompress the three archives to separate folders, proj-4.8.0, geos-3.3.3 and postgis-1.5.3.

Now, follow these steps to build the PROJ4 library:

cd proj-4.8.0
./configure CC=/usr/bin/clang CXX=/usr/bin/clang++ "CFLAGS=-arch x86_64 -arch i386" "LDFLAGS=-arch x86_64 -arch i386" "CXXFLAGS=-arch x86_64 -arch i386"
make
sudo make install
cd..
Next it's GEOS :
cd geos-3.3.3
./configure CC=/usr/bin/clang CXX=/usr/bin/clang++ "CFLAGS=-arch x86_64 -arch i386" "LDFLAGS=-arch x86_64 -arch i386" "CXXFLAGS=-arch x86_64 -arch i386"
make
sudo make install
cd ..

Then PostGIS itself:

cd postgis-1.5.3
./configure CC=/usr/bin/clang CXX=/usr/bin/clang++ "CFLAGS=-arch x86_64 -arch i386" "LDFLAGS=-arch x86_64 -arch i386" "CXXFLAGS=-arch x86_64 -arch i386"
make "BE_DLLLIBS=-bundle_loader /usr/bin/postgres_real"

Note the parameters to 'make'.  By default the link phase uses '/usr/bin/postgres' as the bundle-loader executable however this doesn't work as Apple have craftily swapped it for a Ruby script and the 'real' Postgres binary is called postgres_real so we need to build against that instead.

At this point (after a few warnings) you should see :

PostGIS was built successfully. Ready to install.

so execute :

sudo make install

At this point it's all standard PostGIS installation however for the sake of completeness here are the next steps :
Add plpgsql to your chosen database (prd in our case) :

$ createlang -U _postgres plpgsql prd

Create the functions, tables and spatial reference data:

$ cd /usr/share/postgresql/contrib/postgis-1.5
$ psql -d prd -U _postgres -f postgis.sql
$ psql -d prd -U _postgres -f spatial_ref_sys.sql

(note the use of _postgres as opposed to postgres).

$ psql -d prd -U _postgres
psql (9.0.5)
Type "help" for help.
prd=# select postgis_full_version();
postgis_full_version
--------------------------------------------------------------------------------------------------
POSTGIS="1.5.3" GEOS="3.3.3-CAPI-1.7.4" PROJ="Rel. 4.8.0, 6 March 2012" LIBXML="2.7.3" USE_STATS
(1 row)
prd=#

That's it! Enjoy.


Comments (0)


Add a Comment

Please login to comment.