October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

PostgreSQL Row-Level Security: A Beginner’s Guide to Policies

PostgreSQL row-level security adds per-row access rules on top of SQL privileges. Learn how to enable RLS, write policies, and understand their limits.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • 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.Support on Ko-Fi

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.