PostGres – PostGIS on OSX Lion

I recently just wasted a lot of time trying to get PostGIS up and running on OSX Lion. I wanted to do this so I could pull in Open Street Map data for the DC region and then play with it in QGIS and import it into Tilemill. I will be the first to admit that this is a rather complex toolchain, but it shouldn’t have been as difficult as it was.

On the off chance I can save someone time, I will recap what I did… and some of the misstep.

My big misstep was trying to use the precompiled binaries from KyngChaos. While they work fine, the OSM importer for PostGIS always SegFaulted when I tried running it against these versions. I even tried compiling the importer from source code and couldn’t get it work.

Here is what to do:

  1. Use Homebrew. It makes it easy to download and compile source code. You may need to get rid of old cruft you have previously downloaded. I had to remove MacPython and some old Frameworks. If you run “brew doctor” it will give you hints.
  2. Install Homebrew
  3. Install PostGres, PostGIS and all dependancies: ‘brew install postgis’
  4. Initialize the database. This will create a Database structure and a database user (“role”) that is the same as user Mac user name: ‘initdb /usr/local/var/postgres/’
  5. Create a database for GIS stuff (Instead of Luke, use your OSX username): ‘createdb -U Luke -E utf8 gis’
  6. Start the PostGres server – for some reason I needed to specify that it is running on localhost: ‘postgres -D /usr/local/var/postgres/ -h localhost’
  7. Add the PostGIS stuff to the DB: ‘psql -d gis -U Luke -f /usr/local/share/postgis/postgis.sql’ and also ‘psql -d gis -U Luke -f /usr/local/share/postgis/spatial_ref_sys.sql’
  8. Awesome! Now you should have a PostGIS DB loaded up. Now time to install the importer: ‘brew install osm2pgsql’
  9. It is going to fail. This is because you need to apply a patch to fix some inconsistency. Big pain in the ass. To do that, download the patch from here: http://trac.openstreetmap.org/raw-attachment/ticket/3299/patch-build_geometry.cpp
  10. To apply the patch, use the patch command: ‘cd ~/Library/Caches/Homebrew/osm2pgsql–svn/’ and then ‘patch -p0 < ~/Downloads/patch-build_geometry.cpp’
  11. Now try it again and it should work: ‘brew install osm2pgsql’
  12. Awesome, almost there. Next step is to download some OSM data. Cloudmade makes it pretty easy. I grabbed the DC data from here: http://downloads.cloudmade.com/americas/northern_america/united_states/district_of_columbia#downloads_breadcrumbs
  13. Goto where ever you downloaded your files to and run (replacing it with your OSM file): ‘osm2pgsql -U Luke -H localhost -d gis  district_of_columbia.osm’
  14. TADA! All that wonderful data should be loaded into your PostGIS DB. Now open up QGIS and add in a PostGIS layer. Make sure you use your OSX Username as the username (Luke) and the PostGIS DB you just created (DB).

Posted

in

by

Tags:

Comments

7 responses to “PostGres – PostGIS on OSX Lion”

  1. joe larson Avatar
    joe larson

    thank you so much. this taught me how to patch! #n00b

  2. Bobby Wilson Avatar
    Bobby Wilson

    brew install --HEAD osm2pgsql builds cleanly now. No need to run the patch. The directions are otherwise still relevant, thanks.

  3. Simon Avatar
    Simon

    Great walk-through, thanks.

    Got the following an error when trying to load the OSM file on step 13.


    osm2pgsql SVN version 0.80.0 (32bit id space)

    Using projection SRS 900913 (Spherical Mercator)
    Setting up table: planet_osm_point
    NOTICE: table "planet_osm_point" does not exist, skipping
    NOTICE: table "planet_osm_point_tmp" does not exist, skipping
    SELECT AddGeometryColumn('planet_osm_point', 'way', 900913, 'POINT', 2 );
    failed: ERROR: AddGeometryColumn() - invalid SRID
    CONTEXT: SQL statement "SELECT AddGeometryColumn('','',$1,$2,$3,$4,$5, $6)"
    PL/pgSQL function "addgeometrycolumn" line 5 at SQL statement

    Error occurred, cleaning up

    Solved by initializing the projection:


    wget http://svn.openstreetmap.org/applications/utils/export/osm2pgsql/900913.sql

    psql -U -H localhost -d gis -f 900913.sql

    Info found here

    Thought it might help others.

  4. […] blog es algo similar al the Luke Berndt pues después de haber reinstalado OS X seguí sus instrucciones, vale la pena mencionar que mi […]

  5. […] was following this guide, but when I ran into the osm2pgsql compilation issue with the latest version of […]

  6. Teofilo Sibileau Avatar
    Teofilo Sibileau

    Just so you know, it’s actually useful to wrap the postgis db initialization in a template. Just take a look at https://docs.djangoproject.com/en/dev/ref/contrib/gis/install/#creating-a-spatial-database-template-for-postgis
    Thanks for the post!

  7. Tom Avatar
    Tom

    Hopefully you can help me on the problem I’m having.

    brew install osm2pgsql
    Error: No available formula for osm2pgsql

    brew doctor
    Your system is ready to brew.

    I’m on OSX 10.8.3 and can’t get osm2pgsql to install, any ideas?