Postgres "ERROR: permission denied for schema public": Why It Happens Since PostgreSQL 15 and How to Fix It
"permission denied for schema public" usually means PostgreSQL 15+ no longer lets every role create tables in public. How to check it and which fix to use.
In most cases this error appears because PostgreSQL 15 stopped giving every role permission to create objects in the public schema. Your app or migration user can still read from public, but it isn't allowed to run CREATE TABLE there unless it owns the database or has been granted CREATE on the schema. The cleanest fix for a database that belongs to one app is to make that app's role the database owner. The quick fix is GRANT CREATE ON SCHEMA public TO your_role;.
Checked against the PostgreSQL 15 release notes, the PostgreSQL 18 documentation (schemas, predefined roles, ALTER DATABASE), the Docker Postgres image docs and Prisma's shadow database docs in November 2026.
The error usually shows up the first time migrations run against a new database:
ERROR: permission denied for schema public
LINE 1: CREATE TABLE "django_migrations" ("id" bigint NOT NULL PRIMA...
Django shows it as django.db.utils.ProgrammingError: permission denied for schema public, Prisma includes the same Postgres message when a migration fails, and other migration tools hit it the same way. The SQLSTATE is 42501 (insufficient privilege).
What changed in PostgreSQL 15
The PostgreSQL 15 release notes list two related changes among the incompatibilities:
- "Remove
PUBLICcreation permission on thepublicschema." Before 15, every role could create tables inpublic. From 15 on, only the schema's owner (and roles grantedCREATE) can. - "Change the owner of the
publicschema to be the newpg_database_ownerrole."pg_database_owneris a built-in role whose only member is, per the predefined roles docs, "the current database owner". In practice: whoever owns the database also controlspublic.
The reason is security. A role that can create objects in a schema other people use can plant a function or table that changes what their queries do. The new default matches the "secure schema usage pattern" the docs had recommended for years.
Two details explain why the error seems to appear at random:
- The change "applies to new database clusters and to newly-created databases in existing clusters. Upgrading a cluster or restoring a database dump will preserve
public's existing permissions." An old database upgraded to 16 keeps working. A brand-new one created on the same server fails. USAGEonpublicis still granted to everyone. That's why your role can connect and select from existing tables but can't create new ones.
Step 1: confirm who you are and who owns what
Connect as the role your app or migration tool uses, then run:
SELECT current_user;
-- who owns the current database
SELECT datname, pg_get_userbyid(datdba) AS owner
FROM pg_database
WHERE datname = current_database();
-- can I create in public?
SELECT has_schema_privilege(current_user, 'public', 'CREATE') AS can_create,
has_schema_privilege(current_user, 'public', 'USAGE') AS can_use;
In psql, \dn+ public shows the schema's owner and its access privileges. On a fresh PostgreSQL 15+ database the owner is pg_database_owner, and the only entry for all users (=U/...) is U for usage, with no C for create.
If can_create is false and your role isn't the database owner, you've found the cause.
This often happens when the database was created by a different role than the one the app uses. A typical case is the official Docker image: its docs say POSTGRES_USER creates a user "with superuser power and a database with the same name." If you then create a separate, non-superuser app_user (a good idea), that role owns nothing and gets this error on its first migration.
Step 2: pick a fix
Run these as a superuser or as the current database owner. Replace myapp and app_user with your names.
| Fix | When it fits | Command |
|---|---|---|
| Make the role the database owner | One app per database, the app runs its own migrations | ALTER DATABASE myapp OWNER TO app_user; |
Grant CREATE on public |
The database is owned by someone else, and you trust this role to create objects | GRANT CREATE ON SCHEMA public TO app_user; |
| Give the app its own schema | Several apps or teams share one database | CREATE SCHEMA app AUTHORIZATION app_user; |
Option A: make the app role the database owner
ALTER DATABASE myapp OWNER TO app_user;
Because public is owned by pg_database_owner, the new owner automatically controls public in that database. You don't need any schema grants. The ALTER DATABASE docs say that to change the owner "you must be able to SET ROLE to the new owning role, and you must have the CREATEDB privilege", which a superuser always has.
Ownership of the database doesn't move existing tables. Tables created earlier by another role stay owned by that role, which matters for the next section.
When you create new databases, set the owner up front: CREATE DATABASE myapp OWNER app_user;.
Option B: grant CREATE on the schema
GRANT CREATE ON SCHEMA public TO app_user;
USAGE is already granted to everyone, so CREATE is the missing piece. Grant it to the specific role, not to PUBLIC. Running GRANT CREATE ON SCHEMA public TO PUBLIC; brings back the pre-15 behaviour for every role in the database, which is the risk the change was designed to remove. The release notes only suggest it "for new databases having no need to defend against insider threats".
Option C: a schema per app
CREATE SCHEMA app AUTHORIZATION app_user;
ALTER ROLE app_user SET search_path = app, public;
app_user owns the app schema and can do anything in it. The role-level search_path means unqualified table names resolve to app first, which is enough for most ORMs. Prisma sets its own schema from the connection URL, so add ?schema=app there.
Fixes to avoid
- Making the app role a superuser. It clears the error and removes every safety boundary. One SQL injection or one bad command from an AI agent can then drop the whole cluster.
ALTER SCHEMA public OWNER TO app_user;works, but it breaks the "database owner controlspublic" link thatpg_database_ownerprovides. Prefer Option A.
"permission denied for table" is a different problem
Once the role can create objects, you may see related errors with different causes:
ERROR: permission denied for table orders
ERROR: permission denied for sequence orders_id_seq
These mean the role can use the schema but has no rights on that particular table or sequence. It's common when migrations run as one role (migrator) and the app connects as another (app_user). Grant the table rights, and set default privileges so tables created later get them too:
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_user;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO app_user;
ALTER DEFAULT PRIVILEGES FOR ROLE migrator IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_user;
ALTER DEFAULT PRIVILEGES FOR ROLE migrator IN SCHEMA public
GRANT USAGE, SELECT ON SEQUENCES TO app_user;
GRANT ... ON ALL TABLES only covers tables that exist right now. ALTER DEFAULT PRIVILEGES covers future tables created by migrator. Postgres Roles and Permissions: CREATE USER, GRANT, and Least Privilege walks through a full owner/app/read-only setup. If you only need broad read or write access, the predefined roles pg_read_all_data and pg_write_all_data grant it across all schemas, though neither bypasses row-level security.
A third relative is ERROR: must be owner of table orders. That comes from ALTER TABLE or DROP TABLE in a migration when the table was created by a different role. Grants don't fix it. Either run migrations as the role that owns the tables, or move ownership with ALTER TABLE orders OWNER TO migrator; (or REASSIGN OWNED BY old_role TO migrator; for everything at once, which you should read up on before running).
Framework notes
- Prisma:
prisma migrate deployonly needs to create objects in the target schema, so Option A or B is enough.prisma migrate devalso creates a temporary shadow database, and Prisma's shadow database docs say that on Postgres "the user must be a super user or haveCREATEDBprivilege." Runmigrate devagainst a local database, never production. See Prisma Migrations in Production: migrate dev vs migrate deploy. - Django: the first table it creates is
django_migrations, which is why that name appears in the error. The fix is the same. - After a Postgres upgrade: a
pg_upgradekeeps the old permissions, so the error usually starts after a migration to a new database (a new managed instance, a fresh Docker volume, aCREATE DATABASEon the upgraded server), not after the upgrade itself. Check that the new database has the right owner.
EasySpawn gives each project a managed Postgres on the same server where Claude Code works, so the agent can run migrations against a real database and you see errors like this one before they reach production. It is pre-launch; join the waitlist if that sounds useful.
Related: Postgres Roles and Permissions: CREATE USER, GRANT, and Least Privilege · Prisma Migrations in Production: migrate dev vs migrate deploy · Postgres Major Version Upgrades: pg_upgrade, Logical Replication, and Minimal Downtime · What Are Database Migrations? A Plain-English Guide
Keep reading
Postgres Roles and Permissions: CREATE USER, GRANT, and Least Privilege
Most apps connect to Postgres as a superuser, so one SQL injection or rogue AI command can drop everything. How Postgres roles, privileges, schemas and default privileges work, and a practical setup with separate owner, app and read-only roles.
Role-Based Access Control (RBAC) for Your App: A Practical Guide
How to add roles and permissions to a web app without making a mess: roles vs permissions, a simple database schema, checking permissions on the server, multi-tenant roles per organisation, enforcing in the UI and the API, and testing it.