Command osm2pgsql --append --slim gives ERROR: duplicate key value

I have used manual https://switch2osm.org/serving-tiles/building-a-tile-server-from-packages/ for Ubuntu 14.04 and have imported ¨europe-latest.osm.pbf¨ with command osm2pgsql --slim -C 24500 --number-processes 8 europe-latest.osm.pbf.

Now i want to add USA map, i have downloaded ¨south-america-latest.osm.pbf¨ and executed command osm2pgsql --append --slim -C 24500 --number-processes 8 south-america-latest.osm.pbf BUT i get error:

COPY_END for COPY planet_osm_rels FROM STDIN;
failed: ERROR: duplicate key value violates unique constraint “planet_osm_rels_pkey”
DETAIL: Key (id)=(2668952) already exists.
CONTEXT: COPY planet_osm_rels, line 4301
Error occurred, cleaning up

What could be the problem?

I’m not sure if --append really works with plain OSM files. Normally you use diff files with changes (*.osc) as an input to --append.

I think you could join these files using osmosis and then import from scratch the result of merging

So, if i have imported one map in osm.pbf i can´t just add (append) another map? Lets say, for continents it is very time-consuming process! I can´t just add like map by map to database? Because Europe + USA will take a couple of days again…

You can probably switch databases (changing the name).

Can you please describe a little more your idea?

I mean that in psql you can do:


alter database gis rename to europe;
alter database usa rename to gis;

Of course it would be easier to have kind of script to make the switch instead of doing it manually each time.

So, this mean, if i have imported like Europe map and want to add USA map, in fact, this is no easy and fast process, right? I cannot use just append command. I must somehow join USA and Europe map. In your case, i will have only USA or Europe map, but i need both maps in one :slight_smile:

That is correct. You will first need to merge the two files and then import the merged file. osmconvert or osmium-tools should be able to do the merging.

The reason that ‘–append’ does not work is that --append is for updates to existing data, not for adding another region.

And if you want to import europe and the US, it is questionable whether it even makes sense to use an extract instead of just the whole planet. Because together, NA + Europe have about 75% of the data in OSMs planet file. So you hardly save anything compared to just using the whole planet (which would also make updates easier).

Thank you! :slight_smile: