Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

PostgreSQL Roles and Privileges

Direct answer

PostgreSQL roles are users and groups in one concept; privileges are granted on databases, schemas, tables and sequences, and default privileges cover future objects.

When to use this

Use it when setting up app access or when permission denied errors occur.

First 30 seconds

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;.

Operational verification

role → membership → schema USAGE → object privileges

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it when setting up app access or when permission denied errors occur.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

Grant on the schema (USAGE) and the tables. Use ALTER DEFAULT PRIVILEGES for new tables. Don't use superuser for applications.

Key checks
  • \du — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -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

RoleHasUsed by
shop_owner (NOLOGIN)Owns schema and tablesNobody directly
shop_migrateMember of shop_ownerMigrations / deploy jobs
shop_rw (NOLOGIN)DML on tables, USAGE on sequencesGroup role
shop_app (LOGIN)Member of shop_rwApplication

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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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
Terminology layer

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.

Aliases that resolve here
postgresql roles and privilegespostgresql roles grantalter default privileges
Confirmation

Confirm action

Continue?