osm2pgsql is a tool for loading OpenStreetMap data into a PostgreSQL / PostGIS database suitable for applications like rendering into a map, geocoding with Nominatim, or general analysis.
See the documentation for instructions on how to install and run osm2pgsql.
- freely configurable table layout and content using the LUA scripting language
- support for building geometries in simple feature format and for generalization
- read OSM data from OSM, PBF and O5M formats
- update the database from OSM change files and replication diffs
- usable for importing a small geographic area on a tiny laptop up to importing the whole world on a large server machine
- runs on Linux, Windows and MacOS
- and more
- Linux: most distributions include osm2pgsql. Debian always offers the latest version via backports.
- macOS: osm2pgsql is available through Homebrew
- Windows: use our binary builds
For detailed instructions, visit the Installation page.
The latest source code is available in the osm2pgsql git repository on GitHub and can be downloaded as follows:
git clone https://github.com/osm2pgsql-dev/osm2pgsql.gitosm2pgsql uses the cross-platform CMake build system to configure and build itself.
Required libraries are
- CLI11
- expat
- proj
- bzip2
- zlib
- Boost libraries (for boost geometry)
- nlohmann/json
- PostgreSQL client library
- Lua
- OpenCV (optional, for generalization only)
- potrace (optional, for generalization only)
- Python (only for running tests)
- Psycopg (only for running tests)
The following libraries are included in the contrib directory. You can build
with other versions of those libraries (set the EXTERNAL_*libname* CMake
option to ON) but make sure you are using a compatible version:
It also requires access to a database server running PostgreSQL (version 12+ works, 14+ strongly recommended) and PostGIS (version 3.0+).
Make sure you have installed the development packages for the libraries mentioned in the requirements section and a C++ compiler which supports C++17. We officially support gcc >= 10.0 and clang >= 13.
To rebuild the included man page you need the pandoc tool.
First install the dependencies.
On a Debian or Ubuntu system, this can be done with:
sudo apt-get install make cmake g++ libboost-dev \
libexpat1-dev zlib1g-dev libpotrace-dev \
libopencv-dev libbz2-dev libpq-dev libproj-dev lua5.3 liblua5.3-dev \
pandoc nlohmann-json3-dev pyosmiumOn a Fedora system, use
sudo dnf install cmake make gcc-c++ libtool boost-devel bzip2-devel \
expat-devel fmt-devel json-devel libpq-devel lua-devel zlib-devel \
potrace-devel opencv-devel python3-osmium \
postgresql-devel proj-devel proj-epsg pandocOn RedHat / CentOS first run sudo yum install epel-release then install
dependencies with:
sudo yum install cmake make gcc-c++ boost-devel expat-devel zlib-devel \
potrace-devel opencv-devel json-devel python3-osmium \
bzip2-devel postgresql-devel proj-devel proj-epsg lua-devel pandocOn a FreeBSD system, use
pkg install devel/cmake devel/boost-libs textproc/expat2 \
databases/postgresql94-client graphics/proj lang/lua52On Alpine, use
apk --update-cache add cmake make g++ nlohmann-json \
postgresql-dev boost-dev expat-dev bzip2-dev zlib-dev \
libpq proj-dev lua5.3-dev luajit-devOnce dependencies are installed, use CMake to build the Makefiles in a separate folder:
mkdir build && cd build
cmake ..If some installed dependencies are not found by CMake, more options may need
to be set. Typically, setting CMAKE_PREFIX_PATH to a list of appropriate
paths is sufficient.
When the Makefiles have been successfully built, compile with
makeThe man page can be rebuilt with:
make manThe compiled files can be installed with
sudo make installBy default, a Release build with debug info is created and tests are disabled. You can change that behavior by using additional options like following:
cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=ONNote that Debug builds will be much slower than release build. For production
Release or RelWithDebInfo builds are recommended.
osm2pgsql has builtin support for the Latlong (WGS84, EPSG:4326) and the
WebMercator (EPSG:3857) projection. Other projections are supported through
the Proj library. Support will be automatically compiled
in, when CMake can find the library in the system. Set the CMake
option WITH_PROJ to OFF to disable use of that library.
To speed up Lua tag transformations, LuaJIT can be optionally enabled on supported platforms. This can speed up processing considerably.
On a Debian or Ubuntu system install the LuaJIT library:
sudo apt-get install libluajit-5.1-devConfiguration parameter WITH_LUAJIT=ON needs to be added to enable LuaJIT.
Otherwise make and installation steps are identical to the description above.
cmake -D WITH_LUAJIT=ON ..Use osm2pgsql --version to verify that the build includes LuaJIT support.
The output should show something like
Lua 5.1.4 (LuaJIT 2.1.0-beta3)
There is some experimental support for data generalization. See https://osm2pgsql.org/generalization/ for details.
This fork contains a set of performance optimizations on top of upstream osm2pgsql. All changes preserve behaviour; output data is unchanged. In local benchmarks (small extracts, tmpfs-backed PostgreSQL) applying a diff to a slim database ran about 40% faster and full imports about 5-10% faster; planet-scale deployments with replication updates are expected to benefit the most.
The most important changes:
- Replication updates (sync): Relation member ways are matched against database results with a sorted index instead of a nested scan that was quadratic in the number of members, way node locations are resolved in a single sorted pass, and tags read from the middle tables are parsed with a SAX parser instead of building a JSON DOM per row.
- COPY serialization: Text values are escaped span-wise instead of character by character, geometries are hex-encoded with a table-driven writer into pre-reserved buffers, numbers are formatted directly into the COPY buffer, and the ~10MB COPY buffers are recycled instead of being freshly allocated. The EWKB writer reserves the exact output size and bulk-copies point arrays.
- Tag processing: Style key matching uses precomputed hash lookups instead of scanning the whole export list for every tag, z_order is computed in a single pass, and Lua tag transforms use cached function references and presized tables.
- Tile expiry: Each ring vertex is projected to tile coordinates exactly once instead of twice per tile row, which is a large win when expiry runs over big polygons.
- Parallelism:
--number-processesnow defaults to the number of CPUs (up to 32) instead of being silently capped at 4, flex id indexes and generalizer tile indexes are created in parallel, and dependent object processing no longer allocates database connections when there is nothing to do.
To measure how much headroom is left beyond these optimizations, the hot import path (PBF decode, mercator projection, EWKB/hex serialization, COPY) was also reimplemented as a small, standalone Rust prototype that formats rows in parallel — something the stage-1 architecture here cannot do. The prototype produces byte-identical database contents (validated on full table dumps) and imported the 48MB Luxembourg benchmark extract about 2.3x faster than this optimized C++ build (1.9s vs 4.4s), with PostgreSQL's own COPY ingest speed as the remaining bottleneck. It is an experiment, not a replacement: it handles a fixed schema with nodes and ways only.
If you have problems with osm2pgsql or want to report a bug, go to https://osm2pgsql.org/support/ .
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
The current maintainers of osm2pgsql are Sarah Hoffmann, Paul Norman and Jochen Topf.
We welcome contributions to osm2pgsql. See CONTRIBUTING.md and https://osm2pgsql.org/contribute/ for information on how to contribute.