pgrouting
pgrouting : pgRouting Extension
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 1510 | pgrouting | pgrouting | 4.0.1 |
GIS | GPL-2.0 | C++ |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
| --s-d-r | No | Yes | No | Yes | yes | no |
| Relationships | |
|---|---|
| Requires | plpgsql postgis |
| See Also | postgis mobilitydb h3 pg_polyline q3c pointcloud qdgc pg_geohash pg_sphere pg_eviltransform |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PGDG | 4.0.1 |
18 17 16 15 14 | pgrouting |
plpgsql, postgis |
| RPM | PGDG | 4.0.1 |
18 17 16 15 14 | pgrouting_$v |
- |
| DEB | PGDG | 4.0.1 |
18 17 16 15 14 | postgresql-$v-pgrouting |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
| el8.x86_64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| el8.aarch64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| el9.x86_64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| el9.aarch64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| el10.x86_64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| el10.aarch64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| d12.x86_64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| d12.aarch64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| d13.x86_64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| d13.aarch64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| u22.x86_64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| u22.aarch64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| u24.x86_64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| u24.aarch64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| u26.x86_64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
| u26.aarch64 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 | PGDG 4.0.1 |
Source
github.com/pgRouting/pgrouting
Install
Make sure PGDG repo available:
Install this extension with pig:
Create this extension with:
Usage
pgRouting extends the PostGIS/PostgreSQL geospatial database to provide geospatial routing and other network analysis functionality.
This library contains the following features:
- All Pairs Shortest Path (Floyd-Warshall, Johnson)
- A* algorithm (with bidirectional variant)
- Dijkstra algorithms (cost, cost matrix, driving distance, K shortest paths, via routing, nearest)
- Bidirectional Dijkstra
- Traveling Salesman Problem (TSP)
- Network flow (max flow, Boykov-Kolmogorov, Edmonds-Karp, push-relabel)
- Spanning trees (Kruskal, Prim with BFS/DFS/driving distance variants)
- Graph components (connected, strong, biconnected, articulation points, bridges)
- Turn Restriction Shortest Path (TRSP)
- WithPoints routing (arbitrary locations on edges)
- Graph contraction and utility functions
Getting Started
Enable the extension (requires PostGIS):
Graph Representation
pgRouting represents graphs using SQL queries that return edge data. The standard edge query format:
| Column | Type | Description |
|---|---|---|
id |
ANY-INTEGER | Edge identifier |
source |
ANY-INTEGER | Starting vertex identifier |
target |
ANY-INTEGER | Ending vertex identifier |
cost |
ANY-NUMERICAL | Weight (source to target); negative values exclude the edge |
reverse_cost |
ANY-NUMERICAL | Weight (target to source); default -1 (non-existent) |
Simple Example Without Geometry
Create a graph and find the shortest path:
Function Families
Dijkstra - Shortest Path
The core routing function. Supports one-to-one, one-to-many, many-to-one, many-to-many, and combinations signatures.
Returns: (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
One to One:
One to Many:
Many to Many (undirected):
Combinations:
Dijkstra Cost
Returns only aggregate cost without path details:
Returns: (start_vid, end_vid, agg_cost)
Dijkstra Cost Matrix
Generate a cost matrix for a set of vertices:
Dijkstra Via
Route through an ordered sequence of vertices:
Dijkstra Near
Find the nearest vertex to a set of targets:
A* - Shortest Path
Uses the A* heuristic algorithm. Requires additional coordinate columns (x1, y1, x2, y2) in the edges query.
| Option | Type | Default | Description |
|---|---|---|---|
directed |
BOOLEAN | true |
Graph direction |
heuristic |
INTEGER | 5 |
Distance heuristic (0-5) |
factor |
FLOAT | 1 |
Units manipulation |
epsilon |
FLOAT | 1 |
Approximation factor |
Also available: pgr_aStarCost, pgr_aStarCostMatrix
Bidirectional Algorithms
Bidirectional variants search from both ends simultaneously:
pgr_bdDijkstra,pgr_bdDijkstraCost,pgr_bdDijkstraCostMatrixpgr_bdAstar,pgr_bdAstarCost,pgr_bdAstarCostMatrix
K Shortest Paths (Yen’s Algorithm)
Find the K shortest paths between two vertices:
Returns: (seq, path_id, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
Driving Distance
Find all vertices reachable within a given distance:
Returns: (seq, depth, start_vid, pred, node, edge, cost, agg_cost)
Traveling Salesman Problem
Matrix-based TSP:
Returns: (seq, node, cost, agg_cost)
Euclidean TSP (uses coordinates directly):
Network Flow
Compute maximum flow and related properties:
Flow edges SQL uses capacity and reverse_capacity instead of cost/reverse_cost.
Spanning Trees
Kruskal’s algorithm:
Prim’s algorithm:
Graph Components
Turn Restriction Shortest Path (TRSP)
Route with forbidden path restrictions:
Restrictions SQL format:
| Column | Type | Description |
|---|---|---|
path |
ARRAY[ANY-INTEGER] | Sequence of forbidden edge IDs |
cost |
ANY-NUMERICAL | Cost of the forbidden path |
WithPoints - Routing from Arbitrary Locations
Route between points located on edges (not just vertices):
Points SQL format:
| Column | Type | Default | Description |
|---|---|---|---|
pid |
ANY-INTEGER | Point identifier | |
edge_id |
ANY-INTEGER | Closest edge | |
fraction |
ANY-NUMERICAL | Position on edge (0-1) | |
side |
CHAR | b |
r(right), l(left), b(both) |
Graph Contraction
Simplify graphs by contracting vertices:
Utility Functions
Working with Geometries
Building a Routing Topology
Extract vertices from spatial edges and build topology:
Setting Costs from Geometry Length
Getting Route Geometry
Combine routing results with edge geometries:
Performance Tips
Bound queries to the area of interest to reduce processed edges:
All Pairs Shortest Path
For computing distances between all pairs of vertices:
Returns: (start_vid, end_vid, agg_cost)