skip to Main Content

I’m trying to create a graph database from an existing relational database. So a little background information is that I have a tbale with product numbers and serial numbers and I want a graph database that illustrates their relations. I have a tree like structure in the product numbers, that’s why I want to make a graph database with the apache AGE.

I have managed to run the command:

SELECT * FROM ag_catalog.create_graph('part_numbers_graph');

But when I run:

INSERT INTO part_numbers_graph
    SELECT agtype(format('{"id": %L, "label": "SSN", "properties": {"name": %L}}', SSN, SSN)::jsonb)
    FROM serial_table;

I get the following error:

relation "part_numbers_graph" does not exist

I have ran the command:

SELECT * FROM ag_catalog.ag_graph;

I can see that the graph is there.

 graphid |        name        |     namespace
---------+--------------------+--------------------
32924 | part_numbers_graph | part_numbers_graph

I have succesfully been able to run both

CREATE EXTENSION age;

and

LOAD 'age';

When I run

pg_config

I can see that I have installed
VERSION = PostgreSQL 12.14 (Ubuntu 12.14-1.pgdg22.04+1)

2

Answers


  1. part_numbers_graph is not a regular Postgres table, but a graph table which stores data in the form of vertices and edges and requires data to be inserted using cypher queries.
    For more information above cypher queries, see the official Apache AGE documentation.

    For your particular example, a cypher query would look something like:

    SELECT * FROM cypher('part_numbers_graph', $$
    CREATE (n:SSN {id: 'SSN' , name: 'SSN' }) 
    $$) as (a agtype);
    
    Login or Signup to reply.
  2. To insert values into a graph you should use the cypher function.
    For example in your case the query will look something like this :

    SELECT * FROM cypher('part_numbers_graph', $$ 
    CREATE (u:SSN {"id": "<insert_id>", "name": "<insert_name>""})
    RETURN u $$) AS (u agtype);
    

    With this query you create a node that has SSN label and has 2 property fields id and name. Age has its own way to create id's though so maybe you don’t have to actually hard code your own id in the query and just let age keep track of the id's of the different nodes and edges.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search