Osm2pgsql (maintained by the osm2pgsql development team and open-source contributors under the GPL-2.0 open-source license) is a high-performance command-line importer and incremental update engine for loading OpenStreetMap (`.osm` / `.osm.pbf` / `.o5m`) data into PostgreSQL and PostGIS databases. Built for GIS engineers, routing/cartography backends, spatial analysts, and web map developers, osm2pgsql turns raw OSM nodes, ways, and relations into clean PostGIS points, lines, polygons, and custom relational tables—supporting full initial loads, minute/daily/planet diff updates, and flexible Lua schema transformations.
1. In-Depth Review: Why PostGIS & Spatial Teams Use osm2pgsql
- Programmable Lua `flex` Output for Custom Spatial Schemas: Lets engineers shape exact PostgreSQL/PostGIS tables, column types, JSONB tags, and geometry transformations via Lua scripts (`osm2pgsql-flex`), making it straightforward to feed custom cartography, analytics, or routing tables.
- Continuous Incremental Updates (`--append`) Without Full Re-Imports: Applies OSM replication diffs (`.osc` / `.osc.gz`) directly into an existing PostGIS database so road, building, and POI updates stay fresh without rebuilding planet-scale tables from scratch.
- Tuned Multi-Stage Memory & PostGIS Index Performance: Separates node/way/relation indexing, geometry assembly, and spatial/attribute index creation with clear cache tuning (`-C`) so large regional or national extracts load predictably.

2. Key Features & Capabilities
- Fast `.osm.pbf` & `.osm` Ingestion into PostGIS: Imports OpenStreetMap extracts with automatic geometry construction for points, linestrings, and multipolygons.
- Flexible Lua `flex` Output Engine: Define custom target tables, SRID projections, filtered tags, and derived attributes using clean Lua callbacks.
- Incremental Diff Updates (`--append` & `osm2pgsql-replication`): Keep live PostGIS databases synchronized with ongoing OpenStreetMap changesets.
- Slim Mode & Middle Storage Control: Enables object lookup and incremental updates across large datasets with configurable cache and tablespace options.
- Works with QGIS, MapServer, GeoServer, Martin & pg_tileserv: Produces standard PostGIS geometry columns ready for desktop GIS inspection, WMS/WFS services, and dynamic vector tile servers.
- 100% Free & Open-Source (GPL-2.0 License): Free to download, compile, and run on Linux, macOS, and Windows.
- Official Links: Free Trial, Free / Education & Pricing
Always download osm2pgsql packages, documentation, and manuals directly from official portals:
- Official osm2pgsql Manual & Documentation: https://osm2pgsql.org/doc/
- Official osm2pgsql GitHub Repository & Releases: https://github.com/osm2pgsql-dev/osm2pgsql
- Official Geofabrik OpenStreetMap `.osm.pbf` Downloads: https://download.geofabrik.de/
- Official PostGIS Spatial Extension Documentation: https://postgis.net/documentation/
4. Step-by-Step Installation Guide
- Check PostgreSQL, PostGIS & OS Requirements: Confirm you have PostgreSQL with the `postgis` extension enabled on a 64-bit Linux, macOS, or Windows host with sufficient SSD space for PostGIS tables and indexes.
- Install via OS Package Manager or Build from Source: Install `osm2pgsql` via your Linux package manager (`apt install osm2pgsql` on Ubuntu/Debian), Homebrew on macOS (`brew install osm2pgsql`), or build with CMake from https://github.com/osm2pgsql-dev/osm2pgsql.
- Enable PostGIS in Your Target Database: Connect to your PostgreSQL database (`gisdb`) and ensure `CREATE EXTENSION postgis;` has been executed.
- Verify Installation in Terminal: Run `osm2pgsql --version` in your terminal to confirm the CLI binary and library versions are active.
5. How to Use osm2pgsql: Beginner Workflow
- Download an OpenStreetMap `.osm.pbf` Extract: Download your target country or region file (such as `laos-latest.osm.pbf`) from Geofabrik (`https://download.geofabrik.de/`).
- Create & Prepare Your PostGIS Database: Create a dedicated PostgreSQL database (e.g., `createdb gisdb`) and enable `postgis` (`psql -d gisdb -c "CREATE EXTENSION postgis;"`).
- Run Initial Import with `osm2pgsql`: Execute `osm2pgsql -d gisdb --create --slim -C 2048 laos-latest.osm.pbf` (adjusting `-C` cache MB to suit available RAM) to load features and build spatial indexes.
- Inspect Tables in QGIS or `psql`: Connect QGIS or DBeaver/pgAdmin to `gisdb` and load the generated polygon, line, and point layers (`planet_osm_polygon`, `planet_osm_line`, `planet_osm_point`, or custom flex tables).
- Apply Incremental Diff Updates (Optional): Use `--append` with `.osc` change files or configure `osm2pgsql-replication` to keep your PostGIS layers updated automatically.
6. Frequently Asked Questions (FAQ)
- Is osm2pgsql completely free for commercial and government projects?Yes. osm2pgsql is free and open-source software licensed under the GNU General Public License v2.0 (GPL-2.0).
- What is the difference between `osm2pgsql` and `Tilemaker`?`osm2pgsql` loads OpenStreetMap data into PostgreSQL/PostGIS for SQL queries, spatial analysis, and dynamic tile/WMS servers, whereas `Tilemaker` converts `.osm.pbf` directly into `.pmtiles` or `.mbtiles` vector tile files without needing PostgreSQL.
- Can I customize which OpenStreetMap tags and tables get created in PostGIS?Yes. Using the `flex` output (`-O flex -S style.lua`), you can write Lua logic to filter specific tags, create custom tables, and shape columns exactly for your application.