October 12, 2023
Summary: in this tutorial, you will learn how to download and install PostgreSQL on Linux using yum.
Most Linux platforms such as Debian, Red Hat / CentOS, SUSE, and Ubuntu have PostgreSQL integrated with their package management.
It is recommended that you install PostgreSQL this way since it ensures a proper integration with the operating system including automatic patching and other update management functionality.
To download and install PostgreSQL on Linux, you visit the download page and select your Linux distribution.
In this tutorial, we’ll show you how to install PostgreSQL on Red Hat Enterprise Linux 8.
Install PostgreSQL on Linux
Once visiting the PostgreSQL download for Red Hat page, you find the script that allows you to install PostgreSQL on Red Hat. You need to execute them one by one
First, execute the following command to install the yum repository RPM:
$ sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Second, disable the built-in PostgreSQL module:
$ sudo yum -qy module disable postgresql
Third, update the package list:
$ sudo yum update
Finally, install PostgreSQL version 14:
$ sudo yum install -y postgresql14-server
Due to policies for Red Hat family distributions, the PostgreSQL installation will not be enabled for automatic start or have the database initialized automatically. To make your database installation complete, you need to perform the following steps, based on your distribution:
$ sudo /usr/pgsql-14/bin/postgresql-14-setup initdb $ sudo systemctl enable postgresql-14 $ sudo systemctl start postgresql-14
It will take few minutes to download and install the PostgreSQL.
Connect to the PostgreSQL database server via psql
In PostgreSQL, a user account is referred to as a role. By default, PostgreSQL uses ident authentication.
It means that PostgreSQL will associate its roles with the system accounts of Linux. If a role exists in PostgreSQL, the same Linux user account with the same name is able to log in as that role.
When you installed PostgreSQL, the installation process created a user account called
postgres associated with the default
To connect to PostgreSQL using the
postgres role, you switch over to the
postgres account on your server by typing:
$ sudo -i -u postgres
It’ll prompt for the password of the current user. You need to provide the password and hit the
Then, you can access the PostgreSQL using the
psql by typing the following command:
You’ll access the postgres prompt like this:
From here, you can interact with the PostgreSQL like issuing a query.
To quit the PostgreSQL prompt, you run the following command:
This above command will bring you back to the postgres Linux command prompt.
To return to your regular system user, you execute the
exit command like this:
Load the sample database
To load the sample database into the PostgreSQL database server, you follow these steps:
First, switch over the postgres account using the following command:
$ sudo -i -u postgres
It’ll prompt you for the password of the current user. You need to type the password of the current user and press the
Second, download the sample database using the
$ curl -O https://www.rockdata.net/files/dvdrental.zip
Third, unzip the dvdrental.zip file to get the dvdrental.tar file:
$ unzip dvdrental.zip
Fourth, access the PostgreSQL using the
Fifth, create the
dvdrental database using the
CREATE DATABASE statement:
postgres=# create database dvdrental;
Sixth, quit the
psql by using the
Seventh, use the
pg_restore tool to restore the
$ pg_restore --dbname=dvdrental --verbose dvdrental.tar
Eighth, access PostgreSQL database server again using
Ninth, switch to the
postgres=# \c dvdrental
Now, you’re connected to the
Finally, enter the following command to get the number of films in the
dvdrental=# select count(*) from film;
Here is the output:
count ------- 1000 (1 row)
Congratulations! you have successfully installed PostgreSQL on Red Hat, connect to PostgreSQL database server using psql, and load the sample database.