PostgreSQL is a hugely popular open source relational database management system (RDBMS) that has been around for over 30 years. It provides support for the SQL language, which is used to manage databases and perform CRUD (Create, Read, Update, Delete) operations.
PostgreSQL has earned a strong reputation for its reliability, flexibility, and performance. It is the primary data store for numerous web and analytics applications. Global giants that rely on PostgreSQL include Spotify, Instagram, Trivago, Uber, and Netflix.
At the time of writing this guide, the latest version is PostgreSQL 15, and in this article, we will show you how to install PostgreSQL on Rocky Linux and AlmaLinux.
Step 1: Add the PostgreSQL repository.
The default PostgreSQL version in the Appstream repositories is PostgreSQL 10.
$ sudo dnf module list postgresql
It is clear from the output that the default PostgreSQL thread marked [ d ]is PostgreSQL 10.
To install the latest version of PostgreSQL, we need to first install the PostgreSQL YUM repository on our system as shown below.
--------------- Rocky & AlmaLinux 9 --------------- $ sudo dnf install--------------- Rocky & AlmaLinux 8 --------------- $ sudo dnf install -y
Step 2: Install PostgreSQL 15 on Rocky/Alma Linux
After creating the PostgreSQL YUM repository, the next step is to update the repositories. Just run the following command to achieve this:
$ sudo dnf update -y
Then disable the default module, which as we saw earlier is PostgreSQL 10.
$ sudo dnf -qy module disable postgresql
Once the module is disabled by default, go ahead and install the PostgreSQL 15 client and server as shown.
$ sudo dnf install -y postgresql15-server
Type "Y" and press ENTER each time you are prompted to import a GPG key.
The command installs the PostgreSQL server and client along with other dependencies. At the very end of the installation, you should display output indicating that all packages were successfully installed.
You can confirm the installed version of PostgreSQL with the command:
$ psql -V psql (PostgreSQL) 15.0
Step 3: Initialize the PostgreSQL Database
Before moving on, we need to initialize the initdb database, which is responsible for creating the new PostgreSQL cluster. A cluster is a group or collection of multiple databases managed by a cluster.
So, to initialize the database, run the command:
$ sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
Step 4: Start and Enable the PostgreSQL Service
With PostgreSQL installed and initialized, the next step is to start the service and get the database server running. But before that, enable PostgreSQL to start at boot time.
$ sudo systemctl enable postgresql-15
After that, start the PostgreSQL database server.
$ sudo systemctl start postgresql-15
To make sure PostgreSQL is up and running, run:
$ sudo systemctl status postgresql-15
From the output, we can see that our database server is working as expected.
Step 5: Connect to PostgreSQL database.
When PostgreSQL is installed, a default database user named postgres is created. It does not require any authentication, so no password is required to log in. In the next step, we will create a password for the postgres user for security reasons.
We are now going to log into the PostgreSQL shell by first switching to the postgres user.
$ sudo su - postgres
Once you have switched to the postgresql user, access the database prompt with the command:
$ psql
Step 6: Set a Password for the Postgres User
Finally, we are going to password protect the postgres user for security reasons. As a sudo user run the command:
$ sudo passwd postgres
Enter a new password and confirm. Now log in again as a Postgres user.
$ su - postgres
And run the shown command.
psql -c "ALTER USER postgres WITH PASSWORD 'your-password';"
The next time you try to login with the postgres user, you will need to authenticate.
$ su - postgres