[GIS] Loading OpenStreetMap data to a custom PostgreSQL schema via osm2pgsql

openstreetmaposm2pgsqlpostgresql

What is the procedure to load OSM to a different schema than public?

The osm2pgsql documentation mention the parameter --prefix for specifying the tables prefix (planet_osm by default) but does not mention the schema name.
Running osm2pgsql -h -v to list all available commands, it seems there is no parameter for specifying the schema.

How can I load OSM data to a custom schema?

Best Answer

osm2pgsql creates the tables and indexes in the user default schema, i.e. the first one found in its search path, which is usually public. By changing this setting, in PostgreSql, one can load the data to a custom schema. It is important to keep the public schema in the path, as it is required for getting the hstore type.

--Create the new user
CREATE USER osm_loader WITH ENCRYPTED PASSWORD '*****';
--create the schema
CREATE SCHEMA osm AUTHORIZATION osm_loader;
--Change the search_path. 
ALTER ROLE osm_loader SET search_path TO osm, public;

and you can then launch the osm2pgsql tool with the new user

osm2pgsql.exe data.osm.pbf -U osm_loader [... other parameters as needed]

Alternatively, you could change the search path of the user you are currently using. Make sure to change it back once the data is loaded.