SynapCores
Graph in PostgreSQL vs Neo4j: Apache AGE, Recursive CTEs, or a Graph Database?
You can run graph queries inside PostgreSQL with recursive SQL or the Apache AGE extension. Here is how far that gets you, and when Neo4j is the better tool.
Short answer: keep the graph in PostgreSQL while your relationships are shallow: recursive CTEs handle hierarchies and one-to-three hop questions, and the Apache AGE extension adds openCypher to Postgres when you want graph syntax. Move to Neo4j when most queries walk relationships of unknown depth, or when you need graph algorithms and tooling. Plenty of teams run both: Postgres as the system of record and a graph database for the traversal-heavy features.
Choosing between the two products for an AI app, including vector search? See Neo4j vs PostgreSQL (pgvector): which do you actually need.
The difference shows up in the shape of the query. “Revenue by region last month” is a relational question. “Every account that shares a device or card with this chargeback, up to four hops away” is a graph question — in SQL it becomes a recursive CTE that is hard to write and slows down as the graph grows; in Cypher it is one pattern.
Three ways to run graph queries with PostgreSQL
| Criterion | How it works | Pick it when |
|---|---|---|
| Recursive CTEs (plain SQL) | Works on any PostgreSQL, no extension. Fine for hierarchies and 1–3 hop questions; slow and hard to read for variable-depth traversal with high fan-out. | Nothing to install. Start here. |
| Apache AGE extension | Adds openCypher to PostgreSQL (11–18) through a cypher() function, so SQL and graph patterns share one database. Apache-2.0 licensed. Not offered by every managed Postgres service; check your provider. | You want Cypher syntax without a second database. |
| Neo4j next to Postgres | Postgres stays the system of record; Neo4j holds a graph projection for traversal-heavy features and graph algorithms (Graph Data Science). | Graph is core to the product and you can run a sync pipeline. |
Neo4j vs graph in Postgres, side by side
| Criterion | Neo4j | PostgreSQL |
|---|---|---|
| Data model | A property graph: nodes and relationships, both with properties. Relationships are stored as first-class records that point directly at their nodes. | Relational tables. Relationships are foreign keys resolved at query time by joins (plus JSONB, arrays and extensions when rows are not enough). |
| Query language | Cypher — pattern matching such as (a)-[:KNOWS*1..3]->(b), designed for paths and variable-depth relationships. | SQL — the most widely known query language, with window functions, CTEs, and a huge tool ecosystem. |
| Multi-hop queries | Traversal follows stored pointers from node to node, so the cost grows with the part of the graph you touch, not with total table size. Variable depth is one clause. | Each hop is another join, usually index lookups. Variable depth needs a recursive CTE; it works, but becomes slow and hard to read as depth and fan-out grow. |
| Transactions & consistency | ACID transactions; a clustered enterprise edition for scale-out reads. | ACID with decades of hardening, rich constraints, MVCC, and mature replication options. |
| Graph inside Postgres | Native — graph is the whole engine, plus the Graph Data Science library of algorithms. | Possible with the Apache AGE extension (openCypher on Postgres) or recursive SQL; fewer graph algorithms and tools than Neo4j. |
| Vector search | Native vector indexes in recent versions, useful for GraphRAG next to traversals. | pgvector adds HNSW / IVFFlat indexes; the most common way teams add embeddings to an existing app. |
| Operations & ecosystem | A second database to run for most teams; strongest dedicated graph community, Bloom visualization. | Probably already in your stack; every cloud, ORM and BI tool supports it. |
The same question in SQL and Cypher
“Find every account linked to account 13 through shared devices or cards, up to four hops.” In PostgreSQL, with a link table:
WITH RECURSIVE linked(account_id, depth, path) AS (
SELECT 'ACC-13', 0, ARRAY['ACC-13']
UNION ALL
SELECT l2.account_id, linked.depth + 1, path || l2.account_id
FROM linked
JOIN account_links l1 ON l1.account_id = linked.account_id
JOIN account_links l2 ON l2.shared_id = l1.shared_id
AND l2.account_id <> l1.account_id
WHERE linked.depth < 4 AND NOT l2.account_id = ANY(path)
)
SELECT DISTINCT account_id FROM linked WHERE account_id <> 'ACC-13';With Apache AGE, the same question is a Cypher pattern inside PostgreSQL (after CREATE EXTENSION age and loading the data into a graph):
LOAD 'age';
SET search_path = ag_catalog, "$user", public;
SELECT * FROM cypher('fraud', $$
MATCH (s:Account {id: 'ACC-13'})-[*2..8]-(x:Account)
WHERE x.id <> 'ACC-13'
RETURN DISTINCT x.id
$$) AS (account agtype);In Neo4j (Cypher):
MATCH (s:Account {id: 'ACC-13'})-[*2..8]-(x:Account)
WHERE x.id <> 'ACC-13'
RETURN DISTINCT x.id;Each account-to-account link goes through a device or card node, so four account hops are up to eight relationship hops. Both queries answer the question; the graph version is the one people can still read six months later. For a runnable version of this exact pattern, see the fraud-ring recipe.
Pick PostgreSQL when…
- Your data is mostly tables and your relationships are one or two joins deep.
- You need reporting, aggregation and strong relational constraints.
- You want one database the whole team and every tool already knows.
Pick Neo4j when…
- Your key queries are paths of unknown depth: rings, chains, networks.
- You need graph algorithms (shortest path, centrality, community detection).
- You are building a knowledge graph for GraphRAG.
Neo4j vs PostgreSQL: FAQ
Can I run Cypher queries in PostgreSQL?
Yes, with the Apache AGE extension. It adds a graph model to PostgreSQL and lets you run openCypher through a cypher() function inside ordinary SQL, so relational tables and graph data live in the same database. It supports PostgreSQL 11 through 18; check that your managed Postgres provider offers the extension.
Is Neo4j faster than PostgreSQL?
For deep, variable-length relationship queries — friends of friends of friends, fraud rings, dependency chains — a native graph engine usually wins, because it follows stored relationships instead of re-joining tables at every hop. For aggregations, reporting, and short one- or two-join lookups, PostgreSQL is typically as fast or faster. Benchmark your own query shapes.
Can PostgreSQL replace Neo4j?
Often, if your relationships are shallow: foreign keys, joins and recursive CTEs handle hierarchies and one-to-two-hop questions well, and Apache AGE adds openCypher to Postgres. If graph traversal and graph algorithms are the core of the product, a native graph database is the better tool.
When should I use a graph database instead of PostgreSQL?
When most important queries are about paths and connections rather than rows: recommendations via shared connections, fraud and identity resolution, network or dependency analysis, and knowledge graphs for GraphRAG. If most queries filter, aggregate and join a few tables, stay relational.
Can I use Neo4j and PostgreSQL together?
Yes, and many teams do: Postgres stays the system of record and Neo4j holds a graph projection for traversal-heavy features. The cost is a sync pipeline between them and two query languages in the application.
More comparisons: Neo4j vs PostgreSQL with pgvector · TigerGraph vs Neo4j · FalkorDB vs Neo4j · AI-native database vs PostgreSQL
If you need both — relational tables as the system of record and graph traversal over the same data — running PostgreSQL and Neo4j side by side means a sync pipeline and two query languages. SynapCores runs SQL and Cypher-style graph queries in one self-hosted engine over the same data, with vector search alongside.
See how SynapCores unifies both