PostgreSQL

PostgreSQL Privilege Management: Roles, Default Privileges, and Least Privilege

Implement least-privilege access in PostgreSQL with role hierarchies, ALTER DEFAULT PRIVILEGES, schema separation, and privilege auditing queries.

JusDB Team
Published April 23, 2025
5 min read

Proper privilege management in PostgreSQL is the foundation of database security. This guide covers roles, schema permissions, the principle of least privilege, and common mistakes.

Role Hierarchy

sql
-- Create base roles (not login roles)
CREATE ROLE readonly;
CREATE ROLE readwrite;
CREATE ROLE app_admin;

-- Grant privileges to roles
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO readwrite;
GRANT ALL ON ALL TABLES IN SCHEMA public TO app_admin;

-- Create login users and assign roles
CREATE USER reporting_user WITH LOGIN PASSWORD 'r3port!';
GRANT readonly TO reporting_user;

CREATE USER app_service WITH LOGIN PASSWORD 'srv_p@ss';
GRANT readwrite TO app_service;

Default Privileges for Future Tables

sql
-- Without this, new tables won't inherit role grants
ALTER DEFAULT PRIVILEGES IN SCHEMA public
  GRANT SELECT ON TABLES TO readonly;

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

-- Also for sequences (needed for INSERT with serial/bigserial)
ALTER DEFAULT PRIVILEGES IN SCHEMA public
  GRANT USAGE, SELECT ON SEQUENCES TO readwrite;

Revoke Public Schema Access

sql
-- PostgreSQL 14 and earlier: public has CREATE on public schema by default
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON DATABASE mydb FROM PUBLIC;

-- PostgreSQL 15+: public schema CREATE revoked by default

Audit Existing Privileges

sql
-- Check table privileges
SELECT grantee, table_name, privilege_type
FROM information_schema.role_table_grants
WHERE table_schema = 'public'
ORDER BY table_name, grantee;

-- Check role memberships
SELECT r.rolname AS role, m.rolname AS member
FROM pg_auth_members am
JOIN pg_roles r ON r.oid = am.roleid
JOIN pg_roles m ON m.oid = am.member
ORDER BY role, member;
Warning: Granting superuser to application roles is a security anti-pattern. Application users should never have superuser, CREATEROLE, or CREATEDB privileges.

Schema Separation for Multi-App Databases

sql
-- Each app gets its own schema
CREATE SCHEMA app1;
CREATE SCHEMA app2;

CREATE USER app1_user WITH LOGIN;
GRANT USAGE ON SCHEMA app1 TO app1_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA app1 TO app1_user;
-- app1_user cannot see app2 schema at all

Key Takeaways

  • Use role-based access: create readonly/readwrite roles, assign users to roles
  • Set ALTER DEFAULT PRIVILEGES so future tables inherit the right grants
  • Revoke PUBLIC schema CREATE and database CONNECT privileges immediately after setup
  • Never grant superuser to application database users

JusDB Can Help

Privilege sprawl is a silent security risk. JusDB can audit your PostgreSQL permissions and implement a least-privilege access model. Get started.

Share this article

Database engineering notes

Articles like this one, in your inbox. No spam, unsubscribe anytime.

JusDB Team

Official JusDB content team

Keep reading

PostgreSQL 19 Beta: Every New Feature That Matters to DBAs

PostgreSQL 19 Beta 1 (June 4, 2026) brings parallel autovacuum, the native REPACK command for online table rebuilds, 2x faster inserts under foreign-key load, online logical replication without a restart, WAIT FOR LSN for read-your-writes consistency, and default changes (JIT off, lz4 TOAST, RADIUS removed). A DBA-focused walkthrough of what changed and what to test before GA.

PostgreSQL14 minJun 15, 2026
Read

PostgreSQL Performance Tuning Playbook: A Top-Down Method for Faster Queries

A repeatable, top-down method for tuning PostgreSQL: measure with pg_stat_statements, read plans with EXPLAIN (ANALYZE, BUFFERS), fix queries and indexes before parameters, then tune memory, I/O, WAL, connection pooling, and autovacuum — with a ready-to-adapt postgresql.conf baseline.

PostgreSQL22 minMay 31, 2026
Read

PostgreSQL Architecture Deep Dive: Process Model, MVCC, WAL & Replication Explained

Walk through PostgreSQL's multi-process architecture, shared/local memory layout, page-organized storage, MVCC tuple versioning, the WAL write path, the query execution pipeline, and physical + logical replication — all with ASCII flow diagrams that show how data and control actually move through the system.

PostgreSQL18 minMay 31, 2026
Read