← homeProgramming (Програмування)

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

Row Level Security in PostgreSQL is data protection at the row level of a table. It allows for controlling access directly in the database, enhancing the security of Rails applications.

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, it is data protection at the row level of the table, meaning the system decides which records a 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 a 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 a 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 the 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 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 receives "extra" data. Row Level Security is like where(user_id: current_user.id), but built into the database itself.

🔥 More posts

All posts
What is a HAR file (HTTP Archive)?
Programming (Програмування)Aug 25, '25 18:23

What is a HAR file (HTTP Archive)?

HAR file (HTTP Archive) is a .har format that stores a log of the browser's interaction with the ...

What is exponential growth?
Sep 16, '25 18:57

What is exponential growth?

Exponential growth is a rapid increase in magnitude, where each subsequent step multiplies the re...

What is factorial complexity?
Programming (Програмування)Sep 16, '25 19:03

What is factorial complexity?

Factorial complexity is the rapid increase in the number of options, where for n elements the pos...

What is NP-complexity?
Programming (Програмування)Sep 16, '25 19:31

What is NP-complexity?

NP-completeness is a class of problems where finding a solution is extremely difficult, but verif...

What is ivar in Ruby / Rails?
Programming (Програмування)Oct 19, '25 20:12

What is ivar in Ruby / Rails?

ivar in Ruby is an instance variable, denoted by @. In Rails, it passes data from the controller ...

Main methods of authentication in API
Programming (Програмування)Oct 19, '25 20:26

Main methods of authentication in API

The main methods of authentication in the API on Ruby on Rails: Basic Auth, Token, JWT, and OAuth...

What is ORM and why is it needed?
Programming (Програмування)Oct 26, '25 14:00

What is ORM and why is it needed?

ORM is a technology that allows working with databases through code objects, simplifying developm...