Sub topic 8
Database and Schema Privileges
This chapter assumes you know basic SQL, can connect to PostgreSQL with psql or another client, and understand that a server contains databases and each database contains schemas and tables.
PostgreSQL checks access in layers. A role first needs permission to connect to the database. After connecting, it needs permission to use a schema. It then needs privileges on the objects inside that schema, such as tables or sequences.
Database privileges
A database has three important privileges:
| Privilege | Allows |
|---|---|
| CONNECT | A role can connect to the database |
| CREATE | A role can create schemas and some database-level objects |
| TEMPORARY | A role can create temporary tables |
These privileges do not grant access to tables. For example, CONNECT lets a role open a session, but the role still needs schema and table privileges before it can query application data.
The PUBLIC pseudo-role represents every role. New databases normally grant CONNECT and TEMPORARY to PUBLIC. If that is too broad, revoke those rights and grant them to selected group roles:
-- Run as a database owner or administrator.
REVOKE ALL ON DATABASE appdb FROM PUBLIC;
GRANT CONNECT ON DATABASE appdb TO app_readonly, app_writer;
GRANT TEMPORARY ON DATABASE appdb TO app_writer;A role needs CONNECT even when it has excellent privileges inside the database. Conversely, CONNECT alone does not allow it to read or change application tables.
Schema privileges
A schema is a namespace that contains objects. A role needs USAGE on a schema to resolve and access objects in that schema. USAGE does not grant access to the tables themselves.
The CREATE schema privilege allows a role to create objects in that schema. Grant it only to roles that should create tables, views, functions, or other schema objects.
CREATE ROLE app_readonly NOLOGIN;
CREATE ROLE app_writer NOLOGIN;
CREATE SCHEMA shop;
GRANT USAGE ON SCHEMA shop TO app_readonly, app_writer;
GRANT CREATE ON SCHEMA shop TO app_writer;
REVOKE CREATE ON SCHEMA public FROM PUBLIC;Here, both group roles can use names such as shop.orders. Only app_writer can create a new object in shop. Neither role can read shop.orders until it also receives a privilege such as SELECT on that table.
A role may have table privileges without being able to reach the table through its schema. Missing USAGE commonly causes errors that look like an object is inaccessible even though a table-level GRANT exists.
How the checks fit together
A practical troubleshooting order
When a role cannot access an object, check the layers in this order:
- Can the role connect to the intended database?
- Does it have
USAGEon every schema in the object name? - Does it have the required privilege on the table, sequence, or function?
- Is the client using the expected database and
search_path?
Exercise
A role can connect to appdb but receives a permission error when selecting from shop.orders. It has SELECT on shop.orders. What additional privilege is most likely missing?
Show answer
USAGE on the shop schema. Database CONNECT and table SELECT do not replace schema USAGE.