How To Install PostgreSQL 15 on AlmaLinux 8 | Full Steps

Posted on

How To Install PostgreSQL 15 on AlmaLinux 8 | Full Steps

How To Install PostgreSQL 15 on AlmaLinux 8 | Full Steps

In this guide on the Orcacore website, we intend to teach you How To Install PostgreSQL 15 on AlmaLinux 8. PostgreSQL 15 comes with new features that can help developers code easily. The new version, for example, comes with a SQL standard MERGE command that allows developers to write conditional SQL statements including INSERT, UPDATE, and DELETE actions within a single statement. Understanding How To Install PostgreSQL 15 on AlmaLinux 8 is crucial for developers seeking to leverage these advancements.

PostgreSQL 15 also provides additional flexibility for managing logical replication, the development group said, adding that the new version adds row filtering and column lists for publishers. This allows developers to choose to replicate a specific subset of data from a table, it added. Learning How To Install PostgreSQL 15 on AlmaLinux 8 is your first step towards utilizing these enhancements.

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 AlmaLinux 8.

Set up PostgreSQL 15 on AlmaLinux 8

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

Add PostgreSQL 15 Repository on AlmaLinux 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 AlmaLinux 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:

**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:

**Output**
Initializing database ... OK

Start and Enable PostgreSQL 15 Service

At this point, you can start and enable your PostgreSQL 15 on AlmaLinux 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
Start and Enable PostgreSQL 15 Service

Connect To PostgreSQL 15 Shell on AlmaLinux 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 is able to 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 8 by running the following command:

postgres@sam:~$ psql

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

access the Postgres 15 shell on AlmaLinux 8

To exit from the PostgreSQL 15 shell on AlmaLinux 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 AlmaLinux 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 AlmaLinux 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 AlmaLinux 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 AlmaLinux 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 AlmaLinux 8. Installing PostgreSQL 15 on AlmaLinux 8 allows you to manage and store relational database data efficiently. It provides enhanced performance, security features, and support for modern database applications. Properly understanding How To Install PostgreSQL 15 on AlmaLinux 8 allows you to leverage these features.

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

How To Install PostgreSQL on Ubuntu 22.04

How To Set up PostgreSQL on CentOS 7

Set up PostgreSQL on Ubuntu 20.04

Alternative Solutions for Installing PostgreSQL 15 on AlmaLinux 8

While the above method provides a direct and effective way to install PostgreSQL 15, there are alternative approaches that might be suitable depending on specific needs and preferences. Here are two alternative solutions:

1. Using Docker

Docker provides a containerized environment, allowing you to run PostgreSQL 15 in isolation from your host system. This ensures consistency across different environments and simplifies deployment.

Explanation:

Using Docker, you don’t need to manage the PostgreSQL installation directly on your AlmaLinux 8 system. Instead, you use a pre-built PostgreSQL 15 Docker image. Docker handles the dependencies and configuration within the container.

Steps:

  1. Install Docker: If you don’t have Docker installed, 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 image from Docker Hub.

    sudo docker pull postgres:15
  3. Run the PostgreSQL 15 Container: Run the container with necessary environment variables for configuration.

    sudo docker run --name postgres15 -e POSTGRES_PASSWORD=yourpassword -p 5432:5432 -d postgres:15

    Here:

    • --name postgres15: Assigns a name to the container.
    • -e POSTGRES_PASSWORD=yourpassword: Sets the password for the postgres user. Important: Replace yourpassword with a strong password.
    • -p 5432:5432: Maps the container’s port 5432 to the host’s port 5432.
    • -d: Runs the container in detached mode (in the background).
  4. Connect to PostgreSQL: You can now connect to the PostgreSQL 15 instance running in the Docker container using psql or any other PostgreSQL client.

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

    Enter the password you set in the POSTGRES_PASSWORD environment variable.

Code Example (docker-compose.yml):

For a more structured approach, you can use docker-compose. Create a docker-compose.yml file with the following content:

version: "3.8"
services:
  db:
    image: postgres:15
    restart: always
    environment:
      POSTGRES_PASSWORD: yourpassword
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

Then, run:

sudo docker-compose up -d

This creates and starts the PostgreSQL 15 container using the configurations defined in the docker-compose.yml file.

2. Compiling from Source

Another alternative is to compile PostgreSQL 15 directly from the source code. This method offers the greatest control over the installation process and allows for customization.

Explanation:

Compiling from source involves downloading the PostgreSQL 15 source code, configuring the build environment, and then compiling and installing the software. This method gives you precise control over compilation flags, optimization settings, and installation directories.

Steps:

  1. Download the Source Code: Download the PostgreSQL 15 source code from the PostgreSQL website or a mirror.

    wget https://ftp.postgresql.org/pub/source/v15.0/postgresql-15.0.tar.gz
  2. Extract the Archive: Extract the downloaded archive.

    tar -xzf postgresql-15.0.tar.gz
    cd postgresql-15.0
  3. Install Dependencies: Install necessary build dependencies.

    sudo dnf install -y gcc make zlib-devel readline-devel
  4. Configure the Build: Configure the build using the ./configure script. You can specify installation directories and other options.

    ./configure --prefix=/usr/local/pgsql15
  5. Compile and Install: Compile the source code using make and install it.

    make
    sudo make install
  6. Initialize the Database: Create a user and initialize the database.

    sudo useradd postgres
    sudo mkdir -p /usr/local/pgsql15/data
    sudo chown postgres:postgres /usr/local/pgsql15/data
    sudo -u postgres /usr/local/pgsql15/bin/initdb -D /usr/local/pgsql15/data
  7. Start the Server: Start the PostgreSQL server.

    sudo -u postgres /usr/local/pgsql15/bin/pg_ctl -D /usr/local/pgsql15/data -l logfile start

Code Example (Starting PostgreSQL with custom port):

To start PostgreSQL on a different port (e.g., 5433), you can specify the -p option when starting the server:

sudo -u postgres /usr/local/pgsql15/bin/pg_ctl -D /usr/local/pgsql15/data -l logfile -p 5433 start

This will start PostgreSQL on port 5433. Remember to adjust the connection string accordingly when connecting to the database.
These alternative solutions offer different trade-offs in terms of complexity, control, and portability. Docker provides a convenient and isolated environment, while compiling from source allows for maximum customization. Choose the method that best suits your specific requirements and technical expertise.

Leave a Reply

Your email address will not be published. Required fields are marked *