Skip to main content

psycopgr

Python client for finding routes and travel costs in a PostgreSQL routing graph.

Requirements

  • Python 3.11 or later with Psycopg 3.
  • Reference database stack: PostgreSQL 18, PostGIS 3.7, pgRouting 4.0.
  • A graph with edge and vertex tables. For OpenStreetMap data, use osm2pgrouting 3.0 or an existing osm2pgrouting 2.x import.

Install

pip install 'psycopgr[binary]'

For this checkout, use pip install '.[binary]'. The base package uses your system's libpq; the binary extra bundles it on supported platforms.

Load a graph

In a database where the extensions are available, enable them once as a database administrator:

CREATE EXTENSION IF NOT EXISTS postgis;
CREATE EXTENSION IF NOT EXISTS pgrouting;

For a new OpenStreetMap graph, follow the osm2pgrouting 3.0 installation instructions, then import an OSM XML extract using its supplied map configuration:

osm2pgrouting --file region.osm --conf /path/to/mapconfig.xml \
  --dbname routing --host localhost --username routing

You can also use a graph you prepared yourself. set_meta_data() accepts schema-qualified table and vertex_table names. If you omit vertex_table, the library uses <table>_vertices_pgr in the same schema. The vertex table needs an id and a point geometry column with the configured SRID. A GiST index on the point column speeds up snapping.

Route between coordinates

from psycopgr import PGRouting, PgrNode

start = PgrNode("start", -87.630, 41.881)
end = PgrNode("end", -87.623, 41.887)

with PGRouting("host=localhost dbname=routing user=routing") as routing:
    # Use these overrides for tables created by osm2pgrouting 3.
    routing.set_meta_data(id="id", geometry="geom", vertex_geometry="geom")

    routes = routing.get_routes(start, end, gpx_file="route.gpx")
    costs = routing.get_costs(start, end)

The default names match osm2pgrouting 2.x: ways.gid and ways_vertices_pgr.the_geom. Edge columns default to source, target, cost_s, and reverse_cost_s. Set has_reverse_cost=False for a graph without the reverse cost column. astar() also expects edge coordinate columns x1, y1, x2, and y2.

PgrNode(id, lon, lat) uses longitude and latitude in WGS84 by default. For a projected graph, set srid and pass the graph's X/Y coordinates in those fields. GPX exports use WGS84. Each endpoint argument can be a single node or an iterable of nodes. Results are keyed by (start_node, end_node). Each route contains a path and a cost. Unreachable pairs and identical endpoints are omitted. The cost combines graph travel time with the time it takes to reach the graph at end_speed (default 10 km/h). Use seconds for the edge costs when routing from coordinates.

dijkstra(), dijkstra_cost(), and astar() work directly with vertex IDs. With the 2.0 client, database errors propagate as Psycopg exceptions. Use the context manager or call close() when finished. For an existing database, test the PostgreSQL and extension upgrade on a restored backup before migrating live data.

Develop and test

Install uv and Docker with Compose, then run:

uv sync --locked
docker compose up --build --wait
PYTHONWARNINGS=error PSYCOPGR_TEST_DSN=postgresql://routing:routing@127.0.0.1:55432/routing \
  uv run --locked --no-sync coverage run --branch --source=psycopgr \
  -m unittest discover -s tests -p test_routing_integration.py -v
uv run --locked coverage report --show-missing --fail-under=95
uv build
uv run --locked twine check --strict dist/*
docker compose down --volumes

The integration suite creates a temporary schema and requires permission to enable the two extensions if they are absent. The Compose database is for local development.

CI runs the routing suite on Python 3.11–3.14, treats Python and database warnings as test failures, and enforces at least 95% branch and statement coverage combined. A separate job imports an OSM fixture with osm2pgrouting 3.0 and routes across the imported graph.

Metadata

Release files for psycopgr 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for psycopgr 2.0.0
File Size Uploaded Provenance Actions
psycopgr-2.0.0.tar.gz 12.0 kB Without provenance Details for psycopgr-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for psycopgr 2.0.0
File Interpreter ABI Platform Actions
psycopgr-2.0.0-py3-none-any.whl Python 3 none any Details for psycopgr-2.0.0-py3-none-any.whl

Total release size: 19.9 kB

Release files / psycopgr-2.0.0.tar.gz

Download URL psycopgr-2.0.0.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b2e9b09ea8baac56e40635c2eb193d7c3e0447bdc12e680cc8d213609124dc33
BLAKE2b-256 checksum
How to use checksums
b0b2371b8349c03fe9edda4b9f1b9038e1e85bad459dcbb9728c45ff43d5ef6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / psycopgr-2.0.0-py3-none-any.whl

Download URL psycopgr-2.0.0-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d2a75db85508e6acfe09ec4a25350f21d32085a1edf37553d411a06638d3c21
BLAKE2b-256 checksum
How to use checksums
e68125996de6513ffb2fa9fe7086d1426c15b779d1c2e1ef30ecd48782814ae9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Security for psycopgr 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page