Postgres: permission denied for schema public (42501)
PostgreSQL 15 stopped granting CREATE on public to everyone. Grant USAGE and CREATE to the right role and set default privileges for new tables.

Introduction & Quick Summary
A deployment that worked on the old database fails on a fresh cluster:
ERROR: permission denied for schema public
LINE 1: CREATE TABLE invoices (
^
SQL state: 42501Quick answer: the role running the migration lacks CREATE on public. PostgreSQL 15 stopped granting that privilege to everyone, so the same migration succeeds on an upgraded database (where old grants remain) and fails on a new one.
-- run as the schema owner or a superuser
GRANT USAGE, CREATE ON SCHEMA public TO app_migrator;USAGE lets a role look up objects in the schema; CREATE lets it add tables, views and indexes. A read-only application role needs only USAGE, while a migration role needs both. The error names the schema, not the table, which is a useful clue: if the table already existed, the message would say permission denied for table invoices.
Three variants appear around this one, and they are often confused:
permission denied for schema public— noCREATE(or noUSAGE) on the schema itself.permission denied for table invoices— schema access is fine, table privileges are missing.no schema has been selected to create in—search_pathis empty, a different failure with a similar cause.
Core Mechanics, Signatures & Reproducible Setup
Reproduce on a local PostgreSQL 15 or later:
createdb perms && psql permsCREATE ROLE app_migrator LOGIN PASSWORD 'secret';
-- as the database owner
\c perms postgres
CREATE SCHEMA IF NOT EXISTS public; -- exists by default
-- connect as app_migrator
\c perms app_migrator
CREATE TABLE invoices (id bigserial PRIMARY KEY, total numeric);
-- ERROR: permission denied for schema publicPrivileges are checked in order: connect to the database (CONNECT), enter the schema (USAGE), then act on the object (CREATE, SELECT, INSERT). A failure at the schema level is reported before the object is ever considered, which is why the fix belongs in a grant on the schema.
| Privilege on schema | Allows | Needed by |
|---|---|---|
USAGE | resolving names, accessing contained objects | every role that reads tables |
CREATE | creating tables, views, sequences, functions | migrations and owners |
USAGE plus table grants | normal application access | app roles |
| ownership | everything, including dropping | the schema owner |
Key Comparison Table
| Situation | PostgreSQL 14 and older | PostgreSQL 15 and newer |
|---|---|---|
New cluster, public schema owner | postgres (or bootstrap user) | pg_database_owner |
CREATE on public for PUBLIC role | granted | revoked |
USAGE on public for PUBLIC role | granted | still granted |
| Untrusted users may create objects there | yes, a known risk | no |
Existing database after pg_upgrade | unchanged | unchanged, grants persist |
Grant recipes by intent:
| Intent | SQL | Notes |
|---|---|---|
| Migration role creates tables | GRANT USAGE, CREATE ON SCHEMA public TO app_migrator | run as owner |
| App reads and writes existing tables | GRANT USAGE ON SCHEMA public TO app, then table grants | sequence grants too |
| Future tables in the schema | ALTER DEFAULT PRIVILEGES FOR ROLE owner IN SCHEMA public GRANT SELECT ON TABLES TO app | applies to new objects only |
| Dedicated schema per app | CREATE SCHEMA shop AUTHORIZATION app_migrator | cleanest isolation |
| Restore old behaviour | GRANT CREATE ON SCHEMA public TO PUBLIC | avoid on shared clusters |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. Grant on the schema, then on the objects
GRANT USAGE, CREATE ON SCHEMA public TO app_migrator;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO app;GRANT ... ON ALL TABLES covers only tables that exist at that moment. New tables created later by another role are not covered, which is why server applications report permission denied for table X a week after a migration. The durable form is default privileges set by the creating role:
ALTER DEFAULT PRIVILEGES FOR ROLE app_migrator IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app;
ALTER DEFAULT PRIVILEGES FOR ROLE app_migrator IN SCHEMA public
GRANT USAGE, SELECT ON SEQUENCES TO app;The FOR ROLE clause matters: defaults are stored for the role that creates the object, not for the role that reads it.
2. Sequences and SERIAL columns
A table insert that uses a sequence needs USAGE on the sequence, and a missing grant produces a different 42501 message:
ERROR: permission denied for sequence invoices_id_seq
GRANT INSERT ON TABLE alone is not enough for bigserial or identity columns, because the default expression calls nextval. Include sequences in the same grant, exactly as in the recipe above.
3. Which role is actually connected
SELECT current_user, session_user, current_database();
SHOW search_path;
SELECT has_schema_privilege(current_user, 'public', 'CREATE') AS can_create,
has_schema_privilege(current_user, 'public', 'USAGE') AS can_use;session_user is the login role and current_user is the effective role after SET ROLE. A connection through a pooler often uses a different role than the one the application believes it has, and SET ROLE app_migrator explains why a manual psql session can create tables while the application cannot. SET ROLE requires membership in the target role (GRANT app_migrator TO deploy_user); without it the statement itself fails with permission denied to set role.
4. Managed Postgres and pooled connections
On Supabase, Neon and RDS, the migration role is usually the owner (postgres on Supabase) and the application connects as a separate role. Running migrations through the pooled connection string can land on a different backend or a different role, which produces intermittent 42501 errors on deploy. Use the direct connection for DDL, and keep the grant step in the same migration that creates the objects, so a rollback cannot leave a table without privileges. Check the role inside the migration:
DO $$
BEGIN
IF current_user NOT IN ('postgres', 'app_migrator') THEN
RAISE EXCEPTION 'migrations must run as app_migrator, got %', current_user;
END IF;
END $$;5. Telling permission errors from row-level security
RLS does not raise 42501; it silently filters rows, so a query returns fewer rows or none. When an insert fails with new row violates row-level security policy, the policy rejected the row and the role has table privileges. The distinction matters because granting more schema privileges never fixes an RLS problem, and adding a policy never fixes a missing grant. Supabase's service_role bypasses RLS by attribute, not by grant, so a service_role key with a missing schema grant still fails.
6. Verifying a grant set before deploying
SELECT nspname, nspacl FROM pg_namespace WHERE nspname = 'public';
SELECT grantee, privilege_type
FROM information_schema.role_usage_grants
WHERE object_schema = 'public';A migration test that connects as the application role and runs a single CREATE TABLE ... TEMP (then rolls back) catches a missing CREATE before production. Format the GRANT statements with the SQL formatter so a copied recipe does not lose a clause, and generate the expected DDL for review with the SQL schema visualizer.
Common Compilation/Runtime Mistakes & Version Gotchas
Granting table privileges and stopping there. Without USAGE on the schema, every table grant is unreachable. Start at the schema.
Running the grant in the wrong database. Grants are per-database; a GRANT issued in postgres does nothing for app_production.
Assuming a pg_upgrade cluster behaves like a new one. Upgraded clusters keep their old ACLs, so staging and production diverge after a rebuild. Compare nspacl on both.
Using GRANT ALL ON SCHEMA public TO PUBLIC as a shortcut. It restores the pre-15 behaviour and lets any role create objects, which is the vulnerability the change addressed.
Forgetting ALTER DEFAULT PRIVILEGES for future tables. This is the most common cause of a permission error that appears days after a successful deploy.
Version notes. PostgreSQL 15 changed the public schema ownership to pg_database_owner and revoked CREATE from PUBLIC; the release notes describe it under "Remove PUBLIC creation permission on the public schema". PostgreSQL 16 added GRANT ... WITH INHERIT options that change how role membership applies. Managed services may add their own grants on top, so always verify with has_schema_privilege rather than assuming the documented defaults.
Official references
primary documentationThe SQL documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related SQL guides
All SQL guidesSQLite: database is locked (SQLITE_BUSY) under concurrent writes
SQLite allows one writer. Set a busy timeout, switch to WAL, start write transactions as IMMEDIATE, and reduce to a single writer connection.
Postgres: column does not exist (SQLSTATE 42703)
42703 means the identifier you wrote is not the one that exists. Check case folding, alias scope, search_path and migration drift.
PostgreSQL: canceling statement due to statement timeout
The statement was canceled by statement_timeout. Find whether the role, the pooler or the client set it, then scope a longer budget to that job.
pg_dump: server version mismatch, aborting because of server version
pg_dump refuses to read a newer server. Install a matching client, run it from a container or the server host, and match pg_restore too.
More in SQL
All SQL guidesThe guides filed next to this one in SQL, in library order.