PostgreSQL Install

Installing PostgreSQL

Installing PostgreSQL is a straightforward process, and it is available for all major operating systems, including Windows, macOS, and Linux.

Using an Installer

For Windows and macOS, the easiest way is to use an installer. The official installer is provided by EDB (EnterpriseDB) and includes both PostgreSQL and the pgAdmin tool.

Installation on Windows

First, download the installer from the official website and run the downloaded .exe file. The setup wizard will guide you through the installation, where you will be asked to set a password for the postgres user (remember this password). You can choose the components to install (pgAdmin 4 is recommended), select an installation directory, and choose a data directory. The installer will set up a Windows service to start PostgreSQL automatically.

Installation on macOS

Download the installer for macOS, which will be a .dmg file. Open the .dmg file, run the installer package, and follow the on-screen instructions. Just like on Windows, you will set a password for the postgres user. The installer will add PostgreSQL to your system path, which allows you to run psql from the terminal.

Using a Package Manager

For macOS and Linux, using a package manager is very common as it makes it easy to install, upgrade, and remove software.

Installation on macOS with Homebrew

Homebrew is a popular package manager for macOS. If you don't have Homebrew, install it first. Open your terminal and run this command:

brew install postgresql

This will install the latest version of PostgreSQL. To start the PostgreSQL service, run:

brew services start postgresql

Installation on Ubuntu/Debian (Linux)

For Debian-based systems, use the apt package manager. First, update your package list:

sudo apt update

Then, install PostgreSQL and its client tools:

sudo apt install postgresql postgresql-client

The service will be started automatically after installation.

Installation on CentOS/RHEL/Fedora (Linux)

For Red Hat-based systems, use yum or dnf. First, you may need to add the official PostgreSQL repository by visiting the PostgreSQL Yum Repository website for instructions. Once the repository is set up, install PostgreSQL:

sudo dnf install postgresql-server

After installation, you must initialize the database cluster:

sudo /usr/pgsql-*/bin/postgresql-*-setup initdb

Then, enable and start the service:

sudo systemctl enable postgresql-*
sudo systemctl start postgresql-*

Verifying the Installation

After installation, you can verify it is working by opening a terminal or command prompt and trying to connect using the psql command-line tool. On Linux, you may need to switch to the postgres user first.

sudo -i -u postgres
psql

If you see the psql prompt, your installation was successful.

Exercise

On macOS, what is a common command-line tool for installing PostgreSQL?