How To Install PostgreSQL on CentOS 8 / Rocky Linux 8 / RHEL 8
PostgreSQL is a free, open-source object-relational database management system (ORDBMS) available for various platforms, including Linux, Microsoft Windows, and Mac OS X.
PostgreSQL is developed by the PGDG (PostgreSQL Global Development Group) and released under the PostgreSQL License, a free and open-source software.
In this post, we will see how to install PostgreSQL on CentOS 8 / Rocky Linux 8 / RHEL 8.
Install PostgreSQL on CentOS 8 / Rocky Linux 8 / RHEL 8
You can obtain PostgreSQL packages for CentOS 8 / Rocky Linux 8 / RHEL 8 in two ways.
Install PostgreSQL from Official PostgreSQL Repository
PostgreSQL Community offers PostgreSQL packages for CentOS 8 / Rocky Linux 8 / RHEL 8. Packages provided by PostgreSQL is always fresh and supported by the PostgreSQL community.
Add PostgreSQL Repository
First, install the PostgreSQL repository configuration on your system, as shown below.
dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Install PostgreSQL
Then, install the PostgreSQL server using the dnf
command by mentioning its version postgresql<VERSION>-server
. For this demo, I am installing PostgreSQL 14.
dnf -qy module disable postgresql dnf install -y postgresql14-server
After installing PostgreSQL, initialize the database before using it for the first time.
/usr/pgsql-14/bin/postgresql-14-setup initdb
Output:
Initializing database ... OK
PostgreSQL data is typically found /var/lib/pgsql/14/data/ directory.
Manage 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 the localhost. Therefore, only applications running on the server can connect to the database server by default and restrict external applications from connecting to the database.
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 on all network interfaces using the netstat command.
netstat -antup | grep 5432
Output:
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 4156/postmaster
tcp6 0 0 :::5432 :::* LISTEN 4156/postmaster
Install PostgreSQL from AppStream Repository
dnf module list postgresql
Output:
Name Stream Profiles Summary postgresql 9.6 client, server [d] PostgreSQL server and client module postgresql 10 [d] client, server [d] PostgreSQL server and client module postgresql 12 client, server [d] PostgreSQL server and client module postgresql 13 client, server [d] PostgreSQL server and client module Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
Install PostgreSQL
Install PostgreSQL using the dnf
command. You can install another version with postgresql:stream
in the dnf command.
# PostgreSQL 10 dnf install -y @postgresql # PostgreSQL 13 dnf install -y @postgresql:13
After installing PostgreSQL, initialize the database before using it for the first time.
/usr/bin/postgresql-setup --initdb
Output:
* Initializing database in '/var/lib/pgsql/data' * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
PostgreSQL data is typically found /var/lib/pgsql/data directory.
Manage PostgreSQL Service
To start the PostgreSQL service, run:
systemctl start postgresql
To enable PostgreSQL on system startup, run:
systemctl enable postgresql
To check the status of the PostgreSQL service, run:
systemctl status postgresql
Configure PostgreSQL Server
By default, PostgreSQL listens on the localhost. Therefore, only applications running on the server can connect to the database server by default and restrict external applications from connecting to the database.
Edit the configuration file to enable the database service access for external machines.
vi /var/lib/pgsql/data/postgresql.conf
Set the listen_addresses to *.
listen_addresses = '*'
Restart PostgreSQL service.
systemctl restart postgresql
Confirm the PostgreSQL listening on port 5432 on all network interfaces using the netstat command.
netstat -antup | grep 5432
Output:
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 4156/postmaster
tcp6 0 0 :::5432 :::* LISTEN 4156/postmaster
Access PostgreSQL server
To access or work with the database, log in as a postgres (Linux user) user and the execute psql
command.
su -l postgres
Access the database using the psql command.
$ psql
Output:
psql (13.5) 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 8 / Rocky Linux 8 / RHEL 8.