Sub topic 8
Who Bypasses RLS, and FORCE ROW LEVEL SECURITY
Prerequisites and the default rule
Assume you know basic PostgreSQL roles, GRANT, and CREATE TABLE. Row-level security (RLS) is evaluated after PostgreSQL checks ordinary table privileges. A role still needs SELECT, INSERT, UPDATE, or DELETE permission before an RLS policy can grant access to individual rows.
For a table with RLS enabled, PostgreSQL normally applies its policies to each command. If no applicable policy allows an operation, the role sees no rows or the operation fails. Some roles are exceptions because PostgreSQL trusts them to administer the database.
Roles that bypass RLS
These roles bypass RLS policies:
- A superuser always bypasses RLS.
- A role with the
BYPASSRLSattribute always bypasses RLS. - The table owner normally bypasses RLS.
The BYPASSRLS attribute is separate from ordinary table privileges. A role can have SELECT permission and still be unable to see rows because of RLS. Conversely, a role with BYPASSRLS can see rows that policies would hide, provided it has the required table privilege.
You can inspect these attributes with:
SELECT rolname, rolsuper, rolbypassrls
FROM pg_roles
WHERE rolname IN ('app_user', 'reporting_admin');A role with BYPASSRLS is highly trusted. Grant this attribute only when the role genuinely needs unrestricted access.
Making the owner obey policies
Use FORCE ROW LEVEL SECURITY when the table owner must also be subject to policies:
ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
ALTER TABLE documents FORCE ROW LEVEL SECURITY;FORCE affects the table owner, but it does not defeat a superuser or a role with BYPASSRLS. Those roles remain exempt.
You can reverse the setting with:
ALTER TABLE documents NO FORCE ROW LEVEL SECURITY;The setting belongs to the table, not to an individual query. Therefore, it is useful when an application connects as the table owner and you still need tenant or user isolation.
flowchart TD
A[Query reaches a table] --> B{Has ordinary table privilege?}
B -- No --> C[Permission denied]
B -- Yes --> D{Superuser or BYPASSRLS?}
D -- Yes --> E[Policies bypassed]
D -- No --> F{Table owner and NO FORCE?}
F -- Yes --> E
F -- No --> G[Apply RLS policies]caption: How PostgreSQL decides whether to apply RLS alt: Flowchart showing privilege checks before PostgreSQL either bypasses RLS or evaluates the table policies.
Security-definer code and ownership
A SECURITY DEFINER function runs with the privileges of its owner. If that owner bypasses RLS, the function can read or change rows that the calling role cannot access. This can be intentional, such as for a carefully designed administrative function, but it can also accidentally expose tenant data.
Keep such functions narrow. Validate input, set a safe search_path, and return only the data the caller should receive. FORCE ROW LEVEL SECURITY can make the table owner obey policies, but it does not remove the need to review security-definer functions.
Choosing the setting
Use ordinary ENABLE ROW LEVEL SECURITY when a trusted table owner needs unrestricted maintenance access. Use FORCE ROW LEVEL SECURITY when the owner is also an application role, or when you need policies enforced consistently during owner-owned code paths.
Test both the normal application role and every administrative role. A policy tested only as a table owner may appear correct while silently bypassing RLS.
Exercise
A table has RLS enabled and a policy that permits each user to see only rows where ownername = currentuser. The table owner runs SELECT and sees every row. What change makes the owner subject to the policy, and which roles still bypass it?
Show answer
Run ALTER TABLE table_name FORCE ROW LEVEL SECURITY. The owner then follows the policy, but superusers and roles with the BYPASSRLS attribute still bypass RLS.