Introducing Neon RLS: Simplifying Row-Level Security For Postgres
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.
Related posts
- 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.
- 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.
- 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.
- Is “SQL from the frontend” viable?
Neon easyNovice
Letting the frontend query the database directly, via tools like PostgREST or Firebase, can sharply speed up development by removing backend abstraction layers and frontend/backend team dependencies, which makes it attractive for prototypes and many apps. It does not have to replace the backend, which still holds secrets and sensitive logic. Exposing a database safely would require query-shape and parameter allow-listing with tooling to maintain the lists, per-IP rate limiting, and database-level authorization such as Postgres Row-Level Security. The verdict is that the approach is not production-ready yet, and Neon is exploring it through its RLS work, now part of the Neon Data API.