Higher-level CRUD Abstraction for Postgres RLS
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.
Related posts
- Modelling Authorization for a Social Network with Postgres RLS and Drizzle ORM
Neon mediumAdept
Postgres Row-Level Security can express the full authorization model for a Twitter-like social network, defined with Drizzle ORM's crudPolicy and pgPolicy helpers and the pg_session_jwt extension's auth.user_id(). Profiles, posts and comments get simple policies: public reads, owner-only writes. Private chats need more care: participants can only see other participants of their own chats, a self-referential rule RLS can't express without recursion errors, so it lives in a view made read-only by adding DISTINCT. Takeaways are that raw-SQL RLS is error-prone, testing policies is hard, schemas exposed to the frontend need careful design beyond policies (e.g. unique constraints), and this work shouldn't be trusted to an LLM.
- Introducing Neon RLS: Simplifying Row-Level Security For Postgres
Neon mediumAdept
Neon RLS lets Postgres row-level security policies authenticate requests with JWTs from any auth provider that exposes a JWKS URL. The Neon Proxy verifies the token and exposes its claims through auth.user_id() and auth.session() from the open-source pg_session_jwt extension, for use in RLS policies and WHERE clauses. A Drizzle ORM integration adds pgPolicy and crudPolicy helpers so policies are declared alongside the schema. Neon recommends RLS for core authorization rules such as multi-tenant isolation, keeping intricate access logic in backend code, and says backend-less client apps are only suitable for simple projects.
- How to Set Up Row-Level Security in Postgres Using AWS Cognito
Neon mediumAdept
Postgres row-level security can be driven by AWS Cognito JWTs: a Cognito User Pool issues tokens, and Neon Authorize validates them and maps claims like `sub` into Postgres session variables. A `notes` table gets an RLS policy comparing `user_id` to `current_setting('jwt.claims.sub')`, so each user sees only their own rows. The demo app uses Express.js, HTMX and the Neon serverless driver, with step-by-step setup for Cognito, the database schema, environment variables and policy verification. Neon has since folded this functionality 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.
- Partial data dumps using Postgres Row Level Security
Supabase mediumAdeptPostgres Row Level Security can produce a seed.sql containing only safe, non-customer rows: create a restricted `exporter` role, attach RLS select policies that filter which rows it can see, then run `pg_dump --data-only --enable-row-security` as that role. Example policies filter by hardcoded IDs, email domain, recency (e.g. last two months of analytics events), or an `is_exportable` flag column. Alternatives include fully masked database copies (Snaplet) and copy-on-write database forking (Database Lab Engine on ZFS).