---
title: "Postgres as a Graph Database: (Ab)using pgRouting"
description: "The pgRouting extension, usually paired with PostGIS for geospatial shortest-path queries, can serve as a lightweight graph layer in Postgres instead of Apache AGE or Neo4j."
url: "https://swe.quest/articles/postgres-as-a-graph-database-ab-using-pgrouting-eceed2225b3c0da2"
canonical: "https://supabase.com/blog/pgrouting-postgres-graph-database"
source: "Supabase"
published: "2025-02-25"
difficulty: "medium"
kind: "How-to"
reading_minutes: 8
tags: ["postgres","algorithms","sql","recommendation-systems"]
---

# Postgres as a Graph Database: (Ab)using pgRouting

A post on [Supabase](https://swe.quest/sources/supabase.md), published 2025-02-25, 8 min read, How-to. Original: [https://supabase.com/blog/pgrouting-postgres-graph-database](https://supabase.com/blog/pgrouting-postgres-graph-database)

## Summary

The pgRouting extension, usually paired with PostGIS for geospatial shortest-path queries, can serve as a lightweight graph layer in Postgres instead of Apache AGE or Neo4j. Three non-GIS examples model graphs as SQL node and edge tables: pgr\_dijkstra computes a project's path through a task-dependency DAG, pgr\_astar routes data across servers weighted by latency, and Dijkstra paths over a video-relationship graph produce ranked recommendations via ordinary SQL aggregation. Each example includes the table schemas and queries.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Algorithms](https://swe.quest/tags/algorithms.md) (Architecture & design)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Recommendations & ranking](https://swe.quest/tags/recommendation-systems.md) (AI & ML)

## Prerequisites

- SQL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- Postgres extensions ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- basic graph concepts

## Keywords

pgRouting, PostGIS, Dijkstra's algorithm, A\* algorithm, Apache AGE, Neo4j, Supabase

## Related posts

- [pg\_graphql: A GraphQL extension for PostgreSQL](https://swe.quest/articles/pg-graphql-a-graphql-extension-for-postgresql-3a34efbe6d730fc2.md): Supabase · 2021-12-03 · Medium · 4 min read · GraphQL, PostgreSQL, API design
  pg\_graphql is an open-source PostgreSQL extension, written in C and SQL, that reflects a GraphQL schema from existing tables and resolves queries entirely inside the database.
- [pg\_graphql v1.0](https://swe.quest/articles/pg-graphql-v1-0-5a4770d60b17d945.md): Supabase · 2022-12-16 · Medium · 5 min read · GraphQL, PostgreSQL, Rust, Performance engineering
  pg\_graphql, a PostgreSQL extension that compiles GraphQL requests into a single SQL query, reaches 1.0 with a SemVer backwards-compatibility guarantee.
- [How pg\_graphql works](https://swe.quest/articles/how-pg-graphql-works-70dff1dd9d5a0c27.md): Supabase · 2024-01-24 · Medium · 9 min read · GraphQL, PostgreSQL, Rust, Compilers
  pg\_graphql is a Rust-based Postgres extension that reflects the SQL schema as a GraphQL schema and serves queries through a \`graphql.resolve\` SQL function, with no separate server.
- [20x faster than pgvector: introducing pg\_embedding extension for vector search in Postgres and LangChain](https://swe.quest/articles/20x-faster-than-pgvector-introducing-pg-embedding-extension-for-vector-search-058737fc9388f495.md): Neon · 2023-07-11 · Medium · 7 min read · Vector search, PostgreSQL, Performance engineering
  pg\_embedding brought HNSW graph-based approximate nearest neighbor indexing to Postgres.
- [postgres.new: In-browser Postgres with an AI interface](https://swe.quest/articles/postgres-new-in-browser-postgres-with-an-ai-interface-55583d716a34d5b8.md): Supabase · 2024-08-12 · Medium · 11 min read · PostgreSQL, WebAssembly, LLMs, AI agents, Browsers
  database.build (formerly postgres.new) runs unlimited Postgres databases entirely in the browser via PGlite, a WASM build of Postgres, paired with GPT-4o that has full autonomy over the disposable database.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
