PostgreSQL Permissions¶
PostgreSQL permissions work in layers. Understanding the layers makes debugging much easier.
Permission layers¶
- Authentication
- Role attributes
- Database privileges
- Schema privileges
- Object privileges
- Ownership
- Role membership
- Default privileges
Role attributes¶
Common role attributes:
LOGINSUPERUSERCREATEDBCREATEROLE
Check role attributes:
SELECT rolname, rolsuper, rolcreatedb, rolcreaterole, rolcanlogin
FROM pg_roles
WHERE rolname = 'tenant_user';
Database privileges¶
Important database privileges:
CONNECTCREATETEMPORARY
Example:
GRANT CONNECT ON DATABASE mydb TO tenant_user;
Schema privileges¶
Important schema privileges:
USAGECREATE
Example:
GRANT USAGE ON SCHEMA public TO tenant_user;
Table privileges¶
Common table privileges:
SELECTINSERTUPDATEDELETE
Example:
GRANT SELECT, INSERT, UPDATE, DELETE
ON ALL TABLES IN SCHEMA public
TO tenant_user;
Sequence privileges¶
Sequence permissions are separate from table permissions.
GRANT USAGE, SELECT, UPDATE
ON ALL SEQUENCES IN SCHEMA public
TO tenant_user;
Useful inspection commands¶
\du
\l
\dn+
\dp
Default privileges¶
Grants on existing tables do not automatically apply to future tables.
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO tenant_user;