How to model Graph data in Postgresql?

Viewed 50319

How would one go about storing and querying sparse directed or undirected graphs in Postgresql. There is something like pggraph, but that is still in planning stage.

I realize dedicated graph databases like Neo4J are best suited for this. However is there way to implement same within Postgresql, by using extension or a data type, which would avoid adding another database engine.dtata

5 Answers

At this point I would recommend experimenting with AgensGraph, a promising multimodel distribution of PostgreSQL that offers first class graph databases and queries from both SQL and Cypher. Note that it is a full server, and not an extension like PostGIS, although PostgreSQL extensions can be added to it.

Update:

AgensGraph is now an extension named AGE and was handed over to the Apache foundation.

Since the question is generic, I would add a solution that can work for mostly planar graphs like street networks - PostgreSQL offers an excellent solution through Postgis Topology.

Postgis Topology stores geometries as edges, nodes and faces and their relative relationships. This means that from the geometry of a street network you can select edges and their starting and ending nodes and from this easily build a graph in the processing engine of your choice (networkx or graph-tool for Python are examples).

As I said, though, Postgresql/Postgis Topology works when we want to study geometries like street networks from the perspective of graph analysis.

Use AgensGraph from bitnine.net https://bitnine.net/

And if you want to visualise your graph you can use AgensBrowser also from bitnine

Related