I use the OpenStreetMap data country extracts from Geofabrik, where you can download individual country extracts of OpenStreetMap files. This is imported into a PostGIS database using osm2pgsql.
Once you have created a postgis database, you can import the file into the same database (but with a different table prefix
./osm2pgsql --slim --merc --create --database gis --prefix PREFIX --style ./default.style /path/to/osm/data/file.osm.pbf
After the data has been converted and imported, you can use this simple script
| #! /bin/bash
set -o nounset
OLD_TABLE_PREFIX=$1
for SUFFIX in line point polygon roads ; do
psql -d gis -c "insert into planet_osm_${SUFFIX} select * from ${OLD_TABLE_PREFIX}_${SUFFIX};"
done
|
Caveats
Duplicate primary keys
If there is some object, like a node, that is in both data files then it'll try to import the same row twice and then there'll be a primary key violation, and that merge doesn't work. This usually happens if you are merging 2 areas that share a border.
Unfortunatly PostgreSQL doesn't have UPSERT support (like MySQL's INSERT ... ON DUPLICATE KEY UPDATE), which would solve this problem.
Alternative Approaches
Osmosis
The usual way to merge OSM files is with osmosis, a powerful openstreetmap manipulation tool. However that works on .osm files, and then you have to import the OSM file into PostGIS, so it can take a while to do both. I just wanted a simple way to "put more data into the exiting database"
osm2pgsql --merge
osm2psql can take an optional argument --merge which will do a very similar thing, but it can fail if there are similar primary keys, and then the whole thing gets aborted. In my experience my approach works where osm2psql --merge fails. I'm not sure why.
There are comments.