Install PostgreSQL 15 on AlmaLinux 9 | Full and Easy Setup

Posted on

Install PostgreSQL 15 on AlmaLinux 9 | Full and Easy Setup

Install PostgreSQL 15 on AlmaLinux 9 | Full and Easy Setup

This guide aims to teach you How To Install PostgreSQL 15 on AlmaLinux 9. The PostgreSQL Global Development Group announced on October 13 the release of PostgreSQL 15, which builds on the performance improvements of recent versions with notable gains for managing workloads in local and distributed deployments, including improved sorting. This is key when you want to Install PostgreSQL 15 on AlmaLinux 9.

This release improves the developer experience by adding the famous MERGE command and more capabilities to observe the state of the database.

You can now proceed to the following steps provided by the Orcacore website to Install PostgreSQL 15 on AlmaLinux 9.

Steps To Install and Configure PostgreSQL 15 on AlmaLinux 9

To Install PostgreSQL 15 on AlmaLinux 9, you must log in to your server as a non-root user with sudo privileges. To do this, follow our guide the Initial Server Setup with AlmaLinux 9.

Now follow the steps below to Install PostgreSQL 15 on AlmaLinux 9.

Step 1. Set up PostgreSQL 15 on AlmaLinux 9

To Install PostgreSQL 15 on AlmaLinux 9, you must add the PostgreSQL 15 repository on your server.

Add PostgreSQL 15 Repository on AlmaLinux 9

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-9-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 AlmaLinux 9

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 ALmaLinux 9:

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:

**Output**
psql (PostgreSQL) 15.0

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:

Step 2. Manage PostgreSQL 15 Server

At this step of install PostgreSQL 15 on AlmaLinux 9, you can start and enable your PostgreSQL 15. 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

Step 3. Configure PostgreSQL 15 on AlmaLinux 9

At this step, you have learned to Install PostgreSQL 15 on AlmaLinux 9. Now you can start to configure it. 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 AlmaLinux 9 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 AlmaLinux 9, 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 AlmaLinux 9 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 AlmaLinux 9:

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 AlmaLinux 9:

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 AlmaLinux 9, 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 PostgreSQL 15 on AlmaLinux 9. By following the guide steps, you can easily add the PostgreSQL repository and start to set up your PostgreSQL including initializing the database and managing your databases.

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

Efficiently Check For Security Updates on AlmaLinux 9

Install a Specific Version of PostgreSQL on Ubuntu 20.04

PostgreSQL Setup on Ubuntu 24.04

Install PostgreSQL 15 on Ubuntu 22.04

Install PostgreSQL 14 on Rocky Linux 8

FAQs

What repositories are required for installing PostgreSQL 15 on AlmaLinux 9?

As we said in the guide steps on install PostgreSQL 15 on AlmaLinux 9, you just need to add the latest PostgreSQL repository on your server.

How do I start and enable PostgreSQL 15 service on AlmaLinux 9?

You can easily start and enable your service with the following commands:
sudo systemctl enable postgresql-15
sudo systemctl start postgresql-15

How can I upgrade PostgreSQL to version 15 on AlmaLinux 9?

As described in the guide steps above on Install PostgreSQL 15 on AlmaLinux 9, you can easily upgrade and install your service.

Alternative Solutions for Installing PostgreSQL 15 on AlmaLinux 9

While the provided method is a straightforward approach to Install PostgreSQL 15 on AlmaLinux 9 using the official PostgreSQL repositories, alternative methods exist that may be preferable in certain scenarios. These include using containerization with Docker and leveraging Infrastructure as Code (IaC) tools like Ansible.

1. Using Docker to Install PostgreSQL 15 on AlmaLinux 9

Docker offers a containerized solution, allowing you to run PostgreSQL 15 in an isolated environment. This approach simplifies dependency management, promotes consistency across different environments, and makes it easier to manage multiple PostgreSQL instances.

Explanation:

  • Isolation: Docker containers isolate PostgreSQL from the host system, preventing conflicts with other software.
  • Reproducibility: A Dockerfile defines the exact configuration of the PostgreSQL environment, ensuring that the same environment can be easily recreated on any system.
  • Scalability: Docker containers can be easily scaled up or down as needed.

Steps:

  1. Install Docker: If Docker is not already installed on your AlmaLinux 9 system, install it using the following commands:

    sudo dnf install -y docker
    sudo systemctl start docker
    sudo systemctl enable docker
  2. Pull the PostgreSQL 15 Docker image: Pull the official PostgreSQL 15 Docker image from Docker Hub:

    sudo docker pull postgres:15
  3. Run the PostgreSQL 15 Docker container: Run the container with the following command, replacing <your_password> with a strong password:

    sudo docker run --name postgres15 -e POSTGRES_PASSWORD=<your_password> -p 5432:5432 -d postgres:15
    • --name postgres15: Assigns the name "postgres15" to the container.
    • -e POSTGRES_PASSWORD=<your_password>: Sets the PostgreSQL superuser password.
    • -p 5432:5432: Maps port 5432 on the host to port 5432 in the container.
    • -d: Runs the container in detached mode (in the background).
  4. Access the PostgreSQL 15 instance: You can now access the PostgreSQL 15 instance using any PostgreSQL client, such as psql. Connect to localhost on port 5432 with the username postgres and the password you set in the previous step.

    psql -h localhost -p 5432 -U postgres -W

    You’ll be prompted for the password.

Code Example (Dockerfile – Optional for customized configurations):

While using the official image is generally recommended, you can create a custom Dockerfile to further customize the PostgreSQL environment. Here’s a basic example:

FROM postgres:15

# Set environment variables
ENV POSTGRES_PASSWORD=mysecretpassword
ENV POSTGRES_USER=myuser
ENV POSTGRES_DB=mydb

# Optionally copy initialization scripts
COPY init.sql /docker-entrypoint-initdb.d/

# Expose the PostgreSQL port
EXPOSE 5432

Explanation of Dockerfile elements:

  • FROM postgres:15: Specifies the base image.
  • ENV: Sets environment variables.
  • COPY init.sql /docker-entrypoint-initdb.d/: Copies a SQL script (e.g., init.sql) to the special directory /docker-entrypoint-initdb.d/. Any .sql scripts in this directory are automatically executed when the container is first started, allowing you to initialize the database schema, load data, etc.

2. Using Ansible to Install PostgreSQL 15 on AlmaLinux 9

Ansible is an Infrastructure as Code (IaC) tool that allows you to automate the installation and configuration of software across multiple servers. This approach is ideal for managing PostgreSQL deployments at scale, ensuring consistency, and simplifying updates.

Explanation:

  • Automation: Ansible automates the entire installation and configuration process, reducing manual effort and the risk of errors.
  • Idempotency: Ansible playbooks are idempotent, meaning that they can be run multiple times without causing unintended changes.
  • Centralized Management: Ansible allows you to manage PostgreSQL deployments across multiple servers from a central location.

Steps:

  1. Install Ansible: If Ansible is not already installed on your AlmaLinux 9 system, install it using the following commands:

    sudo dnf install -y ansible
  2. Create an Ansible playbook: Create an Ansible playbook (e.g., postgresql15_install.yml) with the following content, replacing <your_password> with a strong password and adjusting the hosts section to match your target server(s):

    ---
    - hosts: all
      become: true
      tasks:
        - name: Add PostgreSQL 15 repository
          dnf:
            name: https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
            state: present
    
        - 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 for this file to ensure initialization only happens once
    
        - name: Enable and start PostgreSQL 15 service
          systemd:
            name: postgresql-15
            enabled: true
            state: started
    
        - name: Set PostgreSQL password for postgres user
          become: true
          become_user: postgres
          postgresql_user:
            db: postgres
            name: postgres
            password: "<your_password>"
            priv: ALL
            state: present
  3. Run the Ansible playbook: Run the playbook using the following command:

    ansible-playbook postgresql15_install.yml

Explanation of Playbook tasks:

  • Add PostgreSQL 15 repository: Adds the PostgreSQL 15 YUM repository to the system.
  • Disable default PostgreSQL module: Disables any default PostgreSQL modules that might conflict with the installation.
  • Install PostgreSQL 15 server: Installs the postgresql15-server package.
  • Initialize PostgreSQL 15 database: Initializes the PostgreSQL database cluster. The creates argument ensures that this task is only executed once.
  • Enable and start PostgreSQL 15 service: Enables the postgresql-15 service to start automatically at boot and starts the service immediately.
  • Set PostgreSQL password for postgres user: Uses the postgresql_user module to set a password for the postgres user within PostgreSQL. This requires the libpq-devel package to be installed on the Ansible control node. You may need to install the community.postgresql collection. See Ansible documentation for details.

These alternative solutions provide flexibility and scalability for deploying PostgreSQL 15 on AlmaLinux 9, catering to different needs and environments. While the original method is sufficient for simple setups, Docker and Ansible offer advanced features for managing PostgreSQL deployments in more complex scenarios. Always choose the method that best aligns with your specific requirements and expertise.