---
title: "Modelling Authorization for a Social Network with Postgres RLS and Drizzle ORM"
description: "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()."
url: "https://swe.quest/articles/modelling-authorization-for-a-social-network-with-postgres-rls-and-drizzle-orm-6267eb437b8171e7"
canonical: "https://neon.com/blog/modelling-authorization-for-a-social-network-with-postgres-rls-and-drizzle-orm"
source: "Neon"
published: "2024-11-11"
difficulty: "medium"
kind: "How-to"
reading_minutes: 8
tags: ["postgres","identity-auth","typescript","system-design"]
---

# Modelling Authorization for a Social Network with Postgres RLS and Drizzle ORM

A post on [Neon](https://swe.quest/sources/neon.md), published 2024-11-11, 8 min read, How-to. Original: [https://neon.com/blog/modelling-authorization-for-a-social-network-with-postgres-rls-and-drizzle-orm](https://neon.com/blog/modelling-authorization-for-a-social-network-with-postgres-rls-and-drizzle-orm)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Identity & auth](https://swe.quest/tags/identity-auth.md) (Security)
- [TypeScript](https://swe.quest/tags/typescript.md) (Languages)
- [System design](https://swe.quest/tags/system-design.md) (Architecture & design)

## Prerequisites

- PostgreSQL row-level security ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- SQL views ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- TypeScript ORMs ([easy posts on TypeScript](https://swe.quest/tags/typescript.md?difficulty=easy))
- JWT authentication ([easy posts on Identity & auth](https://swe.quest/tags/identity-auth.md?difficulty=easy))

## Keywords

Row-Level Security, Drizzle ORM, pg\_session\_jwt, Neon, PostgREST, crudPolicy

## Related posts

- [Higher-level CRUD Abstraction for Postgres RLS](https://swe.quest/articles/higher-level-crud-abstraction-for-postgres-rls-90207e4bb081ead2.md): Neon · 2024-11-13 · Medium · 3 min read · PostgreSQL, Identity & auth, TypeScript
  Postgres Row-Level Security requires separate USING and WITH CHECK policies per operation, which gets repetitive for CRUD apps.
- [Introducing Neon RLS: Simplifying Row-Level Security For Postgres](https://swe.quest/articles/introducing-neon-rls-simplifying-row-level-security-for-postgres-4b66d76670bb402f.md): Neon · 2024-10-30 · Medium · 6 min read · PostgreSQL, Identity & auth, Multi-tenancy, TypeScript
  Neon RLS lets Postgres row-level security policies authenticate requests with JWTs from any auth provider that exposes a JWKS URL.
- [Is Postgres RLS for Everything and Everyone?](https://swe.quest/articles/is-postgres-rls-for-everything-and-everyone-1df08667813a2c82.md): Neon · 2024-11-15 · Medium · 4 min read · PostgreSQL, Identity & auth, Multi-tenancy, Security
  Postgres row-level security is a poor fit for expressing an entire app's authorization model.
- [How to Set Up Row-Level Security in Postgres Using AWS Cognito](https://swe.quest/articles/how-to-set-up-row-level-security-in-postgres-using-aws-cognito-7cb6bed6c9694d22.md): Neon · 2024-12-04 · Medium · 5 min read · PostgreSQL, Identity & auth, JavaScript, Serverless
  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.
- [Partial data dumps using Postgres Row Level Security](https://swe.quest/articles/partial-data-dumps-using-postgres-row-level-security-7b8cbab5206adee8.md): Supabase · 2022-06-28 · Medium · 4 min read · PostgreSQL, Privacy, Identity & auth, Testing
  Postgres 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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
