PostgreSQL security / RLS / audit logging 的核心責任是把資料庫安全拆成存取邊界、資料列可見性與操作證據。PostgreSQL role / grant 決定誰能連線與操作 schema;Row Level Security 決定同一張表中哪些 row 對某個 role 可見;audit logging 則把敏感操作轉成可查詢、可保留、可告警的證據。

本文的判讀錨點是:資料庫安全是 application auth 的下游防線。Application 仍要負責身份、session、租戶與 workflow;PostgreSQL security layer 負責在資料邊界補上 least privilege、tenant isolation 與 forensic evidence。

Role and Grant Baseline

Role and grant baseline 的核心責任是把人、服務、migration 與分析查詢分開。Production database 至少要區分 application role、migration role、read-only role、admin role 與 replication / CDC role。

Role 類型權限責任常見風險
Application執行產品讀寫權限過大、可 DDL、可讀所有 schema
Migration變更 schema和 app 共用 role,事故難以追蹤
Read-only分析、debug、support讀到 PII 或跨 tenant 資料
Replication / CDClogical replication、slot access權限與 WAL retention 風險
Adminemergency operation日常使用 admin role

Grant review 要以 schema ownership 開始。Tables、sequences、functions、views、extensions 都有權限面;只管 table grant 會漏掉 sequence update、function execution 與 extension 使用。

Row Level Security

Row Level Security 的核心責任是在資料庫層 enforce row visibility。PostgreSQL 官方 RLS 文件描述 policy 可限制 normal query 返回、insert、update、delete 的 row;這讓 tenant boundary 可以在 database 層多一道 guard。

RLS 使用情境適合條件審查問題
Multi-tenant SaaStenant_id 明確且每個 query 都可帶入policy 是否覆蓋 SELECT / INSERT / UPDATE
Support accesssupport role 需受限查詢break-glass 是否有 audit
Regional datarow 上有 region / residencypolicy 是否和 GDPR / residency 對齊
Sensitive subsetPII row 需特別隔離masking / tokenization 是否仍需存在

RLS policy 要有 positive allow rule。每張啟用 RLS 的 table 都要有測試:同 tenant 可讀、跨 tenant 隔離、insert tenant mismatch 被擋、admin / support 例外被記錄。

1ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
2
3CREATE POLICY tenant_isolation ON invoices
4USING (tenant_id = current_setting('app.tenant_id')::uuid)
5WITH CHECK (tenant_id = current_setting('app.tenant_id')::uuid);

這段 policy 依賴 application 在 transaction 內設定 app.tenant_id。使用 connection pooler 時,設定必須跟 transaction boundary 對齊,避免 session state 漂移。

Audit Logging

Audit logging 的核心責任是把敏感資料操作轉成可查詢證據。PostgreSQL 原生日誌可以記錄連線、DDL、錯誤與慢查詢;pgAudit 這類 extension 則補強 session / object audit。

Audit 類型目的Evidence
DDL auditschema 變更追蹤migration id、role、statement、timestamp
Sensitive readPII / payment / health data 查詢role、tenant、operation、reason
Privilege changegrant / revoke / role 變更actor、target role、approval
Failed access權限錯誤與 RLS blockerror code、role、relation
Break-glassemergency admin accessticket id、duration、review result

Audit log 要能進入 SIEM 或集中 log。只留在 database host 上,事故後查詢成本高;正式 runbook 要定義 retention、masking、access control 與 alert。

PII and Data Protection Boundary

PII and data protection boundary 的核心責任是把 database 權限和資料保護策略接起來。RLS 可以限制 row visibility,但 PII 的保護還需要 masking、tokenization、encryption、retention 與 deletion evidence。

資料類型Database control跨模組路由
Tenant dataRLS、tenant-scoped roledata access review
PIIcolumn grant、masking viewData Protection
Audit logappend-only storage、retentionSIEM / incident evidence
Deletion requesttombstone、cascade reviewretention policy、legal hold

Column-level grant 和 masking view 適合 read-only analyst。Application role 通常需要明文處理 workflow;analyst / support role 則應走 restricted view。

Operational Evidence

Operational evidence 的核心責任是讓安全設定可驗證。每次 release 或權限變更後,要跑固定檢查。

  1. Role matrix:每個 role 的 schema / table / sequence / function grant。
  2. RLS test:tenant A / tenant B / support / admin 的可見性測試。
  3. Audit sample:DDL、sensitive read、failed access 是否進 log。
  4. Pooler compatibility:SET LOCAL app.tenant_id 是否跟 transaction 對齊。
  5. Break-glass drill:emergency access 是否可申請、可回收、可審查。

Evidence 要保存在 release artifact。Security 設定只有文件描述時,incident 後難以證明它真的生效。

Failure Modes

Failure modes 的核心責任是把 database security 常見事故提前列出。

Failure mode判讀訊號修正方向
App role 權限過大app 可 DDL / drop / grantrole split + least privilege
RLS bypassowner / superuser / policy 漏洞dedicated app role + RLS test
Pooler state drifttenant setting 漂到下個 requestSET LOCAL + transaction pooling review
Audit gap敏感操作查不到 actorpgAudit / log schema / SIEM route
Support overreadsupport role 可讀全 tenantmasking view + ticket-scoped access

RLS bypass 要特別審查 table owner 與 superuser path。正式 application 連線應使用 dedicated role,並避免使用 table owner role 執行一般 request。

下一步路由

Security / RLS / audit logging 完成後,權限與 PII 治理讀 Data Protection;connection state 風險讀 Connection Pooler Comparison;實作演練可放進 Schema Migration Evidence Lab 的 release gate。