Skip to content

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

Neon 5 min read How-to mediumAdept

How to Set Up Row-Level Security in Postgres Using AWS Cognito

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.

Related posts

  • 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.

  • 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.

  • 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.

  • Recreating S3 in Postgres using PostgREST

    Neon mediumAdept

    A simple S3-like file server can be built inside Postgres by storing binary data in a BYTEA table and exposing it through PostgREST's functions-as-RPC via the Neon Data API. A custom "*/*" domain lets a PL/pgSQL function return arbitrary media types and set Content-Type, Content-Disposition and Cache-Control headers, and generated columns compute size, MIME type and SHA-256 hash. Row-level security policies on anonymous and authenticated roles, with JWT-based user IDs, restrict access to public or owned blobs. Tables grant SELECT to anonymous users by default, so RLS setup is essential, and the pattern is meant as a teaching vehicle rather than a real S3 replacement.

The quest for software engineering knowledge.

Look:
Layout: