SoFunction
Updated on 2025-03-03

Steps to install PostgreSQL 16 on CentOS 9 Stream

Install PostgreSQL 16 on CentOS 9 Stream

Installing PostgreSQL 16 on CentOS 9 Stream can be done by:

Add the official PostgreSQL repository
PostgreSQL provides an RPM repository that allows you to easily install specific versions.

sudo dnf install -y /pub/repos/yum/reporpms/EL-9-x86_64/

Disable the default PostgreSQL module
CentOS 9 Stream will provide the system's own version of PostgreSQL by default, which needs to be disabled to avoid conflicts.

sudo dnf -qy module disable postgresql

Install PostgreSQL 16
usednf installCommand to install PostgreSQL 16.

sudo dnf install -y postgresql16 postgresql16-server

Initialize the database
Before starting PostgreSQL for the first time, the database needs to be initialized.

sudo /usr/pgsql-16/bin/postgresql-16-setup initdb

Start and enable PostgreSQL service
Set PostgreSQL to Power on and start the service immediately.

sudo systemctl enable postgresql-16
sudo systemctl start postgresql-16

Verify installation
You can verify that the installation is successful by checking the PostgreSQL version.

psql --version

Configure a firewall (optional)
If you need remote access to PostgreSQL, open port 5432 of the firewall.

sudo firewall-cmd --add-service=postgresql --permanent
sudo firewall-cmd --reload

Adjust PostgreSQL configuration (optional):

editpg_hba.confandFile, modify the listening address, access permissions, etc. as needed.

The configuration file path is usually:

/var/lib/pgsql/16/data/
/var/lib/pgsql/16/data/pg_hba.conf

After completing the above steps, PostgreSQL 16 should have been successfully installed and run on CentOS 9 Stream.

Set password and connect remotely

After installing PostgreSQL 16 on CentOS 9 Stream, you can connect to the database and set a password as follows:

Switch to PostgreSQL user
By default, PostgreSQL creates a name calledpostgressystem user. Switch to this user to directly access PostgreSQL management commands.

sudo -i -u postgres

Enter the PostgreSQL command line interface
usepsqlCommand line tools connect to PostgreSQL.

psql

set uppostgresUser Password
existpsqlIn, use the following commandpostgresDatabase user sets password (can be changed to the password you need):

ALTER USER postgres WITH PASSWORD 'your_secure_password';

When finished, enter\qquitpsql

Configure allow remote connections (optional)
If you need to connect to PostgreSQL remotely, you need to configure the configuration fileSettings inlisten_addresses, and inpg_hba.confAdjust access permissions in .

Revisedocument

sudo nano /var/lib/pgsql/16/data/

turn uplisten_addressesparameter, set it to'*', indicating that all IP addresses are listened to:

listen_addresses = '*'

Revise pg_hba.conf document

sudo nano /var/lib/pgsql/16/data/

Add the following line at the bottom of the file to allow remote IP to access using password:

host    all             all             0.0.0.0/0               md5

Restart PostgreSQL service
Apply new configuration.

sudo systemctl restart postgresql-16

Local test connection
If you want to connect to PostgreSQL locally with a newly set password, you can run the following command:

psql -U postgres -h localhost

Then enter the password you just setyour_secure_password

remote connection(Optional):
If remote connection is enabled, you can usepsqlOr other clients (such as DBeaver, pgAdmin) connect through IP addresses, the example command is as follows:

psql -U postgres -h server_ip -p 5432

After completing these steps, PostgreSQL can connect locally or remotely via password.

This is the end of this article about the steps to install PostgreSQL 16 on CentOS 9 Stream. For more related CentOS Stream installation, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!