Sub topic 8
Database and schema privileges
This chapter covers the two outer layers of PostgreSQL access control: the database and the schema. It assumes you know how to create roles, run GRANT and REVOKE, and understand that a role can be a member of another role. Table, column, sequence and function privileges come in later chapters.
The access path
A query touches several objects before it reaches a row. Each layer has its own privilege, and a missing privilege at any layer stops the query.
Granting SELECT on a table does nothing for a role that cannot connect to the database or cannot see the schema. This is the most common reason a freshly granted privilege seems to have no effect.
Database privileges
Three privileges apply to a database.
| Privilege | Short code | What it allows |
|---|---|---|
| `CONNECT` | `c` | Opening a session in the database |
| `TEMPORARY` | `T` | Creating temporary tables in the database |
| `CREATE` | `C` | Creating new schemas in the database |
CREATE on a database is about schemas only. It does not let a role create tables, because tables live inside schemas and need CREATE on the schema.
CONNECT is checked only at login. If you revoke it from a role that is already connected, that session keeps running until it ends. To remove the role immediately, also call pg_terminate_backend() on its sessions.
CONNECT is a second gate after pg_hba.conf. The pg_hba.conf file decides whether a connection attempt is accepted from a given address with a given authentication method. The CONNECT privilege then decides whether the authenticated role may enter that particular database. Superusers skip the privilege check.
Grant database privileges with ON DATABASE:
GRANT CONNECT ON DATABASE appdb TO app_readonly, app_writer;
GRANT CREATE ON DATABASE appdb TO app_owner;
REVOKE TEMPORARY ON DATABASE appdb FROM app_readonly;The last line has no effect while PUBLIC still holds TEMPORARY. The next section explains why.
What PUBLIC holds by default
When you create a database, PUBLIC (a pseudo-role meaning every role) receives CONNECT and TEMPORARY. The catalog shows this as an empty ACL, meaning "defaults apply":
CREATE DATABASE appdb OWNER app_owner;
SELECT datname, datacl
FROM pg_database
WHERE datname = 'appdb';datacl is NULL for a new database. The built-in defaults are in force: the owner has everything, and PUBLIC has CONNECT and TEMPORARY. Any role you create later can connect to appdb unless you change this.
To lock the database down, remove the PUBLIC rights and grant back only what you intend:
REVOKE ALL ON DATABASE appdb FROM PUBLIC;
GRANT CONNECT ON DATABASE appdb TO app_readonly, app_writer;After the revoke, datacl becomes an explicit list. It looks like this, though the order of entries may differ:
{app_owner=CTc/app_owner,app_readonly=c/app_owner,app_writer=c/app_owner}Read each entry as grantee=privileges/grantor. An empty grantee, as in =Tc/app_owner, means PUBLIC. Once PUBLIC is gone from the list, the revoke has taken effect.
Every new database is a copy of the template database template1. If you revoke PUBLIC privileges inside template1, future databases start without those defaults.
Schema privileges
A schema has two privileges.
| Privilege | Short code | What it allows |
|---|---|---|
| `USAGE` | `U` | Looking up objects in the schema by name |
| `CREATE` | `C` | Creating objects in the schema, and renaming or dropping objects the role owns there |
USAGE on a schema is a precondition for using any object inside it, whatever privileges the object itself has. It does not grant access to the objects. A role with USAGE on shop and no table privileges still gets permission denied on shop.orders.
You can see the precondition at work. Suppose alice is a member of app_readonly, and app_readonly has SELECT on the table but no USAGE on the schema yet:
SET ROLE alice;
SELECT count(*) FROM shop.orders;
-- ERROR: permission denied for schema shop
RESET ROLE;
GRANT USAGE ON SCHEMA shop TO app_readonly;
SET ROLE alice;
SELECT count(*) FROM shop.orders;
RESET ROLE;The error names the schema, not the table. That tells you which layer failed. The second query now succeeds.
A related trap involves search_path. If you write an unqualified name such as orders, PostgreSQL skips schemas in the path that the role cannot use. You then see relation "orders" does not exist, not a permission error. A permission error means you wrote a qualified name like shop.orders.
A schema is created by CREATE SCHEMA. The role running it needs CREATE on the database. Use AUTHORIZATION to give the schema to another role:
CREATE SCHEMA shop AUTHORIZATION app_owner;The schema owner can create objects in it and grant privileges on it without needing separate grants. A schema owner can also drop objects other roles created in that schema, so keep schema ownership with a role you trust, such as app_owner.
The public schema
Every new database contains a schema named public. Its defaults changed in PostgreSQL 15.
| Version | Schema owner | `PUBLIC` privileges on `public` |
|---|---|---|
| 14 and earlier | The bootstrap superuser | `USAGE` and `CREATE` |
| 15 and later | `pg_database_owner` | `USAGE` only |
On version 14 and earlier, any role could create tables in public. This is an old security problem: a low-privilege role could create objects that shadow others in a shared search path. Remove the right explicitly on those versions, and run the same command on 15 and later to make the intent visible:
REVOKE CREATE ON SCHEMA public FROM PUBLIC;pg_database_owner is a special role that is implicitly a member of whichever role owns the current database. Because of it, the database owner owns public in each database without any extra setup.
You can also stop applications from relying on public at all. Keep application objects in a named schema such as shop, and set the search path for the application role:
ALTER ROLE app_writer SET search_path = shop;The setting applies from the role's next login.
Inspecting both layers
Use the has_*_privilege functions to test the effective answer. They take inheritance into account, so they report what a role can really do:
SELECT has_database_privilege('alice', 'appdb', 'CONNECT') AS can_connect,
has_schema_privilege('alice', 'shop', 'USAGE') AS can_use_shop,
has_schema_privilege('alice', 'shop', 'CREATE') AS can_create_in_shop;For the raw ACLs, query the catalogs. Role membership does not appear in these lists, only direct grants:
SELECT nspname, nspowner::regrole AS owner, nspacl
FROM pg_namespace
WHERE nspname = 'shop';In psql, \l+ shows database ACLs and \dn+ shows schema ACLs.
Common mistakes
- Granting table privileges and forgetting
USAGEon the schema. The error ispermission denied for schema. - Revoking a privilege from a named role while
PUBLICstill grants it. RevokingCONNECTfromapp_readonlychanges nothing ifPUBLICstill holdsCONNECT. Revoke fromPUBLICfirst, then grant back to specific roles. - Assuming
GRANT ... ON ALL TABLES IN SCHEMAincludes the schema itself. It covers only tables, so the schemaUSAGEgrant is separate. - Expecting a revoke of
CONNECTto disconnect a role that is already logged in. - Running
REVOKEon a database from inside another database. Database ACLs are stored in the shared catalog, so the command works from any database, but schema ACLs are per database. Connect to the right database before changing a schema.
Exercise
A role in group app_readonly has SELECT on shop.orders, but running SELECT * FROM shop.orders fails with "permission denied for schema shop". What is missing, and what command fixes it?
Show answer
The group role lacks USAGE on the schema. Fix it with GRANT USAGE ON SCHEMA shop TO app_readonly;. Table privileges only apply once the role can look up objects in the schema.
Exercise
You run REVOKE CONNECT ON DATABASE appdb FROM alice;, but alice can still connect. Give two possible reasons and one check for each.
Show answer
First, PUBLIC may still hold CONNECT. Check datacl in pg_database: a NULL value or an entry starting with = means PUBLIC has it, so run REVOKE CONNECT ON DATABASE appdb FROM PUBLIC. Second, alice may reach CONNECT through a group role, for example app_readonly. Check with has_database_privilege('alice', 'appdb', 'CONNECT') and revoke from the group role if appropriate.