PostgreSQL เป็น Object-Relational Database ที่เข้มงวดกับ SQL Standards, มี MVCC ให้ประสิทธิภาพสูง, รองรับ JSON, Replication, และ Scalability ดี
ติดตั้ง PostgreSQL 11 จาก PGDG Repository
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install postgresql11-server -y
/usr/pgsql-11/bin/postgresql-11-setup initdb
systemctl enable postgresql-11
systemctl start postgresql-11
Firewall และ SELinux
firewall-cmd --zone=public --add-port=5432/tcp --permanent
firewall-cmd --reload
# หรือ iptables
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT
service iptables save
# SELinux
setsebool -P httpd_can_network_connect_db 1
จัดการฐานข้อมูลด้วย PSQL
sudo -u postgres psql
# เปลี่ยนรหัสผ่าน postgres
ALTER ROLE postgres WITH PASSWORD 'StrongPass';
# สร้าง DB และ User
CREATE DATABASE myapp;
CREATE USER myuser WITH PASSWORD 'StrongPass';
GRANT ALL PRIVILEGES ON DATABASE myapp TO myuser;
# คำสั่ง PSQL พื้นฐาน
\l # List Databases
\c dbname # Connect to DB
\dt # List Tables
\du # List Users
\q # Quit
# Admin Commands
SELECT * FROM pg_stat_activity WHERE datname='myapp';
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='myapp';
Web GUI: ใช้ pgAdmin4 (Python/JS) คล้าย phpMyAdmin
ไฟล์ตั้งค่าสำคัญ (/var/lib/pgsql/11/data/)
- postgresql.conf – Main Config
- pg_hba.conf – Host-Based Authentication (Access Control)
- pg_ident.conf – Ident Mapping
pg_hba.conf – บังคับ Password Authentication:
local all all md5
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
postgresql.conf – พารามิเตอร์สำคัญ:
| Parameter | คำอธิบาย | แนะนำ |
|---|---|---|
| listen_addresses | IP ที่ Listen | '*' หรือ '0.0.0.0' สำหรับทุก Interface |
| max_connections | Max Concurrent Connections | 100-200 (ปรับตาม RAM) |
| shared_buffers | Shared Memory Buffer | 25% RAM (เช่น 1GB สำหรับ 4GB RAM) |
| effective_cache_size | OS Cache Estimate | 50-75% RAM (เช่น 3GB สำหรับ 4GB RAM) |
| work_mem | Memory ต่อ Operation (Sort, Hash) | 4-16MB |
| maintenance_work_mem | Memory สำหรับ VACUUM, CREATE INDEX | 256MB-1GB |
| wal_buffers | WAL Buffer | 16MB |
| checkpoint_completion_target | Checkpoint Spreading | 0.7-0.9 |
| random_page_cost | Cost ของ Random Page Access | 1.1 สำหรับ SSD, 4.0 สำหรับ HDD |
| effective_io_concurrency | IO Concurrency | 200 สำหรับ SSD |