Creating your First Graph Database.
Published: 2023-05-03 21:09:59

I assume you have PostgreSQL installed from source code, Apache AGE, installed as well AGE viewer installed. If not, use [this](https://dev.to/chidera/step-by-step-guide-on-installing-wsl-and-postgresql-from-source-code-on-windows-5c84) link, and [this](https://dev.to/chidera/age-installation-guide-for-windows-2c7p) link to install PostgreSQL and AGE.

- 
First, let's create a Database Cluster using the `initdb` command.

```
cd postgresql-11.18   #Enter into the postgres directory
bin/initdb test     #Create a database cluster called test
bin/pg_ctl -D test -l logfile start   #Start the server
bin/createdb testdb   #Create a postgres database
bin/psql testdb   #Enter into the database
```

- 
Having done all that, let us load the extension AGE into our current PostgreSQL session and set the search_path for the current session. This search path determines the order in which PostgreSQL looks for objects (such as tables, functions, and extensions) when they are being referenced in our SQL queries. 
```
CREATE EXTENSION age;
LOAD 'age';
SET search_path = ag_catalog, "$user", public;

```

- 
The syntax of the cypher query is like this.

```
SELECT * FROM cypher('library', $$ 
                     /* cypher_query here */ 
                     () $$) AS (a agtype);
```

- 
Every query in AGE must be enclosed within the `$$` symbol. Let us create a graph, and a vertex without any properties or labels.

```
SELECT create_graph('library');
SELECT * FROM cypher('library', $$ CREATE (n) $$) AS (a agtype);
```

- 
Now let's break down the above queries, To create a vertex I used the `CREATE` clause, In the above query, I created a graph called `library` using the `create_graph` function, and a vertex without any labels or properties.

- 
Having done that, let's create a Vertex, with a label and properties.
```
SELECT * FROM cypher('library', $$ CREATE (n:Book {name : "Purple Hibiscus"}) $$) AS (a agtype);
SELECT * FROM cypher('library', $$ CREATE (n:Book {name : "A Time to Kill"}) $$) AS (a agtype);
SELECT * FROM cypher('library', $$ CREATE (n:Book {name : "If Tomorrow Comes"}) $$) AS (a agtype);
SELECT * FROM cypher('library', $$ CREATE (n:Author {name : "Chimamanda Adichie"}) $$) AS (a agtype);
SELECT * FROM cypher('library', $$ CREATE (n:Author {name : "Sidney Sheldon"}) $$) AS (a agtype);
SELECT * FROM cypher('library', $$ CREATE (n:Author {name : "John Grisham"}) $$) AS (a agtype);
```

- 
In the code above, I created six different Vertices above, and gave them labels of Book and Author respectively, I also gave them different properties as can be seen with the different names they have in the key value pair. 

To view all the Vertex in our graph, run the command below.

```
SELECT * FROM cypher('library', $$
MATCH (v)
RETURN v
$$) as (v agtype);
```

- 
In the query above, we are using the `MATCH` clause to get all the vertices in our graph `library`. We assigned this to a variable called `v`, returned this v variable using the `RETURN` clause and got the output below.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhw96w7tti34zat481vh.jpg)

- 
Now that that is taken care of, Let us create an Edge.

```
SELECT * FROM cypher('library', $$ MATCH (a:Book), (b:Author)
WHERE a.name = 'Purple Hibiscus' AND b.name = 'Chimamanda Adichie' CREATE (a)-[e:WrittenBy]->(b)RETURN e $$) as (e agtype);
SELECT * FROM cypher('library', $$ MATCH (a:Book), (b:Author)
WHERE a.name = 'A Time to Kill' AND b.name = 'John Grisham'
CREATE (a)-[e:WrittenBy]->(b) RETURN e $$) as (e agtype);
SELECT * FROM cypher('library', $$ MATCH (a:Book), (b:Author)
WHERE a.name = 'If Tomorrow Comes' AND b.name = 'Sidney Sheldon' CREATE (a)-[e:WrittenBy]->(b) RETURN e $$) as (e agtype);
```

- 
Again we are using the `MATCH` clause to search out the Vertex Book and Author, we assigned them the variables `a` and `b` respectively, with the` WHERE` clause and the `AND` clause we assessed the properties we are looking for using the `a` and` b` variables, then finally we used the `CREATE` clause to create a relationship(Edge) between these two, We returned our newly created Edge which has the label `WrittenBy` using the variable `e`.

- 
To view all created Edges run the code below.

```
SELECT * FROM cypher('library', $$ MATCH p = (v)-[*]->(b) RETURN relationships(p) $$) as (v agtype);
```

- This will give the output below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oa0it2986m2ctycgdgqt.jpg)
- 
To visualize all we have done, start the Apache AGE viewer. I assume you have installed it using [this](https://dev.to/talhahahae/installing-apache-age-viewer-5np) tutorial, and have done the necessary database connections as explained in that tutorial, remember not to stop the server.

```
npm run start
```

- 
Here is what my view looks like.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ph4nio1lfv6s1d1epn7a.jpg)

- 
Stop the server.


```
bin/pg_ctl -D test -l logfile stop
```
**References**
[Official Apache AGE Documentation](https://age.apache.org/age-manual/master/intro/setup.html)