Some notes about how to build pgRouting on Mac OS X, in case I have to do this again in the future. 🙂 I’m writing this from my memory and I hope I did not forget something, but it should work similar to what I’ve described.
pgRouting adds geospatial routing functionality to a PostGIS / PostgreSQL geospatial database. So, as a preliminary requirement, we have to start with installing PostgreSQL and PostGIS.
I’ve done this using MacPorts, so
$ sudo port -d install postgresql90 postgresql90-server postgis
If you like, you can also install pgAdmin3 via MacPorts, an administration program to PostgreSQL
To create a database instance, after install do
$ sudo mkdir -p /opt/local/var/db/postgresql90/defaultdb
$ sudo chown postgres:postgres /opt/local/var/db/postgresql90/defaultdb
$ sudo su postgres -c '/opt/local/lib/postgresql90/bin/initdb -D /opt/local/var/db/postgresql90/defaultdb'
When executing the last command from above, I remember I had some trouble with the postgres user that was created during installation. Check that the postgres user account can be executed using the
su command, e.g.
$ sudo su - postgres
If not, make sure that the postgres account has a valid shell
$ sudo dscl . -create /Users/postgres UserShell /bin/sh
Note that the installation has created a startup item for starting
postgresql90-server with launchd. It is disabled by default. Execute the following command to start the server, and to cause the server to launch at startup:
$ sudo port load postgresql90-server
As an alternative to launchd, you can also start the database server using:
$ /opt/local/lib/postgresql90/bin/postgres -D /opt/local/var/db/postgresql90/defaultdb
$ /opt/local/lib/postgresql90/bin/pg_ctl -D /opt/local/var/db/postgresql90/defaultdb -l logfile start
After starting the server, you might want to look at
/opt/local/var/log/postgresql90/postgres.log to check if the server is running OK.
Now, let’s try to install pgRouting.
The pgRouting package is also available from MacPorts, but only an older version 1.03. So, I just went to www.pgrouting.org and downloaded the latest source package pgrouting-1.05.tar.gz from 2010/11/22.
However, you won’t be available to successfully build from this archive (at least not on Mac OS X…)! Instead, you will end up with a couple of “undefined symbols” when linking.
For solving this, go to the pgRouting GitHub repository instead. This issue explains the cause why the linking fails on Mac OS X. Fortunately, this issue has already been fixed, so download a current snapshot from the repository and try to build this one instead.
First, you need to edit
cmake/FindPostgreSQL.cmake. Add the include path
and the library path
/opt/local/lib/postgresql90 to the file so that cmake is able to locate your PostgreSQL installation.
Then build pgRouting with
$ cmake .
$ sudo make install
You should end up with something like this
[100%] Built target routing
Install the project...
-- Install configuration: ""
-- Installing: /opt/local/lib/postgresql90/librouting.so
-- Installing: /usr/share/pgrouting/routing_core.sql
-- Installing: /usr/share/pgrouting/routing_core_wrappers.sql
-- Installing: /usr/share/pgrouting/routing_topology.sql
-- Installing: /usr/share/pgrouting/matching.sql
Finally, I hope my memory still serves me right:
$ createdb -U postgres -E utf8 template_postgis
$ psql -U postgres -d template_postgis -f /opt/local/share/postgresql90/contrib/postgis-1.5/postgis.sql
$ psql -U postgres -d template_postgis -f /opt/local/share/postgresql90/contrib/postgis-1.5/spatial_ref_sys.sql
$ psql -U postgres -d template_postgis -f /usr/share/pgrouting/routing_core.sql
$ psql -U postgres -d template_postgis -f /usr/share/pgrouting/routing_core_wrappers.sql
$ psql -U postgres -d template_postgis -f /usr/share/pgrouting/routing_topology.sql
Create a pgRouting enabled database using the new template_postgis template
$ createdb -U postgres -T template_postgis routing
That’s it! If you are new to pgRouting, you can find a good example about route calculation using pgRouting here.