How to Install PostgreSQL on Linux

Written by Semab Tariq

Forget the database setup nightmare. You know the drill—obscure documentation, cryptic error messages, and that sinking feeling as hours turn into days.

But, PostgreSQL is different.

This simple yet powerful open-source relational database management system (RDBMS) combines enterprise-grade features with surprising simplicity. It's why developers and database administrators (DBAs) consistently choose PostgreSQL when they need performance without the setup headaches.

In this blog post, I'll cut through the complexity, showing you exactly how to install PostgreSQL across:

  • Red Hat Family

  • Debian Family

  • FreeBSD

Let’s take a quick look at the key differences between these major Linux distributions before quickly installing PostgreSQL so you can have your database up and running before your coffee gets cold.

Installing PostgreSQL on Linux: Red Hat Family, Debian Family, and FreeBSD Differences

Here is a table that outlines some of the key differences between the Red Hat Family, Debian Family, and FreeBSD Linux systems.

Feature

Red Hat Family

Debian Family

FreeBSD

Package Manager

yum

dnf

apt

apt-get

pkg

ports

Init System

Systemd

Systemd

BSD init (rc scripts)

Kernel

Linux

Linux

FreeBSD

Licensing

GPL (with proprietary add-ons)

GPL and other FOSS licenses

BSD License

Example Distribution

RHEL

CentOS

AlmaLinux

Rocky Linux

Oracle Linux

Fedora

Ubuntu

Linux Mint

Debian

FreeBSD

Update Mechanism

dnf update

yum update

apt update 

apt upgrade

freebsd-update 

pkg upgrade

System requirements

You can install PostgreSQL on even the smallest cloud instances. For example, you can easily install it on a t2.micro instance with just 1 CPU core and 1 GB RAM. 

On Timescale Cloud, PostgreSQL can even run on a minimal instance with just 0.5 CPU cores. 

It shows that PostgreSQL is highly efficient and does not require extensive resources to operate. Now, let's move on to installing PostgreSQL on Linux.

Note: This guide assumes that your instances have an active internet connection to fetch the required packages.

Installing PostgreSQL on Red Hat Family

Go to this URL and choose the options that fit your needs. Here’s what I selected for each one:

  • PostgreSQL Version = 17

  • Platform = Red Hat Enterprise, Rocky, AlmaLinux, or Oracle version 9

  • Architecture = x86_64

Once you select the required information, the necessary installation commands will be displayed. You can copy and paste them into the terminal to start the installation. 

Below is an explanation of what each command does.

Note: If you are using RHEL 8 or Fedora, you should use dnf to install PostgreSQL, as it is the standard package manager for newer systems. However, on RHEL/CentOS 7, you can use yum for package installation.

1.  Add PostgreSQL repository

Install the PostgreSQL Global Development Group (PGDG) repository, which will then enable you to install the PostgreSQL versions not included in the default Red Hat Family repositories.

sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm

2.  Disable the default PostgreSQL module

The default PostgreSQL module in Red Hat Family may provide an older version. The following command disables it (-q suppresses output, and -y auto-confirms).

sudo dnf -qy module disable postgresql

3. Install PostgreSQL server

This step installs PostgreSQL 17 server from the PGDG repository.

sudo dnf install -y postgresql17-server

Now that PostgreSQL is installed on your system, we need to initialize the data directory.

4. Initialize the database

The following command is used to set up the PostgreSQL data directory and configuration files.

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

Note: Replace 17 in the above command with your installed PostgreSQL version.

5. Enable PostgreSQL to start on boot

This command ensures that PostgreSQL starts automatically after a system reboot.

sudo systemctl enable postgresql-17

6. Start the PostgreSQL service

This step starts the PostgreSQL database server.

sudo systemctl start postgresql-17

Installing PostgreSQL on Debian Family

The Debian Family includes PostgreSQL by default. 

A few versions of PostgreSQL packages are already included in the default Debian family repositories, which allows you to install them without needing to configure additional repositories.

You can use this command to find out which version of the PostgreSQL package is available on your system. 

apt-cache policy postgresql If the available version meets your needs, you can install it directly via this command:

apt install postgresql Otherwise, you need to set up the PGDG repository to install a PostgreSQL version that is not available in the Debian Family's default repositories. 

This can be done using an automated script provided by the community or by manually configuring the repository.

Automated Repository Configuration

The automated repository configuration command installs the PostgreSQL common utilities, which include scripts and configuration files required to manage multiple PostgreSQL versions on Debian Family-based systems. 

sudo apt install -y postgresql-common The above command places a script file in the system. Running this file will automatically set up the PGDG repository for our system.

sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh

Manual Repository Configuration

If you want more control over your repository configuration, you can select a manual approach as well, where you will need to execute a couple of commands to install PostgreSQL.

1. Install required packages

Install required packages, such as curl (a tool for downloading files from the Internet) and ca-certificates (which ensure secure communication with HTTPS sources). 

sudo apt install curl ca-certificates

2. Create a directory for PostgreSQL repository files

Now, we need to create a directory where the PostgreSQL repository files, including the signing key, will be stored.

sudo install -d /usr/share/postgresql-common/pgdg   

3. Download the repository signing key

The following command downloads the PostgreSQL repository signing key and saves it in the previously created directory. The signing key ensures that the downloaded PostgreSQL packages are authentic and have not been tampered with.

sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc   

4. Create the repository configuration file

This command adds the PostgreSQL repository to the system’s package sources list. It ensures that the Debian Family retrieves PostgreSQL packages from the official PostgreSQL Global Development Group (PGDG) repository.

sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Install PostgreSQL

After setting up the repository successfully, the next step is to install PostgreSQL.

sudo apt -y install postgresql

If you want to install a version other than the latest one, follow this convention, where PostgreSQL 15 is the required major PostgreSQL version.

sudo apt -y install postgresql-15

Installing PostgreSQL on FreeBSD

The official FreeBSD repository includes PostgreSQL packages by default, and these packages can be installed using the pkg package manager.

1. Switch to root user

Since we are installing the PostgreSQL server package, root privileges are required. Use the following command to switch to the root user

su - or sudo su - 

2. Install the PostgreSQL server package

To install the PostgreSQL client and server packages, use the following command:

pkg install postgresql17-server

You can check the available PostgreSQL versions using the following command:

pkg search postgresql After downloading the packages, a user and group named Postgres will be created.

3. Enabling PostgreSQL to start at boot

After installing PostgreSQL—but before initializing the server—we need to enable it to start automatically at boot by adding postgresql_enable="YES" to the system's rc.conf file. This ensures that PostgreSQL runs automatically on every system reboot without requiring manual intervention.

sysrc postgresql_enable=YES

4. Initialize the PostgreSQL data directory

After installing PostgreSQL, the next step, similar to the Red Hat Family's, is to initialize the data directory before starting the service. Use the following command to initialize it:

service postgresql initdb

5. Start the PostgreSQL service

You have completed the installation and configuration of PostgreSQL. Now, it's time to start the service. Use the following command to do so.

service postgresql start

Installation Verification

After installing PostgreSQL, you should verify that the server is successfully installed and running. If the installation fails, error logs will appear when executing installation commands. However, even if no errors are displayed, it's always a good practice to perform a verification check.

Run the following command in your terminal to check the status of the PostgreSQL service. If you see "Active: active" and no errors in the service logs, then everything is set up correctly. This command works on all distributions.

sudo service postgresql status

Connection With PostgreSQL

Next, we will connect to the newly installed PostgreSQL server. To do this, switch to the postgres user and run the following command, which will connect you with PostgreSQL:

sudo su postgres —- Switch the user to Postgres

postgres@ip-172-31-94-95:/home/ubuntu$ psql —- Connect with PostgreSQL psql (17.4 (Ubuntu 17.4-1.pgdg24.04+2)) Type "help" for help. postgres=#

Note: I installed PostgreSQL 17.4 on my Ubuntu 24.04 VM, which is why the psql utility displays this specific version information. If you are using a different PostgreSQL version or a different distribution, the displayed information will reflect your system accordingly.

Conclusion 

With just a few simple commands, you've installed and configured a PostgreSQL instance. No lengthy troubleshooting sessions, no cryptic error messages—just a powerful database ready for action.

PostgreSQL truly is different.

Need an even faster solution? Timescale Cloud delivers fully-managed PostgreSQL in seconds—zero installation required. You'll get enterprise-grade reliability, automatic backups, and time-series optimizations while skipping the infrastructure management entirely.