Skip to the workspace
Pebellslearn by building
Security  ›  Lesson

Granting and Revoking Privileges

Grant / Revoke 14 minAccess needs a privilege at every layer — CONNECT on the database, USAGE on the schema and the right verb on the object — and default privileges cover objects created later.

postgresql dba / Granting and Revoking Privileges

Explore each scene at your own pace.

01 · The rule

The rule

Granting and Revoking Privileges

Ravi joined the analysts group yesterday, yet his first query fails with permission denied for schema billing. The group was granted SELECT on the tables. What is missing? PostgreSQL checks privileges at every layer of the object tree, and a grant at one layer does nothing if an outer door is shut.

The rule

Open every door on the path: database, schema, then object.

To read billing.invoices, a role needs:

  1. CONNECT on the database pedal.
  2. USAGE on the schema billing (to find anything inside it).
  3. SELECT on the table billing.invoices.
GRANT CONNECT ON DATABASE pedal TO analysts;
GRANT USAGE ON SCHEMA billing TO analysts;
GRANT SELECT ON ALL TABLES IN SCHEMA billing TO analysts;

The table privileges are SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES and TRIGGER (plus MAINTAIN from version 17). Schemas have USAGE and CREATE. REVOKE takes any of them away:

REVOKE DELETE ON public.rentals FROM pedal_app;

Four rules explain most surprises:

  • The owner of an object has every privilege on it and is the only one (apart from superusers) who can ALTER or DROP it.
  • PUBLIC means every role. By default, PUBLIC may connect to any new database. Since PostgreSQL 15 it can no longer create objects in the public schema.
  • ON ALL TABLES IN SCHEMA covers only tables that exist now. Tomorrow's new table gets nothing.
  • Default privileges fix that. They say what future objects created by a given role will grant:
ALTER DEFAULT PRIVILEGES FOR ROLE pedal_owner IN SCHEMA billing
  GRANT SELECT ON TABLES TO analysts;

\dp billing.* in psql shows each table's access list, such as analysts=r/pedal_owner: analysts can read (r), granted by pedal_owner.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.