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.
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 |
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.
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.
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
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
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.
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.
This command ensures that PostgreSQL starts automatically after a system reboot.
sudo systemctl enable postgresql-17
This step starts the PostgreSQL database server.
sudo systemctl start postgresql-17
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.
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
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.
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
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
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
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'
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
The official FreeBSD repository includes PostgreSQL packages by default, and these packages can be installed using the pkg package manager.
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 -
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.
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
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
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
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
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.
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.