PostgreSQL row-level security (RLS) adds rules that control which rows a database role can read or change. It works alongside ordinary SQL privileges: a role needs both the relevant table privilege and permission under an applicable RLS policy. If RLS is enabled but no policy applies, access is denied by default.
What row-level security does
Ordinary SQL privileges determine whether a role may perform operations such as SELECT, INSERT, UPDATE, or DELETE on a table. RLS adds a further decision at the row level: which rows that role may access through those operations.
As an Amazon Associate I earn from qualifying purchases.
That makes RLS useful for rules such as allowing members of a managers role to access only rows assigned to them, or allowing each user to access only their own row. A policy is a Boolean rule PostgreSQL applies to rows for particular roles and commands. It does not replace a GRANT, and creating a policy alone does not turn RLS on. PostgreSQL 18: Row Security Policies
Enable RLS, then add a policy
The table owner enables row security on the table. Once enabled, normal access must pass an applicable policy; without one, PostgreSQL uses default deny.
#1 Best Overall
ALTER TABLE accounts ENABLE ROW LEVEL SECURITY;
For example, this policy applies to the managers role and permits access to rows whose manager value matches the current database user:
CREATE POLICY account_managers ON accounts TO managers
USING (manager = current_user);
In this PostgreSQL 18 example, because no separate WITH CHECK expression is supplied, PostgreSQL reuses the USING expression to check proposed rows too. This constrains both which existing rows the role can access and which rows it can create or modify. PostgreSQL 18 policy examples
Rank #2
The example assumes database roles identify the managers. If many application users connect through one shared database role, current_user identifies that shared role, not automatically the individual user. The application’s identity-to-database design needs to account for that distinction.
How USING and WITH CHECK differ
USING evaluates existing rows considered by an operation. It determines which rows are visible or eligible to be targeted. WITH CHECK evaluates the proposed row values for an INSERT or UPDATE, preventing a permitted user from writing a row that violates the policy.
Rank #3
A policy can use the same expression for both purposes, or define separate expressions when the allowed existing rows and acceptable new values differ. If a policy supports both and omits WITH CHECK, its USING expression is reused for the check. The exact command behavior is documented in the PostgreSQL 17 CREATE POLICY reference; check syntax against the PostgreSQL major version you run. PostgreSQL 17: CREATE POLICY
Choose policy scope and combination rules
Policies can be scoped to database roles and commands. A policy may cover all supported commands or be limited to SELECT, INSERT, UPDATE, or DELETE. The role scope and command scope determine when that policy is considered.
When multiple applicable policies exist, their type affects how their conditions combine:
Recommended Free Tools
- Permissive: applicable permissive policies combine with OR, so satisfying any one can allow access.
- Restrictive: applicable restrictive policies combine with AND, so all applicable restrictive conditions must be satisfied.
For each policy, decide which roles and commands it should govern, whether it should allow access or impose an additional condition, and whether reads and proposed writes need the same expression. PostgreSQL 17: CREATE POLICY
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Know which roles bypass RLS
Superusers and roles with the BYPASSRLS attribute bypass row-security checks. Table owners normally bypass them as well. An owner can make RLS apply to their own access by forcing row security:
ALTER TABLE accounts FORCE ROW LEVEL SECURITY;
Because owners and elevated roles can behave differently from ordinary application roles, test access using the role that will actually run the application. Confirm whether it owns the table, is a superuser, or has BYPASSRLS. PostgreSQL 18: Row Security Policies
Limits to what RLS hides
RLS does not govern whole-table TRUNCATE or REFERENCES operations. Referential-integrity checks, including uniqueness checks, are also not filtered by policies. As a result, constraint outcomes can sometimes reveal indirect information about rows that the caller cannot directly see. RLS should not be treated as a guarantee that every fact about protected rows is concealed. PostgreSQL 18: Row Security Policies PostgreSQL 17: CREATE POLICY
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




