This installation guide (at the time of writing) was tested on SVN trunk of OSM2PGSQL and running on latest/stable Postgres/Postgis versions on OSX via homebrew.

1. Summary

OSX Snow Leopard
 
OSM2PGSQL: 
Head http://svn.openstreetmap.org/applications/utils/export/osm2pgsql Revision: 27034
Last Changed Author: frederik
Last Changed Rev: 27030
Last Changed Date: 2011-11-09 10:57:49 +1100 (Wed, 09 Nov 2011)
 
POSTGRES: 9.0.4
 
POSTGIS: "POSTGIS="1.5.3" GEOS="3.3.1-CAPI-1.7.1" PROJ="Rel. 4.7.1, 23 September 2009" LIBXML="2.7.3" USE_STATS" # SELECT POSTGIS_FULL_VERSION();
 
PHP: 5.3.8 (cli) (built: Nov 14 2011 14:41:52) #php -v

2. Installation
Most of the software is installed via homebrew.

# Install homebrew
% /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"
 
# Install postgresql
% brew install postgresql
% initdb -E utf8 -D /usr/local/var/postgres
% cp /usr/local/Cellar/postgresql/9.0.4/org.postgresql.postgres.plist ~/Library/LaunchAgents/
% launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
% psql -d postgres -f /usr/local/Cellar/postgresql/9.0.4/share/postgresql/contrib/adminpack.sql
 
# Install postgis
% brew install proj
% brew install geos
% brew install postgis
 
# Create template_postgis_osm
% createdb -E utf8 template_postgis_osm
% psql -d template_postgis_osm -f "/usr/local/Cellar/postgresql/9.0.4/share/postgresql/contrib/pg_trgm.sql"
% psql -d template_postgis_osm -f /usr/local/Cellar/postgis/1.5.3/share/postgis/postgis.sql
% psql -d template_postgis_osm -f /usr/local/Cellar/postgis/1.5.3/share/postgis/spatial_ref_sys.sql
 
# Install osm2pgsql. Can skip this.
# % brew install osm2pgsql

For detail instructions on installing Postgres/Postgis via Homebrew, read this homebrew + postgresql9.0.4 + postgis.1.5.3 + proj4 + geos3.3.1 + osm2pgsql. If you are having problems installing GEOS, then read that link as it shows you how to upgrade GEOS to 3.3.1.

OSM2PGSQL needs GEOS as well. Note that brew only install the osm2pgsql binary. Don’t worry, we will compile this via source later.

3. More Installation.

We need to get PHP installed to run gazetteer http://svn.openstreetmap.org/applications/utils/export/osm2pgsql/gazetteer/website/

# Install PHP
% brew install php --with-mysql --with-pgsql --with-apache
 
# Hookup with Apache
# Edit httpd.conf to LoadModule
 
# Install PEAR DB
% pear install db

4. OSM2PGSQL
Read this wiki: http://wiki.openstreetmap.org/wiki/Osm2pgsql. Well, we eventually need the whole OSM2PGSQL source as it contains the website (gazetteer).

% svn co http://svn.openstreetmap.org/applications/utils/export/osm2pgsql osm2pgsql
% cd osm2pgsql
% ./autogen.sh
% ./configure
% make
 
# At this point there should be an osm2pgsql binary.

We need to compile gazetteer for gazetteer.so which is used by gazetteer-functions.sql

gis/osm2pgsql/gazetteer% make clean
gis/osm2pgsql/gazetteer% make 
gis/osm2pgsql/gazetteer% make install
test -z "/usr/local/lib/osm2pgsql" || .././install-sh -c -d "/usr/local/lib/osm2pgsql"
 /bin/sh ../libtool --mode=install /usr/bin/install -c  'gazetteer.la' '/usr/local/lib/osm2pgsql/gazetteer.la'
libtool: install: /usr/bin/install -c .libs/gazetteer.so /usr/local/lib/osm2pgsql/gazetteer.so
libtool: install: /usr/bin/install -c .libs/gazetteer.lai /usr/local/lib/osm2pgsql/gazetteer.la
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/local/lib/osm2pgsql
 
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable
     during execution
 
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
test -z "/usr/local/share/gazetteer" || .././install-sh -c -d "/usr/local/share/gazetteer"
 /usr/bin/install -c -m 644 'extract_countrynames.sql' '/usr/local/share/gazetteer/extract_countrynames.sql'
 /usr/bin/install -c -m 644 'gazetteer-index.sql' '/usr/local/share/gazetteer/gazetteer-index.sql'
 /usr/bin/install -c -m 644 'gazetteer-loaddata.sql' '/usr/local/share/gazetteer/gazetteer-loaddata.sql'
 /usr/bin/install -c -m 644 'gazetteer-tables.sql' '/usr/local/share/gazetteer/gazetteer-tables.sql'
 /usr/bin/install -c -m 644 'import_country_name.sql' '/usr/local/share/gazetteer/import_country_name.sql'
 /usr/bin/install -c -m 644 'import_country_osm_grid.sql' '/usr/local/share/gazetteer/import_country_osm_grid.sql'
 /usr/bin/install -c -m 644 'import_gb_postcodearea.sql' '/usr/local/share/gazetteer/import_gb_postcodearea.sql'
 /usr/bin/install -c -m 644 'import_gb_postcode.sql' '/usr/local/share/gazetteer/import_gb_postcode.sql'
 /usr/bin/install -c -m 644 'import_specialwords.sql' '/usr/local/share/gazetteer/import_specialwords.sql'
 /usr/bin/install -c -m 644 'import_us_statecounty.sql' '/usr/local/share/gazetteer/import_us_statecounty.sql'
 /usr/bin/install -c -m 644 'import_us_state.sql' '/usr/local/share/gazetteer/import_us_state.sql'
 /usr/bin/install -c -m 644 'import_worldboundaries.sql' '/usr/local/share/gazetteer/import_worldboundaries.sql'
 /usr/bin/install -c -m 644 'gazetteer-functions.sql' '/usr/local/share/gazetteer/gazetteer-functions.sql'

5. Download Data
You can get some regional OSM data from cloudmade. http://downloads.cloudmade.com/oceania/australia_and_new_zealand/australia/victoria

I suggest you download a regional extract prior to downloading/testing with the whole planet-osm. If you don’t believe me that it will take long, you can read http://wiki.openstreetmap.org/wiki/Nominatim/Installation

6. Load and Index Data
Basically, this is the summary of commands taken from http://wiki.openstreetmap.org/wiki/Nominatim/Installation At the time of writing this, I had issues such as “planet_osm_ways” (and several tables) does not exist. So I did a pg_dump and restored the tables afterwards. Be very careful with using the script below, you can comment the indexing part just to speed up on loading and see if you have errors, etc.

DATABASE_NAME=gazetteer_vic
OSM2PGSQL_HOME=/Users/rupert/projects/gis/osm2pgsql
SOURCE_DATA=/Users/rupert/Desktop/australia/victoria.osm
DUMP_DIR=/Users/rupert/Desktop/pg_dumps/streetlookup
 
dropdb $DATABASE_NAME 
#dropuser www-data
 
createdb $DATABASE_NAME -E UTF8 -T template_postgis_osm
createuser -SDR www-data
 
# This will create the planet_osm_ways, etc
$OSM2PGSQL_HOME/osm2pgsql --create --latlong --database $DATABASE_NAME --username rupert --slim --prefix planet_osm --cache 2048 $SOURCE_DATA
 
pg_dump --host 127.0.0.1 --port 5432 --username rupert --format custom --file "$DUMP_DIR/planet_osm_ways.backup" --table public.planet_osm_ways $DATABASE_NAME
pg_dump --host 127.0.0.1 --port 5432 --username rupert --format custom --file "$DUMP_DIR/planet_osm_nodes.backup" --table public.planet_osm_nodes $DATABASE_NAME
pg_dump --host 127.0.0.1 --port 5432 --username rupert --format custom --file "$DUMP_DIR/planet_osm_rels.backup" --table public.planet_osm_rels $DATABASE_NAME
 
# This will create the place table
$OSM2PGSQL_HOME/osm2pgsql --latlong -O gazetteer --database $DATABASE_NAME --username rupert --slim --prefix planet_osm --cache 2048 $SOURCE_DATA
 
pg_restore --host 127.0.0.1 --port 5432 --username rupert --dbname $DATABASE_NAME "$DUMP_DIR/planet_osm_ways.backup"
pg_restore --host 127.0.0.1 --port 5432 --username rupert --dbname $DATABASE_NAME "$DUMP_DIR/planet_osm_nodes.backup"
pg_restore --host 127.0.0.1 --port 5432 --username rupert --dbname $DATABASE_NAME "$DUMP_DIR/planet_osm_rels.backup"
 
rm -Rf $DUMP_DIR/*.backup
 
psql -d $DATABASE_NAME -q -f import_country_osm_grid.sql
psql -d $DATABASE_NAME -q -f import_worldboundaries.sql
psql -d $DATABASE_NAME -q -f import_country_name.sql
psql -d $DATABASE_NAME -q -f import_gb_postcode.sql
psql -d $DATABASE_NAME -q -f import_gb_postcodearea.sql
psql -d $DATABASE_NAME -q -f import_us_state.sql
psql -d $DATABASE_NAME -q -f import_us_statecounty.sql
 
psql -d $DATABASE_NAME -f gazetteer-functions.sql
 
psql -d $DATABASE_NAME -f gazetteer-tables.sql
 
psql -d $DATABASE_NAME -f gazetteer-functions.sql
 
psql -d $DATABASE_NAME -f gazetteer-loaddata.sql
 
#Indexing
psql -d $DATABASE_NAME -f gazetteer-index.sql

Save this as run.sh in /Users/rupert/projects/gis/osm2pgsql/gazetteer

Where do you run this?

% cd /Users/rupert/projects/gis/osm2pgsql/gazetteer
% sh run.sh

6. Test
If you are successful, you should have a “placex” table. Now that we have a postgis database running, you can now run spatial statements thru pgadmin. See the guts of reverse.php

SELECT * 
FROM placex
WHERE ST_DWithin( ST_SetSRID(ST_Point(145.234377, -37.856320),4326), geometry, 0.0001)
AND ST_GeometryType(geometry) NOT IN ('ST_Polygon', 'ST_MultiPolygon')

This one took only 21 ms.

7. Website

Make sure www-data have permissions to the tables. Rememeber to replace gazetteer_vic with your DATABASE_NAME.

for tbl in `psql -qAt -c "select tablename from pg_tables where schemaname = 'public';" gazetteer_vic` ; do psql -c "alter table $tbl owner to \"www-data\"" gazetteer_vic; done

Assuming you have PHP and PEAR DB installed. Then update the data connection settings found in http://svn.openstreetmap.org/applications/utils/export/osm2pgsql/gazetteer/website/.htlib/settings.php

Run the same query but using reverse.php.

http://127.0.0.1/nominatim/reverse.php?format=xml&lat=-37.856320&lon=145.234377&zoom=18&addressdetails=1

reverse.png