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 install
Command 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.conf
andFile, 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 calledpostgres
system user. Switch to this user to directly access PostgreSQL management commands.
sudo -i -u postgres
Enter the PostgreSQL command line interface:
usepsql
Command line tools connect to PostgreSQL.
psql
set uppostgres
User Password:
existpsql
In, use the following commandpostgres
Database user sets password (can be changed to the password you need):
ALTER USER postgres WITH PASSWORD 'your_secure_password';
When finished, enter\q
quitpsql
。
Configure allow remote connections (optional):
If you need to connect to PostgreSQL remotely, you need to configure the configuration fileSettings in
listen_addresses
, and inpg_hba.conf
Adjust access permissions in .
Revisedocument:
sudo nano /var/lib/pgsql/16/data/
turn uplisten_addresses
parameter, 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 usepsql
Or 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!