Installing PostgreSQL on Linux 

Check Redhat version

[root@ip-xxx-xx-xx-xx ~]# cat /etc/redhat-release

Red Hat Enterprise Linux release 9.0 (Plow)


Visit  https://www.postgresql.org/download/linux/redhat/ and select version, platform and architecture to install PostgreSQL 

[root@ip-xxx-xx-xx-xx ~]# useradd postgres

[root@ip-xxx-xx-xx-xx ~]# passwd postgres

Changing password for user postgres.

New password:

BAD PASSWORD: The password contains the user name in some form

Retype new password:

passwd: all authentication tokens updated successfully.

[root@ip-xxx-xx-xx-xx ~]# sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm

[root@ip-xxx-xx-xx-xx ~]# sudo dnf install -y postgresql15-server postgresql15-contrib

Initialize the database and enable automatic start:

[root@ip-xxx-xx-xx-xx ~]#  sudo /usr/pgsql-15/bin/postgresql-15-setup initdb

Initializing database ... OK

[root@ip-xxx-xx-xx-xx ~]# sudo systemctl enable postgresql-15

Created symlink /etc/systemd/system/multi-user.target.wants/postgresql-15.service → /usr/lib/systemd/system/postgresql-15.service.

[root@ip-xxx-xx-xx-xx ~]# sudo systemctl start postgresql-15

Post Installation Configuration:
Connect to the default database using psql and change password of the superuser postgres

[postgres@ip-xxx-xx-xx-xx ~]$ /usr/pgsql-15/bin/psql -d postgres -U postgres

psql (15.2)

Type "help" for help.

 postgres=# ALTER USER postgres PASSWORD 'mypostgres';

ALTER ROLE

postgres-# \q


Change the authentication method to md5 in pg_hba.conf file and reload the server. Change local from "peer" to "scram-sha-256"

# vi /var/lib/pgsql/15/data/pg_hba.conf

# /usr/pgsql-15/bin/pg_ctl -D /var/lib/pgsql/15/data/ reload

server signaled

# /usr/pgsql-15/bin/psql -d postgres -U postgres

Password for user postgres:

psql (15.2)

Type "help" for help.

 postgres=#


Setting Environmental Variables

Setting environment variables is very important for trouble free startup/shutdown of the database server

•PATH –should point to correct bin directory

•PGDATA –should point to correct data cluster directory

•PGPORT –should point to correct port on which database cluster is running

•PGUSER –specifies the default database user name

•PGDATABASE –specify the default database

•PGPASSWORD –specify default password

•Edit .profile or .bash_profileto set the variables

•In Windows set these variables using my computer properties page


[postgres@ip-xxx-xx-xx-xx ~]$ vi .bash_profile

PATH=/usr/pgsql-15/bin/:$PATH:$HOME/.local/bin:$HOME/bin

 export PATH

export PGDATA=/var/lib/pgsql/15/data/

export PGUSER=postgres

export PGPORT=5432

export PGDATABASE=postgres

 

[postgres@ip-xxx-xx-xx-xx ~]$ source .bash_profile

[postgres@ip-xxx-xx-xx-xx ~]$ which psql

/usr/pgsql-15/bin/psql

 

[postgres@ip-xxx-xx-xx-xx ~]$ psql

Password for user postgres:

psql (15.2)

Type "help" for help.

 postgres=#

 

[postgres@ip-xxx-xx-xx-xx ~]$ pg_ctl status

pg_ctl: server is running (PID: 12221)

/usr/pgsql-15/bin/postgres "-D" "/var/lib/pgsql/15/data/"