1

I am trying out the Postgres row level security feature and not being able to see it working. Not sure what I am missing.

CREATE TABLE tenants (id uuid PRIMARY KEY, name TEXT);

INSERT INTO tenants (id, name) values ('ec5e9a6b-ed71-4e41-bc1e-11dac6808e41', 'Tenant1'), ('a684edc2-19b2-40d6-b679-519a6f736981', 'Tenant2');

ALTER TABLE tenants ENABLE ROW LEVEL SECURITY ;
ALTER TABLE tenants FORCE ROW LEVEL SECURITY;

SET app.tenant_id = 'ec5e9a6b-ed71-4e41-bc1e-11dac6808e41';

CREATE POLICY tenants_policy ON tenants FOR ALL USING ( current_setting('app.tenant_id')::uuid = id );

SELECT * FROM tenants;

For the last select, I expected it to return only one row with id 'ec5e9a6b-ed71-4e41-bc1e-11dac6808e41' but it is returning both rows. What am I missing? Thank you!

1 Answer 1

3

Your example works for me. There are a few possibilities:

  • The current user is a superuser.

  • The current user is defined with BYPASSRLS.

  • The configuration parameter row_security is off.

Sign up to request clarification or add additional context in comments.

1 Comment

It was the super user (facepalm). Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.