Using OpenStreetMap Data with PostgreSQL

By Ercan S. - 13/04/2025 - 0 comments

This guide outlines the steps to import OpenStreetMap (OSM) data into a PostgreSQL database with PostGIS support on a Windows environment.

1. Install PostGIS Extension
Add spatial support to PostgreSQL by installing the PostGIS extension:

https://postgis.net/install/

2. Create a PostgreSQL Database
Create a new database in PostgreSQL to store the OSM data. Then, enable the necessary extensions by executing:

CREATE EXTENSION postgis;
CREATE EXTENSION hstore;

3. Download OSM Data
Obtain the desired .osm.pbf file from Geofabrik:

https://download.geofabrik.de/

4. Download osm2pgsql Import Tool
Download the osm2pgsql tool for importing OSM data into PostgreSQL:

https://osm2pgsql.org/download/windows/

5. Import OSM Data into PostgreSQL
Use the following command to import the downloaded .osm.pbf file into the PostgreSQL database:

<<osm2pgsql_path>>\osm2pgsql.exe -c -d <<db_name>> --slim -U <<db_user>> -S <<osm2pgsql_path>>\default.style -C 1024M -k <<osm.pbf_path>> --password

Example:

C:\Users\Ercan\Downloads\osm2pgsql-bin\osm2pgsql.exe -c -d turkey_osm --slim -U postgres -S C:\Users\Ercan\Downloads\osm2pgsql-bin\default.style -C 1024M -k E:\turkey-latest.osm.pbf --password

Tags: postgresql, postgis, openstreetmap