Skip to content
On this page

@rls

Row-level security policies for PostgreSQL tables

Postgres

Example

Enable RLS and add a policy

Use @rls plus one or more @rls.policy tags on the table.

Input SQL
sql
-- @rls
-- @rls.policy(for: SELECT, to: authenticated, using: 'user_id = current_user_id()')
CREATE TABLE documents (
  id SERIAL PRIMARY KEY,
  user_id INTEGER NOT NULL,
  title TEXT NOT NULL
);
Compiled Output
sql
ALTER TABLE "documents" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "documents_authenticated_select" ON "documents"
  FOR SELECT
  TO authenticated
  USING (user_id = current_user_id());

Tags

@rls.policy

Create a row-level security policy (requires @rls on the table)

Targets: table

Arguments:

NameTypeRequiredValues
forstringNo
tostringNo
usingstringNo
checkstringNo

@rls

Enable row-level security on this table

Targets: table

Arguments: No arguments.

Lint Rules

rls.require-policy

Public tables should have RLS enabled

Default severity: warn