Video in progress

How do you organize access rights to the database across an organization: roles, least privilege and password rotation?

Each service connects with its own role to only its own schema, migrations use a separate role, and people get access to prod only temporarily and with auditing. Passwords live in a secret manager and are rotated automatically, and where possible I use IAM or short-lived credentials.

📖 Answer

In many companies the starting point looks like this: every service connects as the postgres superuser, the password sits in application.yml, and developers get into prod with that same password. One SQL injection or leaked config, and the whole database is gone.

The model I build:

-- schema owner: migrations only
CREATE ROLE orders_owner NOLOGIN;
CREATE ROLE orders_migrator LOGIN IN ROLE orders_owner;

-- application: DML only
CREATE ROLE orders_app LOGIN;
GRANT USAGE ON SCHEMA orders TO orders_app;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA orders TO orders_app;
ALTER DEFAULT PRIVILEGES FOR ROLE orders_owner IN SCHEMA orders
  GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO orders_app;

-- read access: reports and support
CREATE ROLE orders_readonly NOLOGIN;
GRANT SELECT ON ALL TABLES IN SCHEMA orders TO orders_readonly;

The application role can't DROP, ALTER or TRUNCATE: migrations and the application are separated.

People. Personal accounts, no shared password. Access to prod goes through a request and approval, with a credential issued for a few hours (Vault's database secrets engine or cloud IAM auth). Read-only by default, through views without the PII columns. pgaudit records who did what.

Rotation. A static password lives in a secret manager and is replaced automatically. During the swap, the old and new passwords must both work for a while (alternate between two roles or two users), otherwise connections in the pool fail when they reconnect.

I manage all of this as code (Terraform or a migrations repo), not by hand: who was given which right is visible in the git history.