// Security
Security in PostgreSQL
ระบบ Security ระดับ Enterprise ใน PostgreSQL — ตั้งแต่ Authentication, Role-Based Access Control ไปจนถึง Row-Level Security และ Audit Logging
Authentication
pg_hba.conf ควบคุมว่าใครสามารถเชื่อมต่อได้ รองรับ md5, scram-sha-256, certificate, LDAP, GSSAPI
Authorization
Role-Based Access Control (RBAC) ควบคุม Privileges ในระดับ Database, Schema, Table, Column
Row-Level Security
RLS Policy ควบคุมการเข้าถึงข้อมูลในระดับ Row — แต่ละ User เห็นเฉพาะ Row ที่ได้รับอนุญาต
Encryption
SSL/TLS สำหรับ Data in Transit, pgcrypto สำหรับ Data at Rest, Column-Level Encryption
Audit Logging
pgAudit บันทึกทุก SQL Statement สำหรับ Compliance (PCI-DSS, HIPAA, SOX)
Secrets Management
รองรับ Vault Integration, Environment Variables และ pg_catalog Hidden Columns
// Authentication
pg_hba.conf — ควบคุมการเชื่อมต่อ
# pg_hba.conf (Host-Based Authentication)
# TYPE DATABASE USER ADDRESS METHOD
# Local connections (Unix socket)
local all postgres peer
local all all scram-sha-256
# IPv4 connections
host all all 127.0.0.1/32 scram-sha-256
host mydb myapp 10.0.1.0/24 scram-sha-256
# SSL required from specific IP
hostssl all all 0.0.0.0/0 scram-sha-256
# Reject all other connections
host all all 0.0.0.0/0 reject
// Role-Based Access Control
Roles & Privileges
-- สร้าง Role สำหรับแต่ละ Function
CREATE ROLE readonly_role;
CREATE ROLE app_role;
CREATE ROLE admin_role;
-- ให้ Privilege กับ readonly_role
GRANT CONNECT ON DATABASE mydb TO readonly_role;
GRANT USAGE ON SCHEMA public TO readonly_role;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly_role;
-- ให้ app_role เขียนได้
GRANT readonly_role TO app_role;
GRANT INSERT, UPDATE, DELETE ON orders, products TO app_role;
-- สร้าง User และกำหนด Role
CREATE USER api_user WITH PASSWORD 'strong_password';
GRANT app_role TO api_user;
-- ตรวจสอบ Privilege
SELECT * FROM information_schema.role_table_grants
WHERE grantee = 'api_user';
// Row-Level Security
Row-Level Security (RLS) — จุดเด่นของ PostgreSQL
RLS คืออะไร?
Row-Level Security (RLS) ช่วยให้กำหนดได้ว่า User แต่ละคนสามารถเห็น (SELECT) หรือแก้ไข (INSERT/UPDATE/DELETE) Row ไหนได้บ้าง โดยตรงใน Database Layer
ไม่ต้องเขียน WHERE clause ใน Application Code — PostgreSQL บังคับ Policy ให้อัตโนมัติ เหมาะมากสำหรับ Multi-Tenant Applications
-- เปิดใช้งาน RLS บน Table
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
-- Policy: User เห็นเฉพาะ Order ของตัวเอง
CREATE POLICY user_orders_policy
ON orders
FOR ALL
USING (user_id = current_user_id());
-- Policy แบบ Multi-Tenant (by tenant_id)
CREATE POLICY tenant_isolation
ON customers
USING (tenant_id = current_setting('app.tenant_id')::int);
-- Set tenant ใน Session
SET app.tenant_id = '42';
-- superuser bypass RLS โดยอัตโนมัติ
-- ใช้ FORCE ROW LEVEL SECURITY สำหรับ table owner
ALTER TABLE orders FORCE ROW LEVEL SECURITY;
// SSL/TLS
SSL/TLS Encryption
# postgresql.conf — เปิด SSL
ssl = on
ssl_cert_file = 'server.crt'
ssl_key_file = 'server.key'
ssl_ca_file = 'root.crt'
ssl_min_protocol_version= 'TLSv1.2'
# Connection String ที่ require SSL
postgresql://user:pass@host/db?sslmode=require
postgresql://user:pass@host/db?sslmode=verify-full
เรียนรู้เพิ่มเติมเกี่ยวกับ PostgreSQL DBA
ดูคู่มือ DBA ครบวงจร รวมถึง Backup, Replication และ High Availability