Step by step guide to install PostgreSQL + Apache AGE on Windows
Tags: postgres, apacheage, database
Published: 2023-04-08 14:44:06

In this post, we will discuss how to install PostgreSQL and Apache AGE on a Windows machine in a step-by-step manner. By the end of this guide, you'll have a fully functional PostgreSQL database and Apache AGE graph database, ready to use for your data-driven applications.

**What is PostgreSQL ?**
PostgreSQL is one of the most famous relational databases in the world that is designed to manage and store large amounts of structured data.

PostgreSQL is open-source and it is known for its reliability, robustness, and scalability. It provides a variety of advanced features, including support for transactions, stored procedures, and triggers.

PostgreSQL supports a wide range of data types, including text, numeric, and date/time data, as well as more complex data structures such as arrays, JSON, and XML.


**What is Apache AGE ?**
Apache AGE is a graph database system that is built on top of PostgreSQL. It allows users to store, manage, and analyze large-scale graph data in a highly performant and scalable way.

Apache AGE combines the benefits of a powerful relational database management system (PostgreSQL) with the flexibility and scalability of a graph database. This means that users can store their data in a relational format and also perform graph-based queries and analyses on the data.

**How to install PostgreSQL and Apache Age ?**
Currently, Apache AGE does not support Windows. Therefore, in this tutorial, we will use Windows Subsystem for Linux (WSL) as an alternative.

**1. Install WSL.**
To enable WSL on your computer, follow these steps: First, go to the Control Panel, and then select Programs. Next, select Turn Windows features on or off. From the list of features, enable both Virtual Machine Platform and Windows Subsystem for Linux, as shown in the image

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

After enabling WSL, the next step is to install a Linux distribution for Windows. To do this, open the Microsoft Store and search for 'Linux.' You will see several options available, and you can choose the one you prefer. For the purposes of this tutorial, we will be using 'Ubuntu 22.04.2 LTS'.

**2. Installing PostgreSQL from source code.**
Please note that currently, Apache AGE only supports PostgreSQL versions 11 and 12, so we must install one of these versions. Before proceeding, we need to install some dependencies. To do this, open a bash terminal by navigating to any directory and typing 'bash' in the address bar, and then run the appropriate command.

```
sudo apt install git libreadline-dev zlib1g-dev bison flex build-essential
```
Next, create a directory where you want to install PostgreSQL and clone the Postgres repository into it.

```
mkdir postgres-AGE-project
cd postgres-AGE-project
git clone https://git.postgresql.org/git/postgresql.git
```
Navigate to the 'postgresql' directory and switch to a compatible version branch. For the purposes of this tutorial, we'll be using version 12.

```
cd postgresql
git checkout REL_12_STABLE
```
Compile the source code and specify the directory where you want the binaries to be installed. Note that this process may take some time to complete.

```
./configure –prefix=/usr/local/pgsql-12
make 
```
After the compilation process is complete, run the following command to add permissions for the binaries directory and replace user by your username. Once this is done, proceed to install PostgreSQL

```
sudo mkdir /usr/local/pgsql-12
sudo chown user /usr/local/pgsql-12
make install
```
To ensure that PostgreSQL can be accessed from anywhere in the terminal, it is necessary to set up the following environment variables
These commands will add the PostgreSQL binaries directory to the system path and set the PGDATA directory to the location of the PostgreSQL data files.

```
export PATH=/usr/local/pgsql-12/bin/:$PATH
export PGDATA=/usr/local/pgsql-12/bin/data
```

Now that you have installed PostgreSQL, you can create a new cluster, which is a collection of databases managed by a single instance of the PostgreSQL server. To create a new cluster, run the following command:

```
initdb
```
After initializing the cluster, you can start the PostgreSQL server by running the following command:

```
pg_ctl -D /usr/local/pgsql-12/bin/data -l logfile start
```

Now you can start PostgreSQL by running the following command:

```
psql postgres
```
Congratulations! You have successfully installed and started PostgreSQL.

To exit PostgreSQL run `\q`

To stop the server run the following command:

```
pg_ctl -D /usr/local/pgsql-12/bin/data -l logfile stop
```

If you would like more detailed information about the installation process, you can visit the following link: [PostgreSQL documentation](https://www.postgresql.org/docs/12/installation.html
)

**3. Installing Apache AGE from source code.**
To install Apache AGE from source code, go to the main directory of the postgres-AGE-project that we created earlier and execute the following command to clone the AGE project.

```
git clone https://github.com/apache/age.git
```
To proceed with the installation, navigate to the project directory and switch to the latest stable release, which is version 1.1.0.

```
cd age
git checkout release/PG12/1.1.0
```
Set the environment variable PG_CONFIG. we need to set the PG_CONFIG environment variable to specify the location of the PostgreSQL pg_config utility. pg_config is a command-line utility that provides information about the installed version of PostgreSQL, including the location of files and libraries required for compiling and linking programs against PostgreSQL. Apache AGE uses pg_config to determine the location of the PostgreSQL header files and libraries needed to compile the AGE extension.

```
export PG_CONFIG=/usr/local/pgsql-12/bin/pg_config
```

Then install AGE extension by running this command:

```
make install
```
Now you have AGE installed to add the extension in your postgres server run the following SQL commands:

```
CREATE EXTENSION age;    
```
Then every time you want to use AGE you must load the extension.

```
LOAD 'age';    
```

Great job! You have successfully installed Apache AGE on your system.

You can find further information on AGE installation and usage on the official Apache AGE documentation at  [AGE documentation](https://age.apache.org/age-manual/master/index.html).

**References**
[PostgreSQL](https://www.postgresql.org/)
[AGE - Installation giude](https://age.apache.org/age-manual/master/intro/setup.html)

**Contribute to Apache AGE**
Apache AGE website: https://age.apache.org/
Apache AGE Github: https://github.com/apache/age