How to install multiple Postgresql versions on macOS?
By: Oğulcan Girginç
Tags: Macos, Postgresql, Cluster

This article was originally published on ogirginc.github.io. If you prefer, it can be read on Roam Research too.

So, you have decided to have multiple Postgresql versions on your Mac. Lucky for you, I have tried all options I was able to find and decided Peter Eisentraut’s Postgresql Common package, which is a Postgresql database cluster manager for Postgresql.

What is a database cluster?

A database cluster is a bit of an ambiguous term. Its definition varies depending on the context. In this article, I am not going to explain what it means in production. On local machines, it is a collection of databases that are managed by a single database server. Clusters allow us to install multiple versions of Postgresql on our macOS. To manage these clusters, we will be using a package called Postgresql Common.

Postgresql Common with Homebrew

First, we will install the Postgresql Common formulae:

brew install petere/postgresql/postgresql-common

With these formulae installed, we can have multiple versions of PostgreSQL in parallel. We should be able to install any version of Postgresql starting with version 8.3. If you do want to check the available versions, use the Homebrew’s search :

brew search petere/postgresql

Or you check the Github repo too see the availble version on your browser: https://github.com/petere/homebrew-postgresql. If I wanted to install the version 12, this is how I would do it:

brew install petere/postgresql/postgresql@12

This command will fetch the latest minor version of the provided major version. For example, if there are 4 minor versions available, we would have version 12.4 installed automatically. Depending on your internet speed, it should not take more than a couple of minutes to download. Same goes for installing it with the make command, which depends on how fast the computer is.

Creating a cluster for the first time

To create a cluster, we will be using the pg_createcluster wrapper scripts. These scripts are just a wrappers around pg_ctl and initdb to make managing easier. Now, lets create our first cluster. We will name this one as the main but you can choose whatever name you like.

pg_createcluster 12 main

We have created a new cluster named main which is version 12.4. If you like, we can check the status of the cluster with the pg_lsclusters wrapper script.

pg_lsclusters

This should list all the clusters, with some additional information, we have created in our machine.

Ver Cluster Port Status Owner Data directory Log file

12 main 5432 down ogirginc /usr/local/var/lib/postgresql/12/main /usr/local/var/log/postgresql/postgresql-12-main.log

Lets start our newly created cluster, we will be using pg_ctlcluster .

pg_ctlcluster 12 main start

Just to be sure, check the Status with pg_lsclusters . You should see the the word down being replaced with online . Also, the output color change might change depending on your theme. In my case, it was red turned to green.

Ver Cluster Port Status Owner Data directory Log file

12 main 5432 online ogirginc /usr/local/var/lib/postgresql/12/main /usr/local/var/log/postgresql/postgresql-12-main.log

After starting your cluster, rest is the same with the any Postgresql installation. Create a database with created command and list those with psql -c '\l' command to check if all is good. You should something similar to this:

List of databases

Name | Owner | Encoding | Collate | Ctype | Access privileges

-----------+----------+----------+-------------+-------+-----------------------

ogirginc | ogirginc | UTF8 | en_US.UTF-8 | UTF-8 |

postgres | ogirginc | UTF8 | en_US.UTF-8 | UTF-8 |

template0 | ogirginc | UTF8 | en_US.UTF-8 | UTF-8 | =c/ogirginc +

| | | | | ogirginc=CTc/ogirginc

template1 | ogirginc | UTF8 | en_US.UTF-8 | UTF-8 | =c/ogirginc +

| | | | | ogirginc=CTc/ogirginc

(4 rows)

That’s it! Now just repeat the process for each version you want to install and it should work with a problem. If you do encounter any problems, check the Troubleshooting section below. If the problem still continues, feel free to open an issue and I would try my best to help!