Least Privilege

Accounts and Permissions

  • The application connects with an account that can only SELECT/INSERT/UPDATE/DELETE its own tables, never DROP or GRANT.
  • Separate accounts for migrations (DDL), reporting (read-only) and administration.
  • Use roles, not per-user grants (see Basic → DCL).
  • Expose sensitive data through views that hide columns (e.g. salary, PAN).

Example

-- PostgreSQL
CREATE ROLE app_rw LOGIN PASSWORD '***';
GRANT CONNECT ON DATABASE shopdb TO app_rw;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_rw;

CREATE ROLE reporting LOGIN PASSWORD '***';
GRANT SELECT ON CustomerSales, MonthlySales TO reporting;   -- views only

-- Row-level security: each tenant sees only its own rows
ALTER TABLE Orders ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON Orders
  USING (TenantID = current_setting('app.tenant_id')::int);