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 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-142. 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.sh3. 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;
\q4. 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 trustRestart the service to apply the changes:
systemctl restart postgresql-145. Open the default port in the firewall
firewall-cmd --add-port=5432/tcp --permanent
firewall-cmd --reload6. Verify service status
systemctl status postgresql-14.service7. 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
