Skip to main content
⚑ OpenDevDocs
PostgreSQL
databasepostgresqlGuideIntermediate

PostgreSQL Roles, Permissions & Row-Level Security (RLS)

Manage database permissions with GRANT/REVOKE, schema ownership, and secure multi-tenant data using Row-Level Security (RLS).

Verified: 2026-09-30
Tested on:postgres 16.x
Platforms:
linuxmacoswindows
#postgresql#permissions#security#rls#grant

1. Granting and Revoking Table Permissions

-- Grant read-only access to a reporting role
GRANT SELECT ON ALL TABLES IN SCHEMA public TO analytics_role;

-- Grant standard CRUD permissions to application role
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_backend;

-- Grant sequence usage (required for SERIAL / IDENTITY columns)
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO app_backend;

-- Revoke dangerous permissions
REVOKE DROP, TRUNCATE ON ALL TABLES IN SCHEMA public FROM app_backend;

2. Automatic Permissions on Future Tables

Configure ALTER DEFAULT PRIVILEGES so newly created tables automatically receive appropriate grants:

ALTER DEFAULT PRIVILEGES IN SCHEMA public 
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_backend;

ALTER DEFAULT PRIVILEGES IN SCHEMA public 
GRANT USAGE, SELECT ON SEQUENCES TO app_backend;

3. Row-Level Security (RLS)

Row-Level Security (RLS) restricts which individual table rows a database user can view or mutate based on security policies (essential for multi-tenant applications).

Enabling RLS on a Table

-- Step 1: Enable RLS
ALTER TABLE documents ENABLE ROW LEVEL SECURITY;

-- Step 2: Create access policy allowing users to see only their own documents
CREATE POLICY user_documents_isolation_policy ON documents
    FOR ALL
    TO app_user
    USING (owner_id = current_setting('app.current_user_id')::bigint)
    WITH CHECK (owner_id = current_setting('app.current_user_id')::bigint);

In your application backend (e.g. Node.js):

BEGIN;
SET LOCAL app.current_user_id = '123';
SELECT * FROM documents; -- Automatically filters strictly to owner_id = 123
COMMIT;

On this page