Installing PostgreSQL on Rocky Linux

 Install the latest version of PostgreSQL from the repository using the dnf command below.

dnf install postgresql-server.x86_64


PostgreSQL Database Initialization

Next, after the PostgreSQL installation is complete, you must initialize the PostgreSQL configuration and then start and enable the PostgreSQL service.

1. Execute the following command to initialize the PostgreSQL database configuration.

postgresql-setup --initdb --unit postgresql

2. After that, start and enable the PostgreSQL service using the command below.

sudo systemctl enable postgresql
sudo systemctl start postgresql

Now the PostgreSQL service is active and running, and it will run automatically on every boot.

3. Now execute the command below to verify the PostgreSQL service.

systemctl status postgresql

If your PostgreSQL service is running, you will see the green output such as "active(running)" as below. Otherwise, you will see the red output such as "failed" following by the error message logs.


Securing PostgreSQL Deployment

During the installation, PostgreSQL will create a new system user and database user name as "postgres". And for this stage, you will be setting up a new password for the "postgres" user, both for the system user and database user.

1. Change the password for default system user "postgres" using the following command.

passwd postgres

Now type the new password for the system user "postgres".

2. Next, to change the password for the "postgres" database user, you must log in to the PostgreSQL shell.

First, log in as a system user "postgres" using the following command.

su - postgres

Now login to the PostgreSQL shell using the psql command below.


Execute the following query to create a new password for the default "postgres" database user.

ALTER USER postgres WITH PASSWORD 'strongpostgrespassword';

Change the string 'strongpostgrespassword' to your own password. Now type exit and press "Ctrl+d" to exit and log out from the 'postgres' user shell.