How to Install PostgreSQL and Apache AGE on MacOS
Tags: postgres, apacheage
Published: 2023-02-23 22:52:16

## Introduction

PostgreSQL is one of the world's most popular open-source relational database management systems. Its high scalability, reliability, and robustness have made it a top choice for enterprise-level applications. Apache AGE is an extension of PostgreSQL that brings graph database features to the table. While they are available on various operating systems, getting started on a Mac can be challenging for some. In this blog post, I will provide you with a step-by-step guide on how to download and compile PostgreSQL and Apache AGE on MacOS, so you can start using this powerful database system for your projects. Whether you're a beginner or an experienced developer, this guide will help you get started with PostgreSQL and AGE on your Mac. So, let's dive in!

## Pre-Installation Procedure

1\. Before following the process, make sure Xcode is installed, which can be downloaded for free from the Mac App Store. This provides the necessary development tools and libraries required for building PostgreSQL.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xthkqwmhj9bniykcdpq7.png)

2\. After installing Xcode, install Homebrew, a package manager that allows easy installation and management of software packages, by running the following command on Terminal:
```
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```

3\. Install the GCC library which is necessary for compiling PostgreSQL:
```
brew install gcc
```

4\. Install Git, a version control system that developers use to manage source code changes. You will need this to pull the source codes from GitHub. Run the command on Terminal:
```
git --version
```

It should prompt you to install it if Git is not already installed.

By completing these pre-installation procedures, you'll have everything you need to install and compile PostgreSQL on your Mac. Let's move on to the next steps.

## Installing PostgreSQL

In this section, I will guide you through the installation process of PostgreSQL on your MacOS device, step-by-step. From cloning the PostgreSQL repository from GitHub to starting the database server and writing PostgreSQL commands. The following commands are all used inside the Terminal.

1\. Change the directory to where you want to install Postgres. I will use my desktop for this tutorial.
```
cd ~/Desktop
```

2\. Create a directory and name it whatever you like.
```
mkdir PostgreSQL
```

3\. Head to the newly created directory.
```
cd PostgreSQL
```

4\. Clone the PostgreSQL repository from GitHub.
```
git clone https://git.postgresql.org/git/postgresql.git
```

5\. After it is finished, go to the postgresql folder inside your current directory.
```
cd postgresql
```

**Note**
If you would like to use a different version of PostgreSQL (for instance if you want to use Apache AGE that currently only supports versions 11 and 12), follow these steps. If you are fine using the latest version, skip to step 8.

6\. `git branch -a` will show a list of branches for the PostgreSQL repo. The format should look like `remotes/origin/{release version}`. Copy the `{release version}` part of your desired version. I will be using the `REL_12_STABLE` branch for this tutorial.

7\. Navigate to the desired branch.
```
git checkout REL_12_STABLE
```

8\. Run the configure query from the folder. The `/usr/local/pgsql` can be replaced with any directory of your choice. Later when compiling your code, it will be directed to install your binary files in that specific directory.
```
./configure --prefix=/usr/local/pgsql --enable-cassert --enable-debug CFLAGS="-glldb -ggdb -O0 -g3 -fno-omit-frame-pointer"
```
**The options and flags `--enable-cassert --enable-debug CFLAGS="-glldb -ggdb -Og -g3 -fno-omit-frame-pointer` are required if you need to use debugging tools for development.**

This command will generate all the make files for you. If there are any messages saying that a certain library is missing, go ahead and install them using Homebrew, replacing `{library name}` with the name of the library required: `brew install {library name}`

9\. Run the following command to compile the PostgreSQL. `-j12` is an option to compile the code in parallel using multiple cores, in this case, 12 cores. You can change the number to however many cores you have/like.
```
make -j12
```
This process will take a while.

10\. Make a directory at the same location specified during the `./configure` command. For this example, it would be `/usr/local/pgsql`.
```
sudo mkdir /usr/local/pgsql
```
The `sudo` grants administrative privileges. It should prompt you with a password. Go ahead and enter your computer admin user password and hit enter.

11\. Change the owner of the directory to give permission for the current user to configure the directory. Make sure to change `{user name}` to your own.
```
sudo chown {user name} /usr/local/pgsql
```  

12\. The files are now ready to be installed using:
```
make install
```

13\. The next step will be to set the path. Again, the path will depend on the directory you have set previously. Locate the `/bin/` folder in your specified path.
```
export PATH=/usr/local/pgsql/bin/:$PATH
```

14\. To specify where you want to create your cluster, use the following command, using any directory you like:
```
export PGDATA=/usr/local/pgsql/bin/data
```

15\. Initialize your cluster:
```
initdb
```

16\. To start the database server and provide the log file:
```
pg_ctl start -l log
```

17\. Run the following to complete the installation and start writing in PostgreSQL:
```
psql postgres
```
If you would like to get out of writing PostgreSQL commands, type `\q`. Stop the PostgreSQL server using:
`pg_ctl stop -l log`

Congratulations! You have successfully installed PostgreSQL on your MacOS device. With PostgreSQL installed, you can now start building robust and scalable databases for your applications.

## Installing Apache AGE

Now it is time to move on to Apache AGE. Installing it might seem a bit intimidating, but fear not, as I will guide you through it. In this section, I will walk you through the installation procedure for Apache AGE on your MacOS.

1\. Change the directory to where you want to install AGE. I will use my desktop for this tutorial.
```
cd ~/Desktop
```

2\. Create a directory and name it whatever you like.
```
mkdir AGE
```

3\. Head to the newly created directory.
```
cd AGE
```

4\. Clone the AGE repository from GitHub.
```
git clone https://github.com/apache/age
```

5\. After it is finished, go to the age folder inside your current directory.
```
cd age
```

6\. Navigate to the `AGE_PG12.1.0_ALPHA` branch which is compatible for PostgreSQL version 12. **This step can be skipped if you have PostgreSQL version 11 installed.**
```
git checkout AGE_PG12.1.0_ALPHA
```

7\. In order to compile and install AGE in the specified version of your Postgres, you will need to pass `pg_config` inside the bin folder of your installed PostgreSQL directory to the `make` command.
```
sudo make PG_CONFIG=/usr/local/pgsql/bin/pg_config install
```
It should prompt you with a password. Go ahead and enter your computer admin user password and hit enter. Run `installcheck` if you would like to run regression tests. There will be an error but it can be ignored.
```
make PG_CONFIG=/usr/local/pgsql/bin/pg_config installcheck
```

8\. Go back to your installed PostgreSQL directory.
```
cd /usr/local/pgsql
```

9\. Initialize a database with a name of your choice. I will name this database `test`.
```
bin/initdb test
```

10\. Before starting the database, there are some options that have to be configured. Use the text editor in Terminal to edit the `postgresql.conf` file with the command:
```
vim test/postgresql.conf
```
The PostgreSQL configuration file should appear.

11\. While in the configuration file, type `/port` and hit enter. It should bring you to the line that says `#port = 5432`. Press the 'i' key on your keyboard to enter INPUT mode, then delete the `#`. Change the port number if you would like to (you might have to change it if there is another instance of server running on the same port). Take note of this port number for later use. Hit 'ESC' to exit INPUT mode.

12\. While in the configuration file, type `/preload` and hit enter. It should bring you to the line that says `#shared_preload_libraries = ''`. Press the 'i' key on your keyboard to enter INPUT mode, then add in the word `age` between the apostrophes. It should look like `#shared_preload_libraries = 'age'`. Hit 'ESC' to exit INPUT mode.

13\. While in the configuration file, type `/search` and hit enter. It should bring you to the line that says `#search_path = '"$user", public'`. Press the 'i' key on your keyboard to enter INPUT mode, then add in the phrase `ag_catalog, ` before the `"$user"`. It should look like `#search_path = 'ag_catalog, "$user", public'`. Hit 'ESC' to exit INPUT mode.

14\. While in the configuration file, type `:wq` to save and exit the document.

15\. Now, start the database server using:
```
bin/pg_ctl -D test -l logfile start
```

16\. Create the database with the same port number from the configuration file.
```
bin/createdb --port=5432 test
```

17\. Open a command line to the database.
```
bin/psql --port=5432 test
```
You should now have an interface with the database that allows you to enter SQL commands.

18\. Create the AGE extension and load it during the first time you set up the database.
```
CREATE EXTENSION age;
SET search_path TO ag_catalog;
LOAD 'age';
```
Age is now installed into PostgreSQL. You are now able to enter Cypher queries into SQL commands.

Exit the interface with `\q` and shut the server down with `bin/pg_ctl -D test -l logfile stop`.

Congratulations! Now that you have successfully installed Apache AGE, you can start using it to work with your PostgreSQL database. AGE brings in a whole new level of flexibility and performance to your database queries, making it an indispensable tool for big data analytics and real-time data processing.

## Next Steps

Now that you have installed PostgreSQL and Apache AGE on your MacOS device, you may be wondering what's next. Here are a few resources and the next steps to help you continue your journey:

PostgreSQL documentation: The PostgreSQL documentation provides comprehensive information on using and managing PostgreSQL. It is a valuable resource for learning about PostgreSQL's features and functionalities. You can access the documentation at https://www.postgresql.org/docs/.

PostgreSQL tutorial: If you're new to PostgreSQL, you can take a look at the PostgreSQL tutorial to learn the basics of PostgreSQL. You can access the tutorial at https://www.postgresqltutorial.com/.

Apache AGE documentation: The Apache AGE documentation provides various guides on how to utilize the tool to its fullest. There are information about clauses, functions, and using Cypher in SQL. You can access the documentation at https://age.apache.org/age-manual/master/index.html

I hope these resources and the next steps will help you to continue your data analysis journey. Good luck, and happy Postgres-ing!

*This post is written based on a webinar held by Bitnine and [this video guide by Bitnine](https://youtu.be/0-qMwpDh0CA).*