All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is Row Security in PostgreSQL and why is it Rails developers

Post cover: What is Row Security in PostgreSQL and why is it Rails developers
Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
PostgreSQL has a powerful but often underrated feature - Row Level Security (RLS).
In short, this data protection at the table row level, that is, the — system decides, which records can the user see or change, before the request even gets into your Rails code.

How it works

In a normal situation, access to data is controlled in the — application, for example, in Rails we write:
@posts = Post.where(user_id: current_user.id)
But RLS allows this check to be delegated the base itself.
You enable the security policy for the table:
ALTER TABLE posts ENABLE ROW LEVEL SECURITY;

CREATE POLICY user_is_owner
  ON posts
  FOR SELECT USING (user_id = current_setting('app.current_user_id')::int);
After that, even if someone does SELECT * FROM posts,
 PostgreSQL will automatically impose a condition that the user sees only your own lines.

How to integrate RLS into Rails

In Rails, you can set current_user.id in the database context before executing a request:
ActiveRecord::Base.connection.execute("SET app.current_user_id = #{current_user.id}")
And then all requests (Post.all, Post.find, even joins) will return only the allowed data - no additional where in the code.
This is convenient for multiplayer systems, SaaS or APIs, where security should not depend only on the application level.

Why this at all

  • Security at the database level <TAG1> even if someone mistakenly forgets where(user_id: ...), the data will not leak.
  • Ease of requests <TAG1> can write Model.all without thinking about filters.
  • Unified access control <TAG1> rules are stored with data, not scattered across controllers and services.
RLS does not replace authorization in the application. She - additional level of protection, which ensures that even at a low level no one will receive "extra" data. Row Level Security <TAG1> is like where(user_id: current_user.id) but sewn into the database itself.

This post doesn't have any additions from the author yet.

What is HAR file (HTTP Archive)?
25 Aug 18:23

What is HAR file (HTTP Archive)?

meme code
meme code@memecode
What is Bubble Sort (algorithm explanation)?
16 Sep 18:42

What is Bubble Sort (algorithm explanation)?

meme code
meme code@memecode
What is exponential growth?
16 Sep 18:57

What is exponential growth?

meme code
meme code@memecode
What is factorial complexity?
16 Sep 19:03

What is factorial complexity?

meme code
meme code@memecode
What is NP-complexity?
16 Sep 19:31

What is NP-complexity?

meme code
meme code@memecode
Offset vs Cursor Pagination in Rails: What to Choose and Why
24 Sep 15:22

Offset vs Cursor Pagination in Rails: What to Choose and Why

meme code
meme code@memecode
What is ivar in Ruby /Rails?
19 Oct 20:12

What is ivar in Ruby /Rails?

meme code
meme code@memecode
Basic methods of authentication in the API
19 Oct 20:26

Basic methods of authentication in the API

meme code
meme code@memecode
How do OAuth 1 differ from OAuth 2
19 Oct 20:34

How do OAuth 1 differ from OAuth 2

meme code
meme code@memecode
What is ORM and why is it needed?
26 Oct 14:00

What is ORM and why is it needed?

meme code
meme code@memecode
MCP: A new internet where sites communicate with AI
04 Nov 11:43

MCP: A new internet where sites communicate with AI

meme code
meme code@memecode
Why is TOON better than JSON when working with AI?
14 Nov 15:14

Why is TOON better than JSON when working with AI?

meme code
meme code@memecode