How to Install PostgreSQL on CentOS 7 / RHEL 7

6

PostgreSQL is an object-relational database management system (ORDBMS) available for many platforms, including Linux, FreeBSD, Solaris, Microsoft Windows, and macOS.

It is released under the PostgreSQL License.

PostgreSQL is developed by the PostgreSQL Global Development Group, consisting of a handful of community volunteers employed and supervised by Red Hat and EnterpriseDB.

The vast majority of Linux distributions have PostgreSQL available in supplied packages.

Here, we will see how to install PostgreSQL on CentOS 7 / RHEL 7.

Setup PostgreSQL repository

PostgreSQL publishes rpm packages for all Linux platforms, and their packages are fresher than those available in the OS repositories.

So, you need to add the repository to your machine by installing PostgreSQL repo rpm.

yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

Install PostgreSQL on CentOS 7 / RHEL 7

At the time of writing this post, PostgreSQL v14,13,12,11, and 10 are available for CentOS 7 / RHEL 7. You can install a specific version of PostgreSQL mentioning postgresql<version>-server in the yum command.

For example, to install PostgreSQL 14, use the below command.

yum install -y postgresql14-server

Initialize PostgreSQL Server

After installing PostgreSQL, you need to initialize it before using it for the first time. Change the below command based on the PostgreSQL version like /usr/pgsql-<version>/bin/postgresql-<version>-setup initdb.

/usr/pgsql-14/bin/postgresql-14-setup initdb

PostgreSQL data is typically found /var/lib/pgsql/<version>/data/ directory.

Control PostgreSQL Service

To start PostgreSQL service, run:

systemctl start postgresql-14

To enable PostgreSQL on system startup, run:

systemctl enable postgresql-14

To check the status of PostgreSQL service, run:

systemctl status postgresql-14

Configure PostgreSQL Server

By default, PostgreSQL listens on localhost, which means you can access the database from the server itself and won’t connect to the database from outside the network.

Edit the configuration file to enable the database service access for external machines.

vi /var/lib/pgsql/14/data/postgresql.conf

Set the listen_addresses to *.

listen_addresses = '*'

Restart PostgreSQL service.

systemctl restart postgresql-14

Confirm the PostgreSQL listening on port 5432 using the netstat command.

netstat -antup | grep 5432

Output:

tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      1969/postmaster
tcp6       0      0 :::5432                 :::*                    LISTEN      1969/postmaster

READ: netstat command not found on CentOS 7 / RHEL 7 – Quick Fix

Access PostgreSQL server

To create a database, log in as postgres (Linux user). Login from the root user or reset the postgres user’s password for login.

# su -l postgres

Access the database using the psql command. It is an interactive front-end terminal for the PostgreSQL database.

$ psql

Output:

-bash-4.2$ psql
psql (14.1)
Type "help" for help.

postgres=#

Set password for postgres (Database administrator) user.

postgres=# \password

Conclusion

That’s All. I hope you have learned how to install PostgreSQL on CentOS 7 / RHEL 7.

You might also like