In multi-user applications, restricting access at the table level is often insufficient. Two users might both need access to the orders table, yet each should only see their own orders. If enforcement happens only in application code, a single query that forgets to include a user filter can expose data that should remain private.
Row-Level Security (RLS) in PostgreSQL provides an access control layer at the row level. RLS policies determine which rows a database role may read, insert, update, or delete. This mechanism helps strengthen application security design, but must still be combined with proper table privileges, authentication, and testing.
What Is Row-Level Security?
Normally, PostgreSQL access privileges through GRANT control whether a role may perform operations on tables or columns. RLS adds more granular rules: specific rows can be allowed or denied based on conditions—for example, whether a user_id column matches the identity of the user running the query.
RLS does not replace GRANT. Roles still need basic privileges such as SELECT or UPDATE. Once RLS is enabled, operations on rows must also satisfy applicable policies. If RLS is active but no policy permits an operation, PostgreSQL applies a default-deny approach for ordinary row access.
Example Implementation on an Orders Table
Suppose an application stores orders with an owner_name column. The following simple example shows how to enable RLS and allow each role to see rows where the name matches the current database role.
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
owner_name name NOT NULL,
item text NOT NULL
);
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
GRANT SELECT ON orders TO app_user;
CREATE POLICY orders_read_own
ON orders
FOR SELECT
TO app_user
USING (owner_name = current_user);In this example, the policy applies only to SELECT operations by the app_user role. The expression in USING is evaluated to determine which rows are visible. Rows belonging to other roles will not appear in ordinary query results for that role.
This example works well for explaining concepts, but applications that use a single shared database account for all users require a different identity design. current_user refers to the active database role, not automatically the application user who is logged in.
Understanding USING and WITH CHECK
RLS distinguishes between rules for existing rows and rows being written:
USINGdetermines which rows may be selected or targeted by operations such asUPDATEandDELETE.WITH CHECKdetermines whether new row values created or produced byINSERTandUPDATEsatisfy the policy.
For example, an ownership policy can prevent users from moving someone else’s order into their own account by changing the owner column. If WITH CHECK is omitted, behavior depends on the policy type and PostgreSQL defaults; for sensitive designs, write policy intent explicitly to facilitate auditing.
CREATE POLICY orders_owner_update
ON orders
FOR UPDATE
TO app_user
USING (owner_name = current_user)
WITH CHECK (owner_name = current_user);Multiple Policies Can Interact
PostgreSQL can apply more than one policy to a table. Permissive policies, the default type, are combined with OR logic; restrictive policies are combined with AND logic. Therefore, adding a new policy does not always tighten access: one overly broad permissive policy can open unintended access.
Specify target roles and operations for each policy. Avoid general rules for PUBLIC unless truly necessary. Document the rationale for each policy, then test the combined effect of all policies, not just checking them individually.
Limitations and Pitfalls to Avoid
- Do not rely on RLS as the sole control. Grant minimum table privileges through
GRANT, separate application and administrative roles, and protect database credentials. - Check privileged roles. Superusers and roles with the
BYPASSRLSattribute bypass RLS policies. Table owners typically bypass them as well, unless changed withFORCE ROW LEVEL SECURITY. - Be cautious with shared connections. If all application requests use a single database role,
current_useralone cannot distinguish end users. Identity context must be designed so it cannot be forged through user input. - Test write operations. Ensure policies protect
INSERT,UPDATE, andDELETE, not just data reads. - Watch for special paths. Operations such as
TRUNCATEare not controlled by RLS policies, so table-level privileges remain important.
Pre-Production Checklist
- Enable RLS on every table containing cross-user or cross-tenant data.
- Create separate policies for operations and roles that are actually needed.
- Test with ordinary application roles, not just table owners or superusers.
- Add negative test cases: user A attempting to read, modify, or delete user B’s rows.
- Audit policies after schema changes and include RLS testing in your development pipeline.
The official PostgreSQL documentation explains syntax, policy combinations, and RLS exceptions in more detail: Row Security Policies.
Conclusion
PostgreSQL RLS helps enforce row-level access restrictions directly in the database, reducing the risk of cross-user data exposure from mistaken application queries. The benefits are greatest when policies are designed around least-privilege principles, tested with realistic roles, and reviewed alongside other database controls. RLS is not a replacement for secure authentication design, but it is a powerful additional defense layer when implemented correctly.

Leave a comment