Quickstart
This guide creates a small graph in PostgreSQL and runs search, traversal, and shortest-path queries.
Prerequisites
Install PostgreSQL, Rust, and cargo-pgrx:
cargo install cargo-pgrx --version 0.18.0 --locked
cargo pgrx initClone and install pgGraph from the public repository:
git clone https://github.com/Evokoa/pggraph.git
cd pggraph/graph
cargo pgrx install --releaseCreate The Extension
CREATE EXTENSION graph;Create Example Tables
CREATE TABLE companies (
id text PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE people (
id text PRIMARY KEY,
name text NOT NULL,
company_id text REFERENCES companies(id)
);
INSERT INTO companies VALUES
('c1', 'Acme Bank'),
('c2', 'Northwind Trading');
INSERT INTO people VALUES
('p1', 'Alice', 'c1'),
('p2', 'Bob', 'c1'),
('p3', 'Carol', 'c2');Register Tables And Edges
For schemas with primary keys and foreign keys, use auto-discovery:
SELECT * FROM graph.auto_discover('public');
SELECT * FROM graph.build();Manual registration gives more control:
SELECT graph.add_table('people', id_columns := ARRAY['id'], columns := ARRAY['name']);
SELECT graph.add_table('companies', id_columns := ARRAY['id'], columns := ARRAY['name']);
SELECT graph.add_edge(
'people', 'company_id',
'companies', 'id',
'works_at'
);
SELECT * FROM graph.build();Query
Search by indexed property:
SELECT *
FROM graph.search('name', 'Alice');Traverse two hops from Alice:
SELECT *
FROM graph.traverse('people'::regclass, 'p1', 2);Find a shortest path from Alice to Acme Bank:
SELECT *
FROM graph.shortest_path(
'people'::regclass, 'p1',
'companies'::regclass, 'c1'
);Next Steps
- Installation for configuration and upgrade details.
- SQL API for every function and parameter.
- Security before exposing graph functions to application roles.
Last updated on