Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Neon 12 min read How-to mediumAdept

Postgres Roles: What to Know Before You Begin

Postgres has no separate users and groups, only roles: a role with LOGIN behaves like a user, and a role without it acts as a group whose privileges pass to its members through inheritance. Access depends on privileges at every level of the hierarchy. Reading a table requires CONNECT on the database, USAGE on the schema and SELECT on the table, so revoking schema USAGE silently blocks table-level grants. Common gotchas include the public schema defaults, SECURITY DEFINER functions, search_path hijacking, implicit owner privileges and inconsistent attribute inheritance. Migrations with tangled role and object dependencies are the biggest pain point, so the post recommends mapping dependencies, writing custom scripts beyond pg_dump/pg_restore, testing with a rollback plan, and following least privilege.

Related posts

  • Postgres Roles and Privileges

    Supabase mediumAdept

    Postgres access control rests on objects, privileges, roles and ownership: an object's owner holds all privileges on it and is the one who can grant them, so even an admin-like role such as Supabase's non-superuser postgres gets permission denied when granting on a table it neither owns nor holds privileges on. A psql walkthrough with junior_dev and senior_dev roles shows granting CREATE on a schema, reading ACLs with \dn+, \dp and \ddp, delegating with WITH GRANT OPTION, and using ALTER DEFAULT PRIVILEGES so tables created later are automatically shared.

  • Cracking PostgreSQL Interview Questions

    Supabase mediumAdept

    Interview preparation for the transactional side of PostgreSQL, organized around five areas: data modeling, indexes, views, roles and functions. It covers modeling 1-1, 1-M and M-M relationships with foreign keys and join tables, and using rich types like JSONB, enums and timestamptz. On performance and access control it explains B-tree and other index types, comparing plans with EXPLAIN ANALYZE, auto-updatable views, GRANTs to PUBLIC, and row level security policies. It also shows PL/pgSQL trigger functions for auto-updating timestamps, advises thinking in sets rather than loops, and introduces CTEs, all with short SQL examples.

  • Declarative Schemas for Simpler Database Management

    Supabase mediumAdept

    Supabase CLI now supports declarative schemas: the desired final state of Postgres tables, views, functions, RLS policies, grants and types lives in versioned .sql files, and a schema diff tool such as migra generates the migration files. Supabase used this internally for two years on its complex projects table, which sits in a private schema behind a public view with RLS-based ABAC and INSTEAD OF triggers. Previously, adding one column meant hand-copying view and trigger definitions into a migration of several hundred lines that conflicted often. Now it is a one-line diff, and generated migrations still get manual review. The team says this cut schema changes from hours to minutes and made rebasing much easier.

  • Higher-level CRUD Abstraction for Postgres RLS

    Neon mediumAdept

    Postgres Row-Level Security requires separate USING and WITH CHECK policies per operation, which gets repetitive for CRUD apps. Neon's Drizzle ORM helper `crudPolicy` takes a role plus `read` and `modify` expressions (true, false, a SQL expression, or null to skip) and generates the select, insert, update and delete policies. A companion `authUid` helper ties pg_session_jwt's `auth.user_id()` to a table column. The post shows the raw SQL for a todos table next to the equivalent concise Drizzle schema. The functionality has since moved into the Neon Data API.

  • Is Postgres RLS for Everything and Everyone?

    Neon mediumAdept

    Postgres row-level security is a poor fit for expressing an entire app's authorization model. Its syntax is dense, org-membership checks hit "infinite recursion detected in policy" errors (worked around with views or RLS-bypassing functions), and there are several pitfalls: wrapping auth calls in a subselect so the optimizer caches them per statement, permissive policies being OR'd while restrictive ones are AND'd, security_invoker semantics on views and functions, and hard-to-write integration tests. The recommended hybrid is to keep fine-grained authorization in a higher-level framework like CASL and use RLS as a baseline guard against cross-tenant access, applying one tenant_id policy to every table driven by a JWT tenantId claim.

The quest for software engineering knowledge.

Look:
Layout: