A privilege on the table is not enough: PostgreSQL checks the schema first. Since PostgreSQL 15, public belongs to the role pg_database_owner, and PUBLIC only has USAGE there, no CREATE. Applications that create their own tables fail on it at first start.
Symptom
psql service=notes -c 'CREATE TABLE IF NOT EXISTS notes (id uuid PRIMARY KEY,
text text NOT NULL, created_at timestamptz NOT NULL DEFAULT now())'
ERROR: permission denied for schema public
LINE 1: CREATE TABLE IF NOT EXISTS notes (id uuid PRIMARY KEY,
^
The application's migration fails as notes_app – even though the table has existed for a long time. PostgreSQL still checks the CREATE privilege on the schema first.
Diagnosis
Look at the schema's privileges and query them for the role – schema and table separately.
psql -d tag19 -c '\dn+ public'
psql -d tag19 -c "SELECT has_schema_privilege('tag19_app', 'public', 'USAGE'),
has_table_privilege('tag19_app', 'auftrag', 'SELECT')"
Name | Owner | Access privileges | ...
--------+-------------------+----------------------------------------+ ...
public | pg_database_owner | pg_database_owner=UC/pg_database_owner | ...
has_schema_privilege | has_table_privilege
----------------------+---------------------
f | t
Causes
Since PostgreSQL 15PUBLIC no longer has CREATE in public
Up to 14, anyone could create tables in public. Since 15 PUBLIC only has "=U" there – USAGE. A fresh role can read but not create anything.
public | pg_database_owner | pg_database_owner=UC/pg_database_owner+| ...
| | =U/pg_database_owner | ...
ERROR: permission denied for schema public
LINE 1: CREATE TABLE public.muell (x int)IF NOT EXISTSThe table exists, CREATE is checked anyway
CREATE TABLE IF NOT EXISTS requires the CREATE privilege on the schema even if the table is already there. Migrations that run on every start hit this every time.
USAGE revokedHardening took everything from PUBLIC
After "REVOKE ALL ON SCHEMA public FROM PUBLIC" the =U entry is gone. The table privilege is there, the level above it is not. Without a schema prefix the name lookup skips the schema – hence "does not exist" first.
psql "$A" -c 'SELECT count(*) FROM auftrag' psql "$A" -c 'SELECT count(*) FROM public.auftrag' ERROR: relation "auftrag" does not exist ERROR: permission denied for schema public
Fix
psql -d notes -c 'GRANT CREATE ON SCHEMA public TO notes_app' psql service=notes -c 'CREATE TABLE IF NOT EXISTS notes (id uuid PRIMARY KEY, text text NOT NULL, created_at timestamptz NOT NULL DEFAULT now())' NOTICE: relation "notes" already exists, skipping CREATE TABLE
Don't: Giving the privileges back to PUBLIC. Grant them only to the application – the hardening stays in place for everyone else.
PostgreSQL hands-on, day 2: databases, schemas, roles and privileges – group roles, default privileges, the PUBLIC trap and RLS.
Go to day 2 →
FAQ
Why does it say "relation does not exist" first, although the table is there?
A schema without USAGE is skipped in the name lookup. With a schema prefix (public.auftrag) you get the real message.
Is there something cleaner than GRANT CREATE?
A separate migration role as owner of the schema and tables, with the application running under a plain read/write role.