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 important for Rails developers

Post cover: What is Row Security in PostgreSQL and why is it important for 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 underestimated feature - Row Level Security (RLS).
In short, it is data protection at the row level of the table, meaning the system determines which records the user can see or modify before the query even reaches your Rails code.

How it works

In a typical 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 you to delegate this check to the database 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 this, even if someone does SELECT * FROM posts,
PostgreSQL will automatically apply the condition so that the user sees only their rows.

How to integrate RLS in Rails

In Rails, you can set current_user.id in the database context before executing the query:
ActiveRecord::Base.connection.execute("SET app.current_user_id = #{current_user.id}")
Then all queries (Post.all, Post.find, even joins) will return only allowed data - without additional where in the code.
This is convenient for multi-user systems, SaaS, or APIs, where security should not rely solely on the application level.

Why is this even necessary

  • Database-level security — even if someone accidentally forgets where(user_id: ...), the data will not leak.
  • Simplicity of queries — you can write Model.all without thinking about filters.
  • Unified access control — rules are stored together with the data, not scattered across controllers and services.
RLS does not replace authorization in the application. It is an additional layer of protection that ensures that even at a low level no one gets "extra" data. Row Level Security is like where(user_id: current_user.id), but built into the database itself.

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

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

What is a 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
Main methods of authentication in API
19 Oct 20:26

Main methods of authentication in API

meme code
meme code@memecode
What are the differences between OAuth 1 and OAuth 2
19 Oct 20:34

What are the differences between OAuth 1 and 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 websites communicate with AI
04 Nov 11:43

MCP: a new internet where websites 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