Use it when setting up app access or when permission denied errors occur.
PostgreSQL Roles and Privileges
PostgreSQL roles are users and groups in one concept; privileges are granted on databases, schemas, tables and sequences, and default privileges cover future objects.
1. \du — roles and attributes.
2. \dp <table> — table privileges.
3. Schema USAGE granted?
4. Default privileges: \ddp.
5. Test as the role: SET ROLE app;.
role → membership → schema USAGE → object privileges
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Connect as the application role and run its typical queries. Confirm it cannot modify other schemas.
Step 2 Common failures Narrow the fault
Granting on existing tables does not cover tables created later. Applications that own their tables can drop them. Superuser application accounts bypass all checks.
Grant on the schema (USAGE) and the tables. Use ALTER DEFAULT PRIVILEGES for new tables. Don't use superuser for applications.
\du— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
Privileges are checked on every object access. Object owners have all privileges on their objects. Since PostgreSQL 15, the public schema no longer allows CREATE for all users by default.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Layers of access
To read shop.public.orders, a role needs CONNECT on the database, USAGE on the schema and SELECT on the table. Missing any one gives permission denied at that layer.
Owner, migration role and application role
| Role | Has | Used by |
|---|---|---|
shop_owner (NOLOGIN) | Owns schema and tables | Nobody directly |
shop_migrate | Member of shop_owner | Migrations / deploy jobs |
shop_rw (NOLOGIN) | DML on tables, USAGE on sequences | Group role |
shop_app (LOGIN) | Member of shop_rw | Application |
The application cannot drop or alter tables because it does not own them.
Default privileges for tables created later
ALTER DEFAULT PRIVILEGES FOR ROLE shop_owner IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO shop_rw;
ALTER DEFAULT PRIVILEGES FOR ROLE shop_owner IN SCHEMA public
GRANT USAGE, SELECT ON SEQUENCES TO shop_rw;
Default privileges apply only to objects created by the role named in FOR ROLE. If migrations run as a different role, new tables are not covered — the usual cause of "it worked until the last migration".
Inspecting privileges
\dp orders -- shop_rw=arwd/shop_owner (a=INSERT r=SELECT w=UPDATE d=DELETE)
\ddp -- default privileges
SELECT has_table_privilege('shop_app', 'orders', 'UPDATE');
Built-in roles worth using
pg_read_all_data, pg_write_all_data (bulk access for tools), pg_monitor (monitoring without superuser), pg_maintain (PostgreSQL 17+: VACUUM, ANALYZE, REINDEX). Avoid giving applications superuser; it bypasses every check above.
Common mistakes
- Applications using the superuser role.
ReferenceConfiguration & state1 section · depth on demand
Commands
CREATE ROLE app_rw NOLOGIN;
GRANT CONNECT ON DATABASE shop TO app_rw;
GRANT USAGE ON SCHEMA public TO app_rw;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_rw;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_rw;
CREATE ROLE app LOGIN PASSWORD '<password>' IN ROLE app_rw;
\du ; \dp orders
Documented example, not captured output. Since PostgreSQL 15, ordinary users cannot create objects in public by default. Use predefined roles (pg_read_all_data, pg_monitor) where they fit.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.