This guide will walk you through the steps used to install PostgreSQL 12 on CentOS 7 / CentOS 8 Linux server?. PostgreSQL is an object-relational database management system based on POSTGRES 4.2. PostgreSQL 12 is available for Production use by Developers and Database Administrators.
PostgreSQL project provides a repository of packages of all supported versions for the most common distributions. Among the distributions supported are all Red Hat family of which includes CentOS, Fedora, Scientific Linux, Oracle Linux and Red Hat Enterprise Linux.
For Ubuntu users, check: Install PostgreSQL 12 on Ubuntu
Use the steps below to install PostgreSQL 12 on CentOS 8 / CentOS 7.
Step 1: Add PostgreSQL Yum Repository
The PostgreSQL Yum Repository will integrate with your normal systems
and patch management, and provide automatic updates for all supported
versions of PostgreSQL throughout the support lifetime of PostgreSQL.
It can be added to CentOS system by running the command below:
CentOS 8:
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
CentOS 7:
sudo yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
You can get more information on installed package by running the command:
$ rpm -qi pgdg-redhat-repo
rpm -qi pgdg-redhat-repo
Name : pgdg-redhat-repo
Version : 42.0
Release : 32
Architecture: noarch
Install Date: Mon 21 Aug 2023 09:07:48 AM UTC
Group : Unspecified
Size : 14041
License : PostgreSQL
Signature : DSA/SHA1, Mon 14 Aug 2023 03:56:52 PM UTC, Key ID 1f16d2e1442df0f8
....
Step 2: Install PostgreSQL 12 on CentOS 8 / CentOS 7
With the YUM repository added, we can install PostgreSQL 12 on CentOS 7/8 with the following command.
PostgreSQL 12 on CentOS 8
Disable the built-in PostgreSQL module:
sudo dnf -qy module disable postgresql
Then Install both client and server packages:
sudo dnf install -y postgresql12-server postgresql12
PostgreSQL 12 on CentOS 7
Install PostgreSQL client and server packages:
sudo yum -y install epel-release yum-utils
sudo yum-config-manager --enable pgdg12
sudo yum install postgresql12-server postgresql12
Sample installation output:
...
Transaction Summary
======================================================================================================================================================================================================
Install 2 Packages (+2 Dependent packages)
Total download size: 14 M
Installed size: 54 M
Is this ok [y/d/N]: y
Step 3: Initialize and start database service
After installation, database initialization is required before service can be started.
sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
The database main configuration ifile is written to: /var/lib/pgsql/12/data/postgresql.conf
Start and enable the database server service.
sudo systemctl enable --now postgresql-12
Confirm that the service is started without any errors.
$ systemctl status postgresql-12
● postgresql-12.service - PostgreSQL 12 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2023-05-26 15:53:06 UTC; 4s ago
Docs: https://www.postgresql.org/docs/12/static/
Process: 1869 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 1875 (postmaster)
CGroup: /system.slice/postgresql-12.service
├─1875 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
├─1877 postgres: logger
├─1879 postgres: checkpointer
├─1880 postgres: background writer
├─1881 postgres: walwriter
├─1882 postgres: autovacuum launcher
├─1883 postgres: stats collector
└─1884 postgres: logical replication launcher
May 26 15:53:06 cent7.mylab.io systemd[1]: Starting PostgreSQL 12 database server...
May 26 15:53:06 cent7.mylab.io postmaster[1875]: 2023-05-26 15:53:06.466 UTC [1875] LOG: starting PostgreSQL 12.15 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red H...-44), 64-bit
May 26 15:53:06 cent7.mylab.io postmaster[1875]: 2023-05-26 15:53:06.472 UTC [1875] LOG: listening on IPv6 address "::1", port 5432
May 26 15:53:06 cent7.mylab.io postmaster[1875]: 2023-05-26 15:53:06.473 UTC [1875] LOG: listening on IPv4 address "127.0.0.1", port 5432
May 26 15:53:06 cent7.mylab.io postmaster[1875]: 2023-05-26 15:53:06.478 UTC [1875] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
May 26 15:53:06 cent7.mylab.io postmaster[1875]: 2023-05-26 15:53:06.483 UTC [1875] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
May 26 15:53:06 cent7.mylab.io postmaster[1875]: 2023-05-26 15:53:06.512 UTC [1875] LOG: redirecting log output to logging collector process
May 26 15:53:06 cent7.mylab.io postmaster[1875]: 2023-05-26 15:53:06.512 UTC [1875] HINT: Future log output will appear in directory "log".
May 26 15:53:06 cent7.mylab.io systemd[1]: Started PostgreSQL 12 database server.
If you have a running Firewall service and remote clients should connect to your database server, allow PostgreSQL service.
sudo firewall-cmd --add-service=postgresql --permanent
sudo firewall-cmd --reload
Step 4: Set PostgreSQL admin user’s password
Set PostgreSQL admin user
$ sudo su - postgres
~]$ psql -c "alter user postgres with password 'StrongPassword'"
ALTER ROLE
Step 5: Enable remote access (Optional)
Edit the file /var/lib/pgsql/12/data/postgresql.conf
and set Listen address to your server IP address or “*” for all interfaces.
listen_addresses = '192.168.10.10'
Also set PostgreSQL to accept remote connections
$ sudo vim /var/lib/pgsql/12/data/pg_hba.conf
# Accept from anywhere
host all all 0.0.0.0/0 md5
# Accept from trusted subnet
host all all 192.168.18.0/24 md5
Restart database service after committing the change.
sudo systemctl restart postgresql-12
Connecting to remote database:
$ psql -U <dbuser> -h <serverip> -p 5432 <dbname>
Step 6: Install pgAdmin 4 Web interface
pgAdmin is the leading Open Source feature-rich PostgreSQL administration and development platform that runs on Linux, Unix, Mac OS X, and Windows. Here is the link for the installation of pgAdmin4 on CentOS.
PostgreSQL 12 has been installed on CentOS 7 / CentOS 8 Linux system. Spare some minutes to go through PostgreSQL 12 documentation.
Recommended books:
More: