How To Install PostgreSQL 15 on Rocky Linux 8 Easy Setup

Posted on

How To Install PostgreSQL 15 on Rocky Linux 8 Easy Setup

This guide on the Orcacore website will teach you How To Install PostgreSQL 15 on Rocky Linux 8. PostgreSQL 15 adds features to simplify conflict management, including the ability to skip replaying a conflicting transaction and to automatically disable a subscription if an error is detected. This release also includes support for using a two-phase commit (2PC) with logical replication. Let’s delve into How To Install PostgreSQL 15 on Rocky Linux 8.

Steps To Install and Configure PostgreSQL 15 on Rocky Linux 8

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Rocky Linux 8.

1. Set up PostgreSQL 15 on Rocky Linux 8

To install PostgreSQL 15, you must add the PostgreSQL 15 repository on your server.

Add PostgreSQL 15 Repository on Rocky Linux 8

At this point, you can use the command below to add the latest PostgreSQL repository on your server:

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

Then, update your local package index with the following command:

sudo dnf update -y

Install PostgreSQL 15 on Rocky Linux 8

First, you need to disable the default module of PostgreSQL with the following command:

sudo dnf -qy module disable postgresql

Then, use the following command to install PostgreSQL 15 on your server:

sudo dnf install -y postgresql15-server

When your installation is completed, verify it by checking its version:

psql -V

In your output you will see:

Install PostgreSQL 15 on Rocky Linux 8

Initialize PostgreSQL 15 Database

At this point, you need to initialize the initdb database which is responsible for creating a new PostgreSQL cluster. A cluster is a group or collection of several databases that are managed by a cluster.

To do this, run the following command:

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

When it is finished, you will get the following output:

**Output**
Initializing database ... OK

Manage PostgreSQL 15 Service

At this point, you can start and enable your PostgreSQL 15 on Rocky Linux 8. To do this, run the commands below:

# sudo systemctl enable postgresql-15
# sudo systemctl start postgresql-15

Verify your PostgreSQL 15 is active and running on your server with the command below:

sudo systemctl status postgresql-15

2. Connect To PostgreSQL 15 Shell on Rocky Linux 8

PostgreSQL uses a concept called roles to handle authentication and authorization. During the PostgreSQL installation, Postgres is set up to use ident authentication, meaning that it associates Postgres roles with a matching Unix/Linux system account.

If a role exists within Postgres, a Unix/Linux username with the same name can sign in as that role. One way is to switch over to the Postgres account on your server by running the following command:

sudo -i -u postgres
**Output**
postgres@sam:~$

You can access the Postgres 15 shell on Rocky Linux 8 by running the following command:

postgres@sam:~$ psql

With this command, you will see that your shell prompt changes to:

**Output**
psql (15.0)
Type "help" for help.

postgres=#

To exit from the PostgreSQL 15 shell on Rocky Linux 8, run the following command:

postgres=# q

With this command, you will be back to the Postgres Linux command prompt.

postgres@sam:~$

You can type exit to return to the regular system user.

postgres@sam:~$ exit

Also, you can connect to your Postgres shell on Rocky Linux 8 in another way by typing the following command:

sudo -u postgres psql

In this way, you will directly connect to your PostgreSQL 15 shell.

**Output**
psql (15.0)
Type "help" for help.

postgres=#

Create a new PostgreSQL 15 Role

At this point, you can create a new Postgres role in two ways.

First, if you are logged in as a Postgres account, you can use the following command:

postgres@sam:~$ createuser --interactive

Second, you can use sudo for each command without switching from your normal account on Rocky Linux 8:

sudo -u postgres createuser --interactive

Both ways will be asked to enter your role name and whether the new role is a superuser or not.

**Output**
Enter name of role to add: orca
Shall the new role be a superuser? (y/n) y

Create a new PostgreSQL 15 Database

The Postgres authentication system for any role used to log in, the role should have a database with the same name that it can access.

If you logged in as the Postgres account, run the following command to create the database with the name of the role you have created in the previous step:

postgres@sam:~$ createdb orca

Or you can use sudo to create the database on Rocky Linux 8:

sudo -u postgres createdb orca

Open the Postgres 15 Shell with the new Role

At this point, you need to create a Linux user with the same name as your Postgres role and database. To do this, run the following command:

sudo adduser orca

Now you can connect to the Postgres database with the following commands:

# sudo -i -u orca
# orca@sam:~$ psql

Or you can use the following command instead:

sudo -u orca psql
Output
psql (15.0)
Type "help" for help.

orca=#

When you have logged in to your PostgreSQL database on Rocky Linux 8, you can check your current connection information by running the following command:

orca=# conninfo

In your output you will see:

**Output**
You are connected to database "orca" as user "orca" via socket in "/var/run/postgres

For more information, you can visit the PostgreSQL Documentation page.

Conclusion

At this point, you have learned to Install and Configure PostgreSQL 15 on Rocky Linux 8. PostgreSQL 15 on Rocky Linux 8 is used as a powerful, open-source relational database for managing structured data efficiently. This guide provided a straightforward approach to How To Install PostgreSQL 15 on Rocky Linux 8.

Hope you enjoy it. You may also like these articles:

How To Migrate From Rocky Linux 8 To Rocky Linux 9

Install and Configure Nextcloud on AlmaLinux 9

How To Install Remi Repository on AlmaLinux 8

Alternative Solutions for Installing PostgreSQL 15 on Rocky Linux 8

While the guide above provides a direct method using the official PostgreSQL repositories, here are two alternative approaches to achieve the same goal of installing PostgreSQL 15 on Rocky Linux 8:

1. Using Docker Container

Docker provides a containerization platform that allows you to run applications in isolated environments. This approach offers several advantages:

  • Isolation: PostgreSQL runs in its own container, preventing conflicts with other software on the host system.
  • Reproducibility: The container image ensures a consistent environment across different machines.
  • Ease of Management: Docker simplifies the process of starting, stopping, and managing PostgreSQL instances.

Explanation:

This method involves pulling a pre-built PostgreSQL 15 Docker image from a registry like Docker Hub. Then, you create a container from that image, mapping a host directory to the container’s data directory for persistent storage. Finally, you configure the container’s environment variables (like POSTGRES_PASSWORD) to set up the initial database.

Code Example:

First, install Docker on Rocky Linux 8 if you haven’t already:

sudo dnf install docker -y
sudo systemctl start docker
sudo systemctl enable docker

Next, pull the PostgreSQL 15 image:

sudo docker pull postgres:15

Then, run the Docker container with the following command:

sudo docker run --name postgres15 -e POSTGRES_PASSWORD=your_password -v /path/to/your/data:/var/lib/postgresql/data -p 5432:5432 -d postgres:15

Explanation of parameters:

  • --name postgres15: Assigns the name "postgres15" to the container.
  • -e POSTGRES_PASSWORD=your_password: Sets the PostgreSQL superuser password. Replace your_password with a strong password.
  • -v /path/to/your/data:/var/lib/postgresql/data: Mounts the host directory /path/to/your/data to the container’s data directory. This ensures that your data persists even if the container is stopped or removed. Replace /path/to/your/data with an actual path on your Rocky Linux 8 system.
  • -p 5432:5432: Maps port 5432 on the host to port 5432 in the container. This allows you to connect to PostgreSQL from the host system.
  • -d: Runs the container in detached mode (in the background).

After running the container, you can connect to the PostgreSQL instance using psql or any other PostgreSQL client, connecting to localhost on port 5432.

2. Using a Configuration Management Tool (Ansible)

Ansible is a powerful automation tool that allows you to define infrastructure as code. You can create an Ansible playbook to automate the installation and configuration of PostgreSQL 15 on Rocky Linux 8. This approach offers:

  • Idempotency: Ansible ensures that the desired state is achieved, even if the playbook is run multiple times.
  • Automation: Eliminates manual steps, reducing the risk of errors.
  • Scalability: Easily deploy PostgreSQL to multiple servers.

Explanation:

This approach involves creating an Ansible playbook that defines the steps required to install and configure PostgreSQL 15. The playbook typically includes tasks to: add the PostgreSQL repository, install the postgresql15-server package, initialize the database, configure authentication, and start the PostgreSQL service.

Code Example:

First, install Ansible on your control machine (if you haven’t already):

sudo dnf install ansible -y

Create a file named postgresql_install.yml with the following content:

---
- hosts: all
  become: true
  tasks:
    - name: Add PostgreSQL 15 repository
      command: dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
      register: repo_add
      ignore_errors: true

    - name: Update package cache
      command: dnf update -y
      when: repo_add.changed

    - name: Disable default PostgreSQL module
      command: dnf -qy module disable postgresql
      ignore_errors: true

    - name: Install PostgreSQL 15 server
      dnf:
        name: postgresql15-server
        state: present

    - name: Initialize PostgreSQL 15 database
      command: /usr/pgsql-15/bin/postgresql-15-setup initdb
      args:
        creates: /var/lib/pgsql/15/data/postgresql.conf # Check if already initializated

    - name: Enable and start PostgreSQL 15 service
      systemd:
        name: postgresql-15
        state: started
        enabled: yes

Explanation:

  • hosts: all: This indicates that the playbook will be executed on all hosts defined in your Ansible inventory.
  • become: true: This allows the playbook to run commands with elevated privileges (using sudo).
  • tasks: This section defines the individual tasks that will be executed.
  • dnf: Uses the DNF module to install and manage packages.
  • systemd: Uses the Systemd module to manage services.
  • creates: Check if initdb needs to be executed.

To run the playbook, use the following command:

ansible-playbook postgresql_install.yml -i "your_target_host," -u your_remote_user -k

Important considerations for the Ansible method:

  • Replace "your_target_host," with the IP address or hostname of your Rocky Linux 8 server in the Ansible inventory. Ensure you have SSH access to the target host.
  • Replace your_remote_user with the username you use to connect to the Rocky Linux 8 server.
  • The -k flag prompts you for the SSH password. You can also use SSH keys for authentication.
  • This playbook provides a basic installation. You might need to add additional tasks to configure authentication, networking, and other settings according to your requirements.

Both of these alternative methods offer different approaches to installing PostgreSQL 15 on Rocky Linux 8, providing flexibility based on your specific needs and environment. Choosing the right method depends on factors such as your familiarity with Docker or Ansible, the level of isolation required, and the need for automation and scalability.