Getting Started with Apache Age and Age Viewer
Tags: database, postgres, analytics, age
Published: 2023-09-22 08:46:10

Apache Age is an open-source graph database that brings together the best of two worlds: the power of PostgreSQL as a relational database and the flexibility of a graph database for managing and querying complex data relationships.  To make working with AGE even more user friendly, the AGE Viewer is available which is a web-based interface for visualizing and querying  graph data stored in a postgreSQL. In this blog post, we will explore how to get started with Apache Age and Age Viewer, step by step.

## Installing Postgres
First you will need to install an AGE-compatible version of PostgreSQL from the source and configure it.

```
wget https://ftp.postgresql.org/pub/source/v13.8/postgresql-13.8.tar.gz && tar -xvf postgresql-13.8.tar.gz && rm -f postgresql-13.8.tar.gz`

```
The above will download the tar file for the and also extract it in the working directory.
We will now install the pg. The —prefix specifies the path to the psql installation. In this situation, we'll put it in the current directory pwd.

```
cd postgresql-13.8

# configure by setting flags
./configure --enable-debug --enable-cassert --prefix=$(pwd) CFLAGS="-ggdb -Og -fno-omit-frame-pointer"

# now install
make install

```
For a more detailed explanation on installing postgres from source read [here](https://dev.to/dukeofhazardz/installing-postgresql-from-source-code-full-installation-guide-1f6j)

## Installing AGE
Now clone the AGE from the GitHub repo https://github.com/apache/age. 

git clone https://github.com/apache/age.git

Run the pg_config utility and check the version of PostgreSQL. Apache AGE supports all the stable versions of postgresql(11, 12, 13, 14 and 15).

```
pg_config
```
Run the following commands in the source code directory of Apache AGE to build and install the extensiona and run tests.

```
make install

make installcheck
```
If the path to your Postgres installation is not in the PATH variable, add the path in the arguments:

```
make PG_CONFIG=/path/to/postgres/bin/pg_config install

make PG_CONFIG=/path/to/postgres/bin/pg_config installcheck

```
Now we will initialize the database cluster, start the server and creat a database.

```
cd postgresql-11.18/

# intitialization
bin/initdb demo

# start serve
bin/pg_ctl -D demo -l logfile start

# create a database
bin/createdb demodb

```
AGE added to pg successfully. Now we can enter in to pg_sql console to start testing.
Now that you've created a new database, we need to load the AGE extension before we can use it. Also, if we didn't specify search_path and other variables previously in the /postgresql.conf file, we need to do so now.

```
CREATE EXTENSION age;
LOAD 'age';
SET search_path = ag_catalog, "$user", public;

```
Now try some cypher queries:

```
SELECT create_graph('demo_graph');
SELECT * FROM cypher('demo_graph', $$ CREATE (n:Person {name : "james", bornIn : "US"}) $$) AS (a agtype);
SELECT * FROM cypher('demo_graph', $$ MATCH (v) RETURN v $$) as (v agtype);

```

## Installing AGE-Viewer
clone the age-viewer and the recommended node version ^14.16.0.

```
git clone https://github.com/apache/age-viewer.git

```

```
cd age-viewer

npm run setup
npm run start

```
This will start the age-viewer on http://localhost:3000 if port 3000 is free.
To start using Age-Viewer we need to have a running postgreSQL database server with Apache Age Extension for to connect the viewer with the database server. Enter your database details to login.

```
# address (default : localhost)
url: server_url;

# port_num (default 5432)
port: port_num;

# username for the database
username: username;

# password for the user
pass: password;

# database name you wanna connect to
dbname: demodb;

```

## Visualizing Graphs
Now try creating some nodes and then you can visualize their relationship on the age-viewer.


References:
[https://theundersurfers.com/age-installation/](https://theundersurfers.com/age-installation/)
Apache AGE GitHub: [age](https://github.com/apache/age)
Apache AGE Viewer GitHub: [age-viewer](https://github.com/apache/age-viewer)