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:
- 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.
- Install Homebrew
- Install PostGres, PostGIS and all dependancies: ‘brew install postgis’
- 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/’
- Create a database for GIS stuff (Instead of Luke, use your OSX username): ‘createdb -U Luke -E utf8 gis’
- 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’
- 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’
- Awesome! Now you should have a PostGIS DB loaded up. Now time to install the importer: ‘brew install osm2pgsql’
- 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
- To apply the patch, use the patch command: ‘cd ~/Library/Caches/Homebrew/osm2pgsql–svn/’ and then ‘patch -p0 < ~/Downloads/patch-build_geometry.cpp’
- Now try it again and it should work: ‘brew install osm2pgsql’
- 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
- 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’
- 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).
Comments
7 responses to “PostGres – PostGIS on OSX Lion”
thank you so much. this taught me how to patch! #n00b
brew install --HEAD osm2pgsql
builds cleanly now. No need to run the patch. The directions are otherwise still relevant, thanks.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.
[…] 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 […]
[…] was following this guide, but when I ran into the osm2pgsql compilation issue with the latest version of […]
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!
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?