pgquery

pgquery / RLS policy editor

Chapter 8 · Policies

Edit Row Level Security policies without guesswork

Row Level Security decides which rows each role can see or change. pgquery shows every table, whether RLS is on, and the policies that apply, and it writes the SQL for you to review.

See the state of every table

The Policies screen lists tables with RLS on or off and the policies attached to each: the command (SELECT, INSERT, UPDATE, DELETE or ALL), the roles it targets, and its USING and WITH CHECK expressions.

Create a policy

Pick a table, a command and roles, then write the expressions. pgquery shows the generated statement before anything runs, for example:

ALTER TABLE playlists ENABLE ROW LEVEL SECURITY;

CREATE POLICY public_playlists_visible
  ON playlists FOR SELECT
  USING (is_public);

Find the gaps

The Security Advisor flags tables that have RLS disabled, alongside superuser roles, public schema grants, SSL and password hashing settings, and gives an overall score.

Remember

  • Table owners and superusers bypass RLS unless you use FORCE ROW LEVEL SECURITY.
  • With RLS on and no policies, a table returns no rows to ordinary roles.
  • Policies combine with OR within a command unless you create them AS RESTRICTIVE.

Questions

Does enabling RLS lock out my application?

It can. With RLS on and no matching policy, ordinary roles see no rows. Create policies first, then enable it, and test with the role your app uses.

Can I test a policy as another role?

Run SET ROLE some_role; in the SQL editor, query the table, then RESET ROLE.

More chapters