Sub topic 7
PUBLIC and the defaults you did not set
This chapter assumes you can create roles, use GRANT and REVOKE, and read basic psql output. You should also know that a server contains databases, and a database contains schemas and tables. The chapter covers a part of the privilege system that you never configured but that still affects access: the PUBLIC pseudo-role and the privileges new objects get automatically.
What PUBLIC is
PUBLIC is a keyword that means "every role, now and in the future". It is not a row in pg_roles, you cannot log in as it, and you cannot create a role with that name. You can only name it in GRANT and REVOKE.
A privilege granted to PUBLIC applies to every role, whatever its attributes or memberships. A role's effective access is therefore the union of three sources:
- privileges granted to the role directly
- privileges it inherits from group roles it belongs to
- privileges granted to
PUBLIC
Do not confuse the PUBLIC pseudo-role with the schema named public. They are unrelated objects that happen to share a name. Many mistakes come from mixing them up.
The defaults PostgreSQL applies
When you create an object, its privilege list starts out in a default state. For most object types the owner can do everything and nobody else can do anything. A few types also give PUBLIC some rights from the start.
| Object type | Default privileges for PUBLIC |
|---|---|
| Database | `CONNECT` and `TEMPORARY` |
| Function and procedure | `EXECUTE` |
| Language | `USAGE` |
| Data type and domain | `USAGE` |
| Schema `public` | `USAGE` (and `CREATE` before PostgreSQL 15) |
| Table, sequence, other schemas | none |
Tables are safe by default, because only the owner can read them. The risks come from the other rows. Any role that can reach your server can connect to your database. Any role that can see a function can run it. On PostgreSQL 14 and earlier, any role can create objects in the public schema.
Superusers bypass all of these checks. The rest of this chapter is about everyone else.
Why revoking from one role often does nothing
The most common mistake is revoking a privilege from a specific role while PUBLIC still holds it. Suppose alice should not connect to appdb:
REVOKE CONNECT ON DATABASE appdb FROM alice;
SELECT has_database_privilege('alice', 'appdb', 'CONNECT') AS can_connect;The query still returns true. The REVOKE removed a direct grant that alice never had. The CONNECT right comes from PUBLIC, and revoking it from one role cannot subtract it from "everyone".
The only way to block alice while keeping others in is to remove the privilege from PUBLIC and grant it back to the roles that need it. That is the pattern from the chapter on least-privilege design, applied here in detail.
REVOKE ALL ON DATABASE appdb FROM PUBLIC;
GRANT CONNECT ON DATABASE appdb TO app_readonly, app_writer;REVOKE ALL removes both CONNECT and TEMPORARY. If your applications use temporary tables, grant TEMPORARY back to the roles that need them:
GRANT TEMPORARY ON DATABASE appdb TO app_writer;Connection rights are also governed by pg_hba.conf, which decides which hosts and authentication methods are allowed. The CONNECT privilege is a second, independent layer that applies after authentication succeeds.
Reading the defaults in the catalogs
PostgreSQL stores privileges as an access control list (ACL) in a column named datacl, nspacl, proacl, and so on. A NULL value means "the built-in defaults". Once you run a GRANT or REVOKE, the full list is written out.
Each entry has the form grantee=privileges/grantor. An empty grantee means PUBLIC. The letters stand for privileges: c is CONNECT, T is TEMPORARY, C is CREATE, U is USAGE, and X is EXECUTE.
SELECT datname, datacl
FROM pg_database
WHERE datname = 'appdb';A default database shows NULL. In psql, \l shows the expanded default as =Tc/postgres and postgres=CTc/postgres, assuming postgres owns the database. The first entry is PUBLIC holding TEMPORARY and CONNECT. After the REVOKE and GRANT above, the list looks like this:
{postgres=CTc/postgres,app_readonly=c/postgres,app_writer=c/postgres}There is no entry with an empty grantee, so PUBLIC has nothing left. Scanning for = at the start of an entry is the fastest way to find PUBLIC grants in any ACL.
To find every function that PUBLIC can still execute in the shop schema, check for an empty-grantee entry or a NULL ACL:
SELECT p.oid::regprocedure AS function, p.proacl
FROM pg_proc p
JOIN pg_namespace n ON n.oid = p.pronamespace
WHERE n.nspname = 'shop'
AND (p.proacl IS NULL
OR EXISTS (SELECT 1 FROM aclexplode(p.proacl) a WHERE a.grantee = 0));In aclexplode, a grantee of 0 means PUBLIC.
Functions: the default people forget
Functions are created with EXECUTE granted to PUBLIC. Consider this function:
CREATE FUNCTION shop.order_count() RETURNS bigint
LANGUAGE sql
AS $$ SELECT count(*) FROM shop.orders $$;Any role with USAGE on the shop schema can call it. Because it is SECURITY INVOKER by default, the caller's table privileges still apply, so a caller without SELECT on shop.orders gets an error. A SECURITY DEFINER function is different. It runs with the owner's privileges, so an open EXECUTE grant exposes whatever the owner can read. For those functions you should always restrict EXECUTE.
REVOKE EXECUTE ON FUNCTION shop.order_count() FROM PUBLIC;
GRANT EXECUTE ON FUNCTION shop.order_count() TO app_readonly;To stop new functions from receiving the open grant, change the default privileges for the creating role:
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner
REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;This statement has no IN SCHEMA clause, and that is deliberate. Per-schema default privileges are added to the global defaults, never subtracted from them. A REVOKE ... IN SCHEMA shop would therefore have no effect on the built-in PUBLIC grant. Only the global form removes it.
Default privileges also apply only to objects created by the named role. If a different role creates a function in shop, the open grant comes back.
The public schema
Before PostgreSQL 15, PUBLIC could create objects in the public schema. That allowed any role to add tables or functions that other users might run by accident, which was a known security problem. From version 15 on, only USAGE is granted to PUBLIC, and the schema is owned by pg_database_owner, which means the database owner.
On older servers, close the gap yourself:
REVOKE CREATE ON SCHEMA public FROM PUBLIC;On version 15 and later this statement is harmless, so you can include it in setup scripts that must work on several versions.
New databases copy template1
CREATE DATABASE copies template1 by default. Objects and grants that exist in template1 appear in every new database. If you want locked-down defaults everywhere, you can apply changes such as the function revoke above while connected to template1. Be aware that this affects all future databases on the server, including those created by other teams. Existing databases are not changed.
Revoking CONNECT from PUBLIC on a database does not carry over to new ones, because the database ACL is not part of the copied contents. You must repeat the REVOKE ALL ON DATABASE for each new database.
A checklist for a new database
- Run
REVOKE ALL ON DATABASE appdb FROM PUBLIC;and grantCONNECTback to your group roles. - Run
REVOKE CREATE ON SCHEMA public FROM PUBLIC;if the server is older than version 15. - Run
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;for each role that creates functions. - Query the ACL columns and confirm that no entry has an empty grantee.
- Test with
has_database_privilegeandhas_function_privilegefor a role that should be denied.
Exercise
You ran REVOKE EXECUTE ON ALL FUNCTIONS IN SCHEMA shop FROM PUBLIC; last week. Today a developer creates a new function in shop as app_owner, and the monitoring role bob can run it even though nobody granted it to him. Why, and what single statement prevents this for future functions?
Show answer
REVOKE ... ON ALL FUNCTIONS only affects functions that exist when it runs. New functions get the built-in default of EXECUTE for PUBLIC. Prevent it with ALTER DEFAULT PRIVILEGES FOR ROLE app_owner REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;, written without IN SCHEMA, because per-schema entries cannot remove the global default.
Exercise
The datacl of a database contains {postgres=CTc/postgres,=c/postgres,app_writer=c/postgres}. Can a role with no direct grants connect to this database, and which entry shows why?
Show answer
Yes. The entry =c/postgres has an empty grantee, which means PUBLIC holds CONNECT. Every role can connect until that entry is removed with REVOKE CONNECT ON DATABASE ... FROM PUBLIC.