Sub topic 7
PUBLIC and the defaults you did not set
Prerequisites and the PUBLIC pseudo-role
This chapter assumes that you can connect to PostgreSQL and understand databases, schemas, tables, and basic GRANT statements.
PUBLIC is a PostgreSQL pseudo-role. It means every role, including roles created later. You do not add users to PUBLIC. A privilege granted to PUBLIC applies to everyone unless another rule, such as row-level security, limits the operation.
PostgreSQL gives PUBLIC some privileges by default. These defaults apply at different levels:
| Object | Common default for `PUBLIC` | Effect |
|---|---|---|
| Database | `CONNECT`, `TEMPORARY` | Roles can connect and create temporary tables |
| `public` schema | `USAGE`; `CREATE` depends on PostgreSQL version and setup | Roles can resolve objects; some can create objects |
| New functions | `EXECUTE` | Roles can call functions |
| New tables | Usually no table data privileges | Roles cannot automatically read or change table rows |
The exact defaults can vary by PostgreSQL version and by changes made by an administrator. Inspect them instead of assuming.
Removing broad database access
A new database normally lets PUBLIC connect and create temporary tables. For an application database, you might remove those rights and grant access only to application roles:
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;This controls entry to the database. It does not grant access to schemas or tables. A role usually needs USAGE on a schema and an appropriate table privilege after it connects.
Restricting the public schema
The public schema is a shared namespace. On PostgreSQL 15 and later, CREATE on this schema is normally already revoked from PUBLIC. On older installations, remove it explicitly:
REVOKE CREATE ON SCHEMA public FROM PUBLIC;A safer design is to create an application-owned schema and grant USAGE only to the roles that need it:
CREATE SCHEMA shop AUTHORIZATION app_owner;
GRANT USAGE ON SCHEMA shop TO app_readonly, app_writer;USAGE lets a role reference objects in the schema. It does not grant SELECT, INSERT, or other table privileges.
PUBLIC and future objects
Object defaults belong to the role that creates the object. For example, revoke broad function execution only if your security design requires it:
REVOKE EXECUTE ON ALL FUNCTIONS IN SCHEMA shop FROM PUBLIC;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;The first statement affects functions that already exist. The second affects functions that app_owner creates later. It does not affect functions created by another owner.
How the rules combine
A role can receive a privilege directly, through membership in a group role, or through PUBLIC. Removing a privilege from PUBLIC does not remove a separate grant to that role.
Exercise
Why does revoking CONNECT from PUBLIC not make a table private by itself?
Show answer
CONNECT controls entry to the database. A role can still need schema and table privileges, and those privileges must also be restricted.