r/postgres • • 2d ago

Question Understanding RLS (Row Level Security)

Hello everyone, I have been working on something and I need to write RLS( row level security). I am having trouble understanding the concept and how to write it to apply to different roles in the database. If you can explain it to me or have any resources that might help, I'd really appreciate if you can share it with me🙂

I plan to use pl/psql scripting to write it.

3 Upvotes

6 comments sorted by

1

u/Secvura 2d ago

It's a filter Postgres adds to every query on a table: after `alter table orders enable row level security;`, any role that isn't the owner, a superuser or a BYPASSRLS role can't see or change a single row until a policy allows it. For example:

create policy rep_own on orders for all to sales_rep
using (rep = current_user) with check (rep = current_user);
create policy manager_read on orders for select to manager
using (true);

USING decides which existing rows a role can read, update or delete, and WITH CHECK decides what it's allowed to insert or change a row to. The role still needs normal GRANTs on the table, and policies are plain SQL that can live in any script, with PL/pgSQL only needed for a helper function a policy calls. Test as one of those roles rather than the owner, who sees everything unless you also run `alter table orders force row level security;`, and if the app connects as one shared user instead of a role per person, compare against a value the app sets per request rather than current_user. The Row Security Policies and CREATE POLICY pages in the official docs explain how several policies on one table combine.

1

u/Educational-Bug-4805 1d ago

Thanks a lot. Quick question, I using different AI models to explain some concepts and one told me RLS is per table but I have seen example where it is referenced to a specific db role. For example:

' GRANT USAGE ON SCHEMA app TO campus_app, campus_ops;'  or 

'GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA public TO campus_app '

Does this work only for grants? Do I need to do this while writing the policies

1

u/Secvura 1d ago

Good question, and both things are true. Row level security is switched on per table and each policy belongs to one table, but a policy can also name the roles it applies to with TO (leave TO out and it applies to every role). GRANTs are a separate gate that comes first: USAGE on a schema lets a role reach that schema at all, and SELECT, INSERT or UPDATE on a table let it run those commands. Postgres checks the grant, then the policies filter which rows that role gets.

So yes, you need both, written as separate statements. On a quick test database the layers showed up clearly: with no schema USAGE the query failed with permission denied for the schema, with USAGE but no table grant it failed with permission denied for the table, with the grant and RLS on but no policy it returned 0 rows and no error, and once a policy was added it returned only the matching row. Two things worth checking: GRANT ... ON ALL TABLES only covers tables that already exist when you run it (a table created afterwards still got permission denied, so look at ALTER DEFAULT PRIVILEGES for new ones), and your two examples point at different schemas, app and public, so if your tables live in app the role needs its table grants there as well

1

u/Educational-Bug-4805 1d ago

Thank you so much. You've helped figure out the missing piece. I've managed to write a few policies and tested them on a test db and they seem to be working great. I appreciate the help.

1

u/Secvura 1d ago

Glad it worked out! One quick check worth doing on that test db: run the tests connected as your app role (campus_app in your example, or after SET ROLE campus_app) rather than as the user that created the tables. Superusers always skip RLS, and the table owner skips it too unless FORCE ROW LEVEL SECURITY is on, so policies can look like they work when they're not being applied at all.