Cài đặt PostgreSQL trên CentOS 7

Tác giả: 17/01/2023

Hôm nay Nhân Hòa sẽ hướng dẫn các bạn cài đặt PostgreSQL trên CentOS 7 và enable remote connect.

Phiên bản hiện tại mình sẽ cài đặt là PostgreSQL 15.1 , mình sẽ cài phiên bản mới nhất này.

Bước 1 – Update OS

yum update -y

Bước 2 – Thêm PostgreSQL Repository

yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

Kiểm tra lại repos đã được add thêm và hoạt động chưa

yum -y repolist

Bước 3 – Install PostgreSQL 15  

yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum -y install postgresql15-server

psql -V

Bước 4: Khởi tạo PostgreSQL 15 trên CentOS 7/RHEL 7

Initdb phải được khởi tạo trước khi bạn có thể sử dụng PostgreSQL 15 trên CentOS 7 / RHEL 7. Để thực hiện thao tác này, hãy sử dụng các lệnh:

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

[root@postgresql ~]# sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
Initializing database … OK

Khởi động lại cùng hệ thống

systemctl enable --now postgresql-15
systemctl restart --now postgresql-15
systemctl status postgresql*

Bước 5: Open port 5432  trên firewall

Bước 6:: Set postgres user Password

Với tư cách là root hoặc user có đặc quyền sudo, hãy đặt mật khẩu cho user postgres

sudo passwd postgres

[root@postgresql ~]# sudo passwd postgres
Changing password for user postgres.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Test access to user’s shell using password set above.

su - postgres

Update postgres database user password

psql -c "ALTER USER postgres WITH PASSWORD 'Pp098765a@1';"

Bước 7 – Enable remote PostgresSQL

Như chúng ta có thể thấy ở trên, cổng 5432 đang bind 127.0.0.1. Điều đó có nghĩa là mọi nỗ lực kết nối với máy chủ postgresql từ bên ngoài máy sẽ bị từ chối. Chúng ta có thể thử nhấn cổng 5432 bằng cách sử dụng telnet.

Chúng ta sẽ open kết nối từ internet cấu hình file  postgresql.conf

find / -name "postgresql.conf"

listen_addresses = '*'

Khởi động lại service và kiểm tra

systemctl restart --now postgresql-15

Configuring pg_hba.conf

Let’s try to connect to remote postgresql server using “psql”.

[root@postgresql ~]# psql -h 45.117.80.207 -U postgres
psql: error: connection to server at “45.117.80.207”, port 5432 failed: FATAL: no pg_hba.conf entry for host “45.117.80.207”, user “postgres”, database “postgres”, no encryption

[root@postgresql ~]# find / -name “pg_hba.conf”
/var/lib/pgsql/15/data/pg_hba.conf

vi /var/lib/pgsql/15/data/pg_hba.conf

Thêm vào cuối dòng

host all all 0.0.0.0/0 md5
host all all ::/0 md5

Md5 là yêu cầu password,,nếu bạn muốn khách hàng cho phép thu thập mà không cần cung cấp bất kỳ mật khẩu nào thì hãy thay đổi “md5” thành “trust” và điều đó sẽ cho phép kết nối vô điều kiện.

Khởi động lại service và connect lại

systemctl restart --now postgresql-15

psql -h 45.117.80.207 -U postgres

Tạo Database và kết nối từ xa bằng pgAdmin 4, ở đây mình sử dụng luôn database postgres

Nhập master password pgAdmin

Connect

TỔNG KẾT

Như vậy Nhân Hòa đã hướng dẫn các bạn tạo PostgresSQL server và cấu hình connect từ xa.

Hẹn gặp lại các bạn tại các bài viết sau tại wiki.nhanhoa.com

Trả lời