Pgrouting- A Practical Guide Jun 2026
This populates the source and target columns and creates a vertices_tmp table containing all nodes.
To see your route in QGIS or a web map like Leaflet, you must join the routing results back to your geometry table:
This returns a sorted list of line segments that form your complete path. PgRouting- A Practical Guide
To visualize this, you must aggregate the result and do a dissolve in PostGIS.
Verify the installation:
-- Then run Dijkstra SELECT * FROM pgr_dijkstra( 'SELECT gid AS id, source, target, cost, reverse_cost FROM roads', (SELECT source FROM vertices_tmp ORDER BY the_geom <-> ST_SetSRID(ST_Point(-122.4194, 37.7749), 4326) LIMIT 1), (SELECT source FROM vertices_tmp ORDER BY the_geom <-> ST_SetSRID(ST_Point(-122.2711, 37.8044), 4326) LIMIT 1), true );
SELECT * FROM pgr_kdijkstra( 'SELECT id, source, target, cost_s FROM roads', ARRAY[101, 102, 103], -- Hospital vertex IDs ARRAY[5001, 5002, 5003], -- Accident vertex IDs directed := true ); This populates the source and target columns and
PgRouting is a powerful extension for PostgreSQL and PostGIS that turns your spatial database into a routing engine. While PostGIS handles the "where" of your data, pgRouting handles the "how to get there." This guide will walk you through the core concepts and practical steps to get a routing system up and running. The Core Concept: Graphs Not Lines
Now that we have a network, let's calculate a path. The most fundamental algorithm in routing is , used for finding the shortest path between two points. Verify the installation: -- Then run Dijkstra SELECT
PgRouting extends the PostGIS (PostgreSQL) spatial database to provide graph routing functionality. Instead of exporting your road network data to an external tool, PgRouting allows you to perform complex shortest path, traveling salesman, and driving distance calculations entirely within the database.
Before we write a single line of SQL, we must understand what PgRouting does under the hood.