มกราคม 27, 2026คู่มือ
วิธีติดตั้ง PostgreSQL บนเซิร์ฟเวอร์ Linux
คู่มือฉบับสมบูรณ์เกี่ยวกับการติดตั้ง การกำหนดค่า และการรักษาความปลอดภัยของเซิร์ฟเวอร์ฐานข้อมูล PostgreSQL บน Ubuntu และ CentOS

PostgreSQL เป็นระบบจัดการฐานข้อมูลเชิงสัมพันธ์แบบโอเพ่นซอร์สที่มีประสิทธิภาพ รู้จักกันดีในเรื่องความน่าเชื่อถือ ความอุดมสมบูรณ์ของฟีเจอร์ และการปฏิบัติตามมาตรฐาน คู่มือนี้จะช่วยให้คุณติดตั้งและกำหนดค่า PostgreSQL บนเซิร์ฟเวอร์ Linux Hiddence ของคุณ
การติดตั้ง PostgreSQL บน Ubuntu/Debian
PostgreSQL มีอยู่ใน repository เริ่มต้น ติดตั้งเวอร์ชันล่าสุด:
bash
sudo apt update
sudo apt install postgresql postgresql-contrib -y
# เริ่มต้นและเปิดใช้งาน PostgreSQL
sudo systemctl start postgresql
sudo systemctl enable postgresql
# ตรวจสอบการติดตั้ง
sudo systemctl status postgresql
psql --versionการติดตั้ง PostgreSQL บน RHEL / CentOS / AlmaLinux / Rocky Linux
สำหรับการแจกจ่ายที่ใช้ RHEL คุณต้องเพิ่ม repository PostgreSQL ก่อน:
bash
# ติดตั้ง repository PostgreSQL
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# สำหรับ CentOS 8/Rocky Linux 8 ใช้:
# sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# ติดตั้ง PostgreSQL
sudo dnf install -y postgresql15-server postgresql15
# เริ่มต้นฐานข้อมูล
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
# เริ่มต้นและเปิดใช้งาน PostgreSQL
sudo systemctl start postgresql-15
sudo systemctl enable postgresql-15
# ตรวจสอบการติดตั้ง
sudo systemctl status postgresql-15
psql --versionการตรวจสอบการติดตั้ง
bash
# ตรวจสอบสถานะ PostgreSQL
sudo systemctl status postgresql
# ตรวจสอบเวอร์ชัน PostgreSQL
psql --version
# เชื่อมต่อกับ PostgreSQL (Ubuntu/Debian)
sudo -u postgres psql
# เชื่อมต่อกับ PostgreSQL (CentOS/RHEL)
sudo -u postgres psql -d postgresการรักษาความปลอดภัย PostgreSQL
ตั้งรหัสผ่านสำหรับผู้ใช้ postgres:
bash
# เชื่อมต่อกับ PostgreSQL
sudo -u postgres psql
# ตั้งรหัสผ่านสำหรับผู้ใช้ postgres
ALTER USER postgres PASSWORD 'รหัสผ่านที่แข็งแกร่งของคุณ';
# ออกจาก PostgreSQL
\qการสร้างฐานข้อมูลและผู้ใช้
bash
# เชื่อมต่อเป็นผู้ใช้ postgres
sudo -u postgres psql
# สร้างฐานข้อมูล
CREATE DATABASE myapp_db;
# สร้างผู้ใช้
CREATE USER app_user WITH PASSWORD 'รหัสผ่านที่แข็งแกร่ง';
# ให้สิทธิ์
GRANT ALL PRIVILEGES ON DATABASE myapp_db TO app_user;
# ออก
\qการจัดการผู้ใช้
bash
# เชื่อมต่อกับ PostgreSQL
sudo -u postgres psql
# แสดงรายการผู้ใช้ทั้งหมด
\du
# สร้างผู้ใช้ใหม่
CREATE USER newuser WITH PASSWORD 'รหัสผ่าน';
# ให้สิทธิ์
GRANT ALL PRIVILEGES ON DATABASE myapp_db TO newuser;
# ถอนสิทธิ์
REVOKE ALL PRIVILEGES ON DATABASE myapp_db FROM newuser;
# ลบผู้ใช้
DROP USER newuser;การเปิดใช้งานการเชื่อมต่อระยะไกล
เพื่ออนุญาตการเชื่อมต่อระยะไกล แก้ไขไฟล์การกำหนดค่า PostgreSQL:
bash
# แก้ไข pg_hba.conf (Ubuntu/Debian)
sudo nano /etc/postgresql/15/main/pg_hba.conf
# แก้ไข pg_hba.conf (CentOS/RHEL)
sudo nano /var/lib/pgsql/15/data/pg_hba.conf
# เพิ่มบรรทัด:
host all all 0.0.0.0/0 md5
# แก้ไข postgresql.conf (Ubuntu/Debian)
sudo nano /etc/postgresql/15/main/postgresql.conf
# แก้ไข postgresql.conf (CentOS/RHEL)
sudo nano /var/lib/pgsql/15/data/postgresql.conf
# เปลี่ยน:
listen_addresses = '*'
# รีสตาร์ท PostgreSQL
sudo systemctl restart postgresqlคำสั่ง PostgreSQL พื้นฐาน
bash
# เชื่อมต่อกับฐานข้อมูล
psql -U app_user -d myapp_db
# แสดงรายการฐานข้อมูล
\l
# เชื่อมต่อกับฐานข้อมูล
\c database_name
# แสดงรายการตาราง
\dt
# อธิบายตาราง
\d table_name
# รันไฟล์ SQL
psql -U app_user -d myapp_db -f script.sql
# สำรองฐานข้อมูล
pg_dump -U app_user myapp_db > backup.sql
# กู้คืนฐานข้อมูล
psql -U app_user -d myapp_db < backup.sqlเคล็ดลับที่มีประโยชน์
- ใช้รหัสผ่านที่แข็งแกร่งเสมอสำหรับผู้ใช้ฐานข้อมูล
- จำกัดการเข้าถึงระยะไกลไปยังที่อยู่ IP เฉพาะใน pg_hba.conf
- สำรองฐานข้อมูลของคุณเป็นประจำโดยใช้ pg_dump
- ตรวจสอบบันทึก PostgreSQL: /var/log/postgresql/ (Ubuntu) หรือ /var/lib/pgsql/15/data/log/ (CentOS)
- ใช้ connection pooling (pgBouncer) สำหรับแอปพลิเคชันที่มีการใช้งานสูง
- อัปเดต PostgreSQL ให้เป็นเวอร์ชันเสถียรล่าสุดสำหรับแพตช์ความปลอดภัย