Install PostgreSQL and AGE from Source Code
Tags: postgres, age, apache
Published: 2023-02-12 08:16:10

## What is Apache AGE
AGE is an acronym for _A Graph Extension_ - an extension built on the top of already existing object relational database management system (ORDBMS)PostgreSQL. AGE is a multi-model database which means that it supports the following 

- graph database and cypher queries

- relational database and SQL queries

- JSON 

- Key value database

- SQL and/or cypher Hybrid Querying

In this article we will look into the following

1. Installation of PostgreSQL 11.18 from source code
 

1. Installation of AGE from source code

## Prerequisites
This article uses the commands for Linux Operating System. You should have Linux installed either on the virtual machine or dual boot alongside Windows/MacOS

- For Windows users: [here](https://www.youtube.com/watch?v=ddk8VX8Hm-I&list=PLGp3huJbWNDjgwP7s99Q-9_w1vxpjNHXG)

- For MacOS users: [here](https://www.youtube.com/watch?v=0-qMwpDh0CA) 

You should also have git installed on your system. To check if git is installed, type into terminal
`git --version`
If it returns the version of git, you are good to go else see the installation guide on [Git Doc](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)

## Installing postgres
**Step 1** Make a directory and a subdirectory where you will install postgres

```
mkdir postgres_age
cd postgres_age
mkdir postgres
cd postgres
```

**Step 2** Install the Linux libraries before the actual installation. The commands may vary for different OS. Check this [link](https://age.apache.org/age-manual/master/intro/setup.html#pre-installation) out if you are not Ubuntu.
`sudo apt-get install build-essential libreadline-dev zlib1g-dev flex bison`

**Step 3** Install the development files for postres from `postgresql-server-dev-xx` package. This command will vary according to the version of Linux. Check out the compatible command for your version [here](https://packages.ubuntu.com/search?keywords=postgresql-server-dev). Note: _Install postgres 11 or 12 as these are the versions AGE is compatible with_. I am on Jammy(22.04 LTS), so I use the following command
`sudo apt install postgresql-server-dev-all`

**Step 4** Download the tar file to install postgres which will extract to the directory /postgres_age/postgres
`wget https://ftp.postgresql.org/pub/source/v11.18/postgresql-11.18.tar.gz && tar -xvf postgresql-11.18.tar.gz && rm -f postgresql-11.18.tar.gz`

**Step 5** Installing psql. While running ./configure, enable debugging flag. To read more on the flags, read [Doc](https://www.postgresql.org/docs/current/install-procedure.html). `pwd` will install postgres in the current directory
```
cd postgresql-11.18
./configure --enable-debug --enable-cassert --prefix=$(pwd) CFLAGS="-ggdb -Og -fno-omit-frame-pointer"
make install
cd ../../
```

## Installing AGE
**Step 6** Clone the AGE GitHub Repository
`git clone https://github.com/apache/age.git`

**Step 7** Installing and configuring AGE with psql

```
cd age
sudo make PG_CONFIG=/home/zainab/postgres_age/postgres/postgresql-11.18/bin/pg_config install
make PG_CONFIG=/home/zainab/postgres_age/postgres/postgresql-11.18/bin/pg_config installcheck
```
If you see that all the tests have passed as shown in the image below, then AGE is installed successfully

![If you see that all the tests have passed as shown in the image, then AGE is installed successfully](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3k8d6bxextlheglab2rc.png)

## Database Initialization
**Step 8** Initialize a database cluster. You can think of database cluster as _The collection of databases which will be managed by single instance of running server._
```
cd ..
cd postgres/postgresql-11.18
bin/initdb pg_age
```

**Step 9** Start server
`bin/pg_ctl -D pg_age -l logfile start`
Now create a database named practicedb
`bin/createdb practicedb`

**Step 10** Launch the postgres command line client psql
`bin/psql practicedb`

## Create and Load AGE
**Step 11** Create and Load AGE extension in order to start using it
```
CREATE EXTENSION age;
LOAD 'age';
SET search_path = ag_catalog, "$user", public;
```

**Step 12** Start querying using cypher commands
```
SELECT create_graph('people');
SELECT * FROM cypher('people', $$ CREATE (n: Person {name : "Zainab", bornIn : "Pakistan"}) $$ ) AS (a agtype);
SELECT * FROM cypher ('people', $$ CREATE (n : Person {name : "John", bornIn : "US"}) $$ ) AS (a agtype);
SELECT * FROM cypher('people', $$ MATCH (v) RETURN v $$) as (v agtype);
```
The output is shown below

![Output screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jl21o3ca2b12a8p02lhr.png)

To get more information about Apache AGE, explore the [website](https://age.apache.org/)

**References**
- [AGE GitHub repository](https://github.com/apache/age)

- [PostgreSQL Doc](https://www.postgresql.org/docs/current/install-procedure.html)

- [AGE Doc] (https://age.apache.org/age-manual/master/intro/setup.html#installing-from-source-code)


If you find any ambiguity or incorrect statement, let me know in comment section

Happy learning!