Import OSM data into PostGIS using Osmosis

This blog post is made as a “note-to-self” so that I can remember the procedure. You are of course welcome to read along. It’s does nothing fancy, simply imports the planet.osm file into PostGIS using Osmosis with the Snapshot Schema.

Step by step

Assuming Osmosis is installed (if not download osmosis), and a planet.osm file has been download.

Create database

Update: there is a new way to enable PostGIS and hstore on database, namely ‘create extension postgis’ and ‘create extension hstore’.

My traditional way:

createdb osm
createlang plpgsql osm
# locate hstore.sql
psql -d osm -f /PATH/TO/hstore.sql
# locate postgis.sql
psql -d osm -f /PATH/TO/postgis.sql

New way:

CREATE extension postgis;
CREATE extension hstore;

Create schema

SQL-script files located in Osmosis script directory. Here we’re using the “snapshot schema” (the “simple schema” is another way to go):

psql -d osm -f pgsnapshot_schema_0.6.sql
psql -d osm -f pgsnapshot_load_0.6.sql
psql -d osm -f pgsnapshot_schema_0.6_action.sql
psql -d osm -f pgsnapshot_schema_0.6_bbox.sql
psql -d osm -f pgsnapshot_schema_0.6_linestring.sql

Import data

(if you use the “simple schema”, use --write-pgsimp instead of --write-pgsql):

osmosis \
--read-xml file="planet.osm" \
--write-pgsql host="localhost" database="osm" user="x" password='x'

Wait… (between a couple of minutes and a couple of days, depending on size of planet.osm)

2 Replies to “Import OSM data into PostGIS using Osmosis”

Leave a Reply