How to Install PostgreSQL Server on Linux, Windows, and Mac

In our previous article, we explored some of the crucial features that have made Postgres a popular and favored choice among developers.

In this section, we will provide a guide for installing PostgreSQL on various operating systems such as Linux, Windows, and Mac.

Install PostgreSQL Database Server on Linux

The installation process for Linux varies depending on the distribution type, therefore, we will outline the installation steps for significant distributions.

Install PostgreSQL on Debian, Ubuntu & Mint

$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get -y install postgresql

Install PostgreSQL on RHEL, Rocky & AlmaLinux

------------- On RHEL, Rocky & AlmaLinux 9 ------------- 
$ sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
$ sudo dnf -qy module disable postgresql
$ sudo dnf install -y postgresql15-server
$ sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
$ sudo systemctl enable postgresql-15
$ sudo systemctl start postgresql-15

------------- On RHEL, Rocky & AlmaLinux 8 ------------- 
$ sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
$ sudo dnf -qy module disable postgresql
$ sudo dnf install -y postgresql15-server
$ sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
$ sudo systemctl enable postgresql-15
$ sudo systemctl start postgresql-15

Install PostgreSQL on Windows

To install PostgreSQL on Windows, you need to download the installer, which includes the PostgreSQL server, pgAdmin – a graphical tool for managing databases, and StackBuilder – a package manager that can be used to download and install additional PostgreSQL tools and drivers.

Install PostgreSQL on Windows
Install PostgreSQL on Windows

Another way to install PostgreSQL on Windows is using WSL (Windows Subsystem for Linux) as shown.

Install PostgreSQL Using WSL

I highly recommend using WSL (Windows Subsystem for Linux), which will give us an Ubuntu distribution inside Windows.

We can now install everything we need to run Windows Subsystem for Linux (WSL) by entering the following command in an administrator PowerShell or Windows Command Prompt and then restarting the machine.

wsl --install

This command will download and install the latest Linux kernel with all the necessary options; by default, it will install the Ubuntu distribution, which we can change if we want, but for this course, we will use Ubuntu.

After the installation of WSL, we need to update and upgrade system packages using the following command:

$ sudo apt update && sudo apt upgrade

Next, install wget package, which allows us to retrieve files using HTTP, HTTPS, FTP, and FTPS. In our example, we need it to import a PostgreSQL GPG key (GPG is a tool that allows us to verify that we got the official repository).

Wsl is Ubuntu, which means by default it comes with PostgreSQL 12, and here we need the latest version, which is 15, so let’s add the repository for PostgreSQL 15:

$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
$ sudo apt -y update
$ sudo apt -y install postgresql-15
$ sudo -u postgres psql -c "SELECT version();

Install PostgreSQL on Mac

Like Windows, we can download the installer for Mac from the official website and follow the usual installation steps.

The other way to install PostgreSQL on a Mac is by using Homebrew. Let’s check that we have Homebrew installed by running brew in the terminal.

If it is not installed, we need to first install it with the following command:

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once the installation is finished, first update before we dive into the PostgreSQL installation with the following command:

$ brew update

Now everything is up and running, so let’s start installing PostgreSQL:

$ brew install postgresql

This command will download the latest PostgreSQL from the repository and install it for us, which is version 15. Using Homebrew ensures that we get all the dependencies needed for Postgres. we don’t need to install anything else. Everything is up and running.

Conclusion

In conclusion, we have outlined the essential steps to install PostgreSQL on various operating systems. The installation process for Linux differs depending on the distribution.

While Windows and MacOS offer two primary methods of installation: through the official installer or Homebrew for MacOS. It is recommended for Windows users use Windows Subsystem for Linux (WSL) to simplify the installation process by providing an Ubuntu distribution within Windows.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Mead Naji is a freelance developer with extensive experience in web development and Linux system administration. Since January 2022, Mead has been working on developing an e-commerce solution with a new payment system like Stripe, as well as doing one-on-one teaching sessions.

Each tutorial at GeeksVeda is created by a team of experienced writers so that it meets our high-quality standards.

Join the GeeksVeda Weekly Newsletter (More Than 5,467 Programmers Have Subscribed)
Was this article helpful? Please add a comment to show your appreciation and support.

Got Something to Say? Join the Discussion...