On this page
Security, Roles & Row-Level Security
Prev: Indexes & EXPLAIN ANALYZE | Up: postgresql | Next: mongodb
Rel: DDL & Constraints, Web Security Mitigations
Ref: PostgreSQL — Row Security Policies
Tags: #public #databases #postgresql #security #rls
Contents
Least-Privilege Roles
Never run application traffic as a superuser (postgres). Separate a migration role (DDL owner) from a runtime app role granted only SELECT, INSERT, UPDATE, DELETE on specific tables:
CREATE ROLE app_runtime LOGIN PASSWORD 'replace_me';
GRANT USAGE ON SCHEMA app TO app_runtime;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA app TO app_runtime;
Multi-Tenant Row-Level Security (RLS)
RLS enforces per-row visibility inside the database engine itself, preventing cross-tenant data leaks even if an application query forgets a WHERE tenant_id = ... clause:
ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON documents
USING (tenant_id = current_setting('app.current_tenant_id')::uuid);
Set the session variable per transaction (SET LOCAL app.current_tenant_id = '...';) so connection pools never leak tenant context across requests.
Linked from
Related notes
- tuts/databases/postgresql Linked here; links to this page; shared tags: #databases, #postgresql
- tuts/databases/postgresql/indexes_and_explain_analyze Linked here; links to this page; shared tags: #databases, #postgresql
- tuts/databases/mongodb Linked here; shared tags: #databases
- tuts/databases/sql/ddl_and_constraints Linked here; shared tags: #databases
- tuts/web/security/csp_cookies_and_mitigations Linked here; shared tags: #security
- tuts/databases/postgresql/jsonb_arrays_and_advanced_types shared tags: #databases, #postgresql
- tuts/databases/postgresql/mvcc_vacuum_and_wal shared tags: #databases, #postgresql
- tuts/databases shared tags: #databases
- tuts/databases/design_patterns shared tags: #databases
- tuts/databases/design_patterns/indexing_and_storage_engines shared tags: #databases