
pgRouting is a Postgres extension. It's often used for finding the “shortest path” between two locations, however it's a hidden gem in Postgres and can be used for basic graph functionality.
pgRouting is typically combined with PostGIS for working with geospatial data, but it can also be useful beyond that as a lightweight alternative to Graph extensions like Apache AGE, or specialized graph databases like Neo4j.
Let's explore some useful applications of pgRouting and graphs.
What is pgRouting?
pgRouting is an extension of PostGIS that provides geospatial routing functionality. You can use it to calculate the shortest path, perform network analysis, and solve complex routing problems on a graph-based structure. Most commonly, this is used in Geographic Information Systems (GIS) for tasks like determining the fastest route between two locations.
Working with Graphs
The power of pgRouting lies in its ability to work with any data structured as a graph. A graph is essentially a network of interconnected points, where:
- Nodes represent entities.
- Edges represent relationships or paths between those nodes.
In maps / GIS, nodes and edges represent intersections and roads respectively. However, this structure can also be applied to abstract systems like a social networks, where users are nodes and friendships are edges.

Non-GIS Use Cases for pgRouting
Let's explore how pgRouting can be applied to a few non-GIS problems.
Task scheduling
In any project, tasks have dependencies. For example, task B can only start after task A is completed. This creates a directed acyclic graph (DAG), where:
- nodes represent tasks
- edges represent dependencies
One of the most challenging aspects of managing projects is determining the “critical path” — the project's overall duration, determined by the longest sequence of dependencies.

Using pgRouting, you can model your task's dependencies, using graph algorithms to find the critical path. Suppose we have a table tasks with task dependencies modeled as a graph:
You can then use the pgr_dijkstra()
function to find the shortest (or longest) path through the tasks, allowing you to map out the project schedule effectively:
Which returns a table showing that this project will take 20 days from start to finish:
seq | path_seq | node | edge | cost | agg_cost |
---|---|---|---|---|---|
1 | 1 | 1 | 1 | 3 | 0 |
2 | 2 | 2 | 2 | 4 | 3 |
3 | 3 | 3 | 3 | 5 | 7 |
4 | 4 | 4 | 4 | 2 | 12 |
5 | 5 | 5 | 5 | 6 | 14 |
6 | 6 | 6 | -1 | 0 | 20 |
Tangent: the Dijkstra algorithm
The pgr_dijkstra()
function implements Dijkstra's
algorithm, which is used to find the
shortest path between nodes in a graph. This algorithm guarantees the shortest path from a
source node to a target node (or all other nodes), based on the cost of edges connecting the
nodes.
Fun fact: Dijkstra's algorithm was published in 1959 by Dutch computer scientist Edsger Dijkstra. It's a “greedy” algorithm, meaning it always picks the closest, cheapest node to explore next.
Reverse proxy routing based on resource allocation
Distributed systems usually involve allocating resources efficiently across a network of nodes. Each node might represent a physical location or a computing process, and the edges represent the available pathways to move resources between them. For example, in a cloud infrastructure, pgRouting could help determine how to allocate compute tasks across a set of distributed servers by finding the shortest or least-congested path to route data.
Suppose you have a network of servers represented by nodes and their data connections as edges in a table servers.
You can then use pgr_astar
() to find the most efficient path for data or compute tasks to travel through this network, optimizing for speed or load:
Tangent: the A* algorithm
The pgr_astar()
function is an implementation of the A* (A-star)
algorithm. It's used to find the most
efficient (shortest) path between two points in a graph. A* is commonly used in navigation and
routing because it is more efficient than Dijkstra's algorithm in many scenarios, especially
when you have spatial data with coordinates (e.g., X, Y positions).
Fun fact: A* was originally designed in the 1960s for artificial intelligence applications and pathfinding in games. Today, it's one of the most widely used algorithms in video game development to help characters navigate complex environments efficiently.
Recommendation engines like YouTube
In recommendation engines or search algorithms that use knowledge graphs, pgRouting can be used to build relationships between entities and events. Take YouTube's recommendation algorithm, we can structure this data as a graph where:
- Nodes represent entities like users, videos, or categories.
- Edges represent relationships or interactions between those entities, such as a user liking a video or videos being part of the same category.
Let's create a list of “nodes”:
And some “edges”:
Now we can use the pgr_dijkstra()
function to find the shortest or most relevant path between a user and new videos. For example, let's find videos that are most relevant to user_01
considering their past interactions:
Tangent: ranking recommendations
Since it's “just postgres” it's simple enough to rank the results using an order by
clause. For example, if we stored the pgr_dijkstra()
results above in a table called “recommendations”, we use this a query like this to sort the paths by the highest ranking:
Get Started
pgRouting is a powerful extension for Postgres that can be used to solve a wide range of graph-based problems. Check out the pgRouting docs for more information on how to use it. You can also use it on Supabase:
- Docs: pgrouting: Geospatial Routing
- Launch a new Postgres database: database.new