Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readSQL
#postgresql#permissions#migrations#supabase
Bold white headline reading SCHEMA DENIED with a Postgres schema crate behind an amber privilege gate and a cyan role badge.

Introduction & Quick Summary

A deployment that worked on the old database fails on a fresh cluster:

code
ERROR: permission denied for schema public LINE 1: CREATE TABLE invoices ( ^ SQL state: 42501

Quick 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.

sql
-- 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 — no CREATE (or no USAGE) 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_path is empty, a different failure with a similar cause.

Core Mechanics, Signatures & Reproducible Setup

Reproduce on a local PostgreSQL 15 or later:

bash
createdb perms && psql perms
CREATE 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 public

Privileges 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 schemaAllowsNeeded by
USAGEresolving names, accessing contained objectsevery role that reads tables
CREATEcreating tables, views, sequences, functionsmigrations and owners
USAGE plus table grantsnormal application accessapp roles
ownershipeverything, including droppingthe schema owner

Key Comparison Table

SituationPostgreSQL 14 and olderPostgreSQL 15 and newer
New cluster, public schema ownerpostgres (or bootstrap user)pg_database_owner
CREATE on public for PUBLIC rolegrantedrevoked
USAGE on public for PUBLIC rolegrantedstill granted
Untrusted users may create objects thereyes, a known riskno
Existing database after pg_upgradeunchangedunchanged, grants persist

Grant recipes by intent:

IntentSQLNotes
Migration role creates tablesGRANT USAGE, CREATE ON SCHEMA public TO app_migratorrun as owner
App reads and writes existing tablesGRANT USAGE ON SCHEMA public TO app, then table grantssequence grants too
Future tables in the schemaALTER DEFAULT PRIVILEGES FOR ROLE owner IN SCHEMA public GRANT SELECT ON TABLES TO appapplies to new objects only
Dedicated schema per appCREATE SCHEMA shop AUTHORIZATION app_migratorcleanest isolation
Restore old behaviourGRANT CREATE ON SCHEMA public TO PUBLICavoid 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:

sql
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.

Reference material: Schemas and GRANT.

Official references

primary documentation

The SQL documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.

All SQL guides

More in SQL

All SQL guides

The guides filed next to this one in SQL, in library order.