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 / RHEL 8.
THIS DOCUMENT IS ALSO AVAILABLE FOR
Install PostgreSQL on CentOS 8 / RHEL 8
Contents
You can obtain PostgreSQL packages for CentOS 8 / RHEL 8 in two ways.
Install PostgreSQL from AppStream Repository
PostgreSQL is now available for RHEL 8 from Red Hat’s Application Stream (rhel-8-for-x86_64-appstream-rpms) and CentOS 8 from AppStream repository.
At this moment, only PostgreSQL 10 and PostgreSQL 9.6 are available from the AppStream repository.
[root@centos8 ~]# yum module list postgresql Last metadata expiration check: 0:05:18 ago on Wed 25 Sep 2019 06:04:22 AM EDT. CentOS-8 - AppStream Name Stream Profiles Summary postgresql 10 [d] client, server [d] PostgreSQL server and client module postgresql 9.6 client, server [d] PostgreSQL server and client module Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
Install PostgreSQL
Install PostgreSQL using yum command.
### PostgreSQL 10 ### yum install -y @postgresql ### PostgreSQL 9.6 ### yum install -y @postgresql:9.6
After installing PostgreSQL, initialize the database before using it for the first time.
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 PostgreSQL service, run:
systemctl start postgresql
To enable PostgreSQL on system startup, run:
systemctl enable postgresql
To check the status of PostgreSQL service, run:
systemctl status postgresql
Output:
Configure PostgreSQL Server
By default, PostgreSQL listens on the localhost. Only applications running on the server itself can connect to the database server by default and restricts external applications from connecting to the database.
To enable the database service access for external machines, edit the configuration file.
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 10867/postmaster
tcp6 0 0 :::5432 :::* LISTEN 10867/postmaster
Install PostgreSQL from Official PostgreSQL Repository
PostgreSQL Community offers PostgreSQL packages for CentOS 8 / RHEL 8. Packages provided by PostgreSQL is always fresh and supported by the PostgreSQL community.
Add PostgreSQL Repository
Install the PostgreSQL repository configuration on your system with the below command.
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Install PostgreSQL
Install the PostgreSQL server using yum command by mentioning its version postgresql<VERSION>-server. For this demo, I am installing PostgreSQL 12.
We need to disable the rhel-8-for-x86_64-appstream-rpms and AppStream repository temporarily on RHEL 8 and CentOS 8 respectively to allow yum to download packages from PostgreSQL mirror.
### CentOS 8 ### yum install -y python2-libs yum --disablerepo=AppStream install -y postgresql12-server postgresql12 ### RHEL 8 ### yum --disablerepo=rhel-8-for-x86_64-appstream-rpms install -y postgresql12-server postgresql12
After installing PostgreSQL, initialize the database before using it for the first time.
/usr/pgsql-12/bin/postgresql-12-setup initdb
Output:
Initializing database ... OK
PostgreSQL data is typically found /var/lib/pgsql/12/data/ directory.
Manage PostgreSQL Service
To start PostgreSQL service, run:
systemctl start postgresql-12
To enable PostgreSQL on system startup, run:
systemctl enable postgresql-12
To check the status of PostgreSQL service, run:
systemctl status postgresql-12
Output:
Configure PostgreSQL Server
By default, PostgreSQL listens on the localhost. Only applications running on the server itself can connect to the database server by default and restricts external applications from connecting to the database.
To enable the database service access for external machines, edit the configuration file.
vi /var/lib/pgsql/12/data/postgresql.conf
Set the listen_addresses to *.
listen_addresses = '*'
Restart PostgreSQL service.
systemctl restart postgresql-12
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 postgres (Linux user) user. Login from the root user or reset the password of postgres user for login.
# su -l postgres
Access the database using the psql command.
$ psql
Output:
[postgres@centos8 ~]$ psql psql (12.0) Type "help" for help. postgres=#
Set password for postgres (Database administrator) user.
postgres=# password
Conclusion
In this post, you have learned how to install PostgreSQL on CentOS 8 / RHEL 8. Please share your feedback in the comments section.