Databases 4 min read

How to Install PostgreSQL 14 on CentOS Using a Script

This guide walks through installing PostgreSQL 14 on a CentOS server via a shell script, covering repository setup, package installation, initialization, user and database creation, remote‑connection configuration, firewall adjustments, service verification, and connecting with Navicat.

The Dominant Programmer
The Dominant Programmer
The Dominant Programmer
How to Install PostgreSQL 14 on CentOS Using a Script

The article explains how to install PostgreSQL 14 on a CentOS system by creating and executing a shell script that automates the required steps.

1. Generate the installation script

Download the PostgreSQL repository RPM and install the server package, then initialize the database and enable the service:

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql14-server
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
sudo systemctl enable postgresql-14
sudo systemctl start postgresql-14

2. Create the script file

Create a file named postgresqlInstall.sh, paste the commands above, make it executable, and run it:

touch postgresqlInstall.sh
vi postgresqlInstall.sh   # paste the commands
chmod 755 postgresqlInstall.sh
./postgresqlInstall.sh

3. Create a user and a database

Switch to the automatically created postgres system user and launch psql to create a new database user, a database, and grant privileges:

su postgres
psql
create user badao with password '123456';
create database test_db owner badao;
grant all privileges on database test_db to badao;
\q

4. Enable remote connections

Edit /var/lib/pgsql/14/data/postgresql.conf to set listen_addresses = '*', then modify /var/lib/pgsql/14/data/pg_hba.conf to allow trusted IPv4 connections:

# In postgresql.conf
listen_addresses = '*'
# In pg_hba.conf (add the line)
host all all 0.0.0.0/0 trust

Restart the service to apply the changes:

systemctl restart postgresql-14

5. Open the default port in the firewall

firewall-cmd --add-port=5432/tcp --permanent
firewall-cmd --reload

6. Verify service status

systemctl status postgresql-14.service

7. Connect remotely with Navicat

Use Navicat or any compatible client to connect to the server’s IP address on port 5432 with the credentials created earlier.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DatabasePostgreSQLInstallationCentOSShell scriptRemote access
The Dominant Programmer
Written by

The Dominant Programmer

Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.