Sub topic 8
Who bypasses RLS, and FORCE ROW LEVEL SECURITY
This chapter assumes you can write CREATE POLICY statements with USING and WITH CHECK, and that you understand PostgreSQL roles, GRANT, and SET ROLE. The earlier chapters covered the default-deny behavior. Here the question is which sessions never see that behavior at all, and how to close the gaps you control.
Policies are not applied to everyone
Enabling RLS on a table does not mean every query against it is filtered. PostgreSQL decides per query, based on the current role, whether policies apply. A policy that is never evaluated is a common reason for the report "RLS works in my tests but not in production."
Three groups of roles skip policies:
- Superusers.
- Roles that have the
BYPASSRLSattribute. - The table owner, unless the table has
FORCE ROW LEVEL SECURITY.
Everyone else is subject to the table's policies, and gets default deny if no policy matches.
Superusers and BYPASSRLS
A superuser always bypasses RLS. This cannot be changed with any table setting. Any session that connects as a superuser, including a "temporary" debugging session, sees every row.
BYPASSRLS is a role attribute that gives the same exemption without full superuser power. It is meant for roles such as backup, replication-adjacent, or reporting accounts that must read everything.
-- Run as a superuser. Only superusers can grant BYPASSRLS.
CREATE ROLE reporting LOGIN BYPASSRLS;
-- Remove it later:
ALTER ROLE reporting NOBYPASSRLS;PostgreSQL checks the attribute on the current role. Like other role attributes (LOGIN, SUPERUSER, CREATEDB), BYPASSRLS is not inherited through role membership. Granting a role membership in reporting does not make that role bypass RLS. When a superuser runs SET ROLE alice, the check is made against alice, so policies apply. This is why the SET ROLE testing in the earlier chapters works from a superuser session.
To audit who can bypass:
SELECT rolname, rolsuper, rolbypassrls
FROM pg_roles
WHERE rolsuper OR rolbypassrls
ORDER BY rolname;Review this list regularly. Every row in it is a role for which your policies do nothing.
The table owner
The owner of a table is exempt from its policies by default. The reasoning is that the owner controls the table's definition and could drop the policies anyway. The result is a frequent mistake: the application connects as the same role that ran the migrations, so it owns the tables, and RLS is silently ignored.
The following script shows the effect. Run it as a superuser.
CREATE ROLE app_owner;
CREATE ROLE alice;
CREATE TABLE notes (
id bigserial PRIMARY KEY,
owner_name text NOT NULL DEFAULT current_user,
body text
);
ALTER TABLE notes OWNER TO app_owner;
GRANT SELECT, INSERT ON notes TO alice;
ALTER TABLE notes ENABLE ROW LEVEL SECURITY;
CREATE POLICY notes_owner ON notes
USING (owner_name = current_user)
WITH CHECK (owner_name = current_user);
SET ROLE alice;
INSERT INTO notes (body) VALUES ('alice note');
SELECT body FROM notes; -- 1 row: alice's own note
RESET ROLE;
SET ROLE app_owner;
SELECT body FROM notes; -- 1 row: the owner bypasses the policy,
-- even though owner_name is 'alice'
RESET ROLE;app_owner has no matching rows under the policy, yet it reads alice note. The policy exists and is enabled, but the owner is exempt.
FORCE ROW LEVEL SECURITY
FORCE removes the owner exemption:
ALTER TABLE notes FORCE ROW LEVEL SECURITY;
SET ROLE app_owner;
SELECT body FROM notes; -- 0 rows: the policy now applies to the owner
INSERT INTO notes (body) VALUES ('owner note'); -- allowed: owner_name = 'app_owner'
SELECT body FROM notes; -- 1 row: 'owner note'
RESET ROLE;
SELECT body FROM notes; -- superuser: 2 rows, FORCE does not affect superusersUndo it with ALTER TABLE notes NO FORCE ROW LEVEL SECURITY;. The two flags are stored in pg_class, and you can check them with:
SELECT relname, relrowsecurity AS enabled, relforcerowsecurity AS forced
FROM pg_class
WHERE relkind = 'r' AND relrowsecurity
ORDER BY relname;Note what FORCE does not change. Superusers and BYPASSRLS roles still skip policies on a forced table.
Why owners matter beyond direct connections
Code that runs with the owner's privileges also inherits the exemption. A SECURITY DEFINER function owned by the table owner can read every row of an unforced table, whoever calls it. A view owned by the table owner behaves the same way by default, which the views chapter returns to with security_invoker. FORCE makes the owner's own access subject to policies. That includes access through these indirect paths, because they all run as the owner.
Operations that always ignore policies
Some operations skip RLS regardless of role:
| Operation | Behavior |
|---|---|
| Foreign key checks | Referential integrity checks always see all rows, so a policy cannot hide a referenced row from the check. |
| `TRUNCATE` | Not row-by-row, so policies are not consulted. Control it with the `TRUNCATE` privilege. |
| Superuser or `BYPASSRLS` sessions | All policies skipped. |
The foreign key behavior is a side channel: a role can learn that a hidden key exists from an error message. The pitfalls chapter covers this.
The row_security setting
The row_security parameter does not mean "turn RLS off." It controls what happens when a query would be subject to a policy:
on(the default): policies are applied normally.off: for a role that is subject to policies, the query fails with an error instead of returning filtered rows. Roles that bypass RLS are unaffected.
This is designed for pg_dump, which sets row_security = off by default. A dump taken by a role that policies apply to fails loudly instead of producing a backup that is silently missing rows. Use pg_dump --enable-row-security only if you really want a dump of the rows that role can see.
A safe role layout
A layout that avoids the common failures:
- Create a dedicated owner role for the schema's tables, for example
app_owner, with no login. Run migrations as it, orSET ROLEto it. - Create a separate runtime role, for example
app_user, withLOGINand only the table privileges it needs. It does not own tables. - Run
FORCE ROW LEVEL SECURITYon every table that has policies. It is a safeguard in case a code path later runs as the owner. - Keep
BYPASSRLSfor named maintenance roles, and keep the application's credentials away from them. - Check
pg_rolesandpg_classin a test or deployment step, and fail the build if a tenant table is not forced or if an unexpected bypass role exists.
Managed database services often give you an administrator account that is either the owner of what you create or has bypass rights. Check what yours has, and do not connect the application with it.
Common mistakes
- Testing policies as a superuser or as the table owner and concluding they do nothing. Test as the real runtime role.
- Forgetting that
ENABLEandFORCEare separate commands.FORCEalone does not turn RLS on, because policies are only enforced when RLS is enabled. - Granting
BYPASSRLSto the application role "to fix a permissions error." The error was probably a missing policy orGRANT. Fix that instead. - Assuming membership in a bypass role confers the exemption. It does not, but a login role that has the attribute itself does.
Exercise
The application connects as app_user, which does not own the invoices table. A nightly job connects as app_owner, which owns it. Both can read invoices. The table has RLS enabled and a tenant policy, but not FORCE. The nightly job reports invoices from all tenants. Why, and what is the minimal change that makes the job's queries subject to the policy? What would still read all rows afterward?
Show answer
app_owner owns the table, and owners are exempt from policies unless FORCE ROW LEVEL SECURITY is set. Run ALTER TABLE invoices FORCE ROW LEVEL SECURITY;. After that the policy applies to app_owner, so the job sees only rows matching the tenant setting (and none if the setting is unset). Superusers and roles with BYPASSRLS would still read all rows. If the job really needs every tenant's invoices, give it a dedicated role with BYPASSRLS instead of relying on ownership.