PostgreSQL is a object-relational, open source database system that can be used for both small and large-scale projects; from simple websites and intranet applications to complex, ‘big data’ projects.
When a new server is installed, PostgreSQL’ s settings are very minimum as they are designed to run on the least amount of hardware possible.
In a survey conducted by Statista in August 2022, PostgreSQL is ranking second (just behind MySQL), in terms of the most used database technologies available. The main reason for PostgreSQL’s popularity, in addition to being open source, is the fact that it requires very minimum requirements on the hardware it will be installed at.
In this tutorial we will guide you through the simple steps of installing PostgreSQL on AlmaLinux 9 Operating system.
Install PostgreSQL on AlmaLinux 9
Before going through the rest of the article on how to install PostgreSQL on AlmaLinux 9 Server, please make sure you check the pre-requisites.
- VPS or Dedicated Server with AlmaLinux 9 Operating System
- Access on the server with a root or sudo privileges user
Step 1 – Update AlmaLinux 9 Server
Once you have access your newly installed AlmaLinux 9 server, update the packages to the latest versions available by executing the following command:
root@almalinux-server:~$ dnf update && dnf upgrade -y
Step 2 – Install PostgreSQL
At the time of writing this article (December 2022) the latest stable PostgreSQL version is 15.0. This version is not available, yet, in the default repo of AlmaLinux 9, so type the following command to install the official PostgreSQL repository in our system:
root@almalinux-server:~$ dnf install -y sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Then, update the index of your server’s packages with the following command:
root@almalinux-server:~$ dnf update -y
Now, you can install the PostgreSQL server with the following command:
root@almalinux-server:~$ dnf install postgresql15 postgresql15-server
As soon as the installation completes, execute the following command to initialize the newly installed database:
root@almalinux-server:~$ /usr/pgsql-15/bin/postgresql-15-setup initdb
Initializing database … OK
Once database has been initialized, enter the following commands to start the PostgreSQL service and automatically enable it on startup:
root@almalinux-server:~$ systemctl start postgresql-15 && systemctl enable postgresql-15
To verify that the service has been started without errors, enter the following command:
root@almalinux-server:~$ systemctl status postgresql-15
postgresql-15.service – PostgreSQL 14 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-15.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2022-12-02 21:03:15 CEST; 1min 25s ago
Step 3 – Set or Change the PostgreSQL User Password
Upon successful installation and first startup of the PostgreSQL database service, you can change the default user password for PostgreSQL as follows:
root@almalinux-server:~$ passwd postgres
You will be prompted to set a new password and then to re-type it.
Changing password for user postgres. New password: Retype new password: passwd: all authentication tokens updated successfully.
Step 4 (optional) – Allow Remote Access to PostgreSQL Database
By default, PostgreSQL is configured to listen to local connections only.
If you need to access the database remotely (e.g. from your computer or from a software in another server) then do the following:
- Open the file /var/lib/pgsql/15/data/postgresql.conf
- Replace the line listen_addresses = ‘localhost‘ with listen_addresses = ‘*“
- Open the file /var/lib/pgsql/15/data/pg_hba.conf
- Add this entry at the end of the file: host all all 0.0.0.0/0 md5
- Save and close the file
For the changes to take effect restart the PostgreSQL service as follows:
root@almalinux-server:~$ systemctl restart postgresql-15
This is it! You have successfully installed PostgreSQL 15 on AlmaLinux 9 Server. Enjoy!