PostgreSQL Error: Permission Denied for Schema Public
The PostgreSQL error “permission denied for schema public” (SQLSTATE 42501) means the connected role lacks a privilege on the public schema itself, most often CREATE for a migration or USAGE for accessing an object inside it. Fix it by identifying the role your application actually uses, then granting only the schema and object privileges that role needs. A database-wide grant is not a substitute for privileges on an existing schema.
Use this table to match the failing operation to the usual missing privilege.
| Symptom | Likely Cause | Appropriate Fix |
|---|---|---|
CREATE TABLE ... fails and names schema public |
The role lacks CREATE on public |
Grant CREATE on the schema to the migration or owner role |
SELECT, INSERT, or API request fails and names schema public |
The role lacks USAGE on public |
Grant USAGE on the schema, plus needed table privileges |
| A query says a relation does not exist | The schema is not in the effective search path, or the role cannot use it | Inspect the resolved schema path and qualify the table name |
| EF Core migration fails on its first table | The migration connection uses a role without schema creation rights | Use a migration role with schema CREATE, or migrate in a separate application schema |
GRANT ALL PRIVILEGES ON DATABASE changed nothing |
Database privileges do not control objects inside public |
Grant privileges directly on the schema and tables |
What Permission Denied for Schema Public Means
A PostgreSQL schema is a namespace, similar to a folder for database objects. The public schema is the default schema present in a database, but it is not automatically writable by every login role on modern PostgreSQL installations.
Two schema privileges matter most:
USAGElets a role resolve and access objects in a schema, provided it also has privileges on those objects.CREATElets a role create objects, such as tables, views, sequences, and functions, in that schema.
A role creating a table in public needs the CREATE privilege on that schema. A role reading from public.orders needs USAGE on public and a suitable privilege such as SELECT on orders.
Schema privileges and table privileges are separate. Giving a role USAGE on public does not let it read every table there, and granting SELECT on a table does not help if the role cannot use the schema containing that table.
Also distinguish the lowercase schema name public from PostgreSQL’s uppercase PUBLIC pseudo-role. Grants made to PUBLIC apply to every role in the database, including roles created later.
How to Fix Permission Denied for Schema Public
Fix the error by verifying the effective role and schema path, then granting only the schema and object privileges that role needs.
Step 1: Check the Role and Effective Schema Path
Run these checks through the same connection string, application user, and database that raise the error. Checking as an administrator can hide the problem because administrators may have privileges the application role does not.
SELECT current_user, session_user, current_database();
current_user is especially important because an application, connection pool, or API layer can switch to another role with SET ROLE.
Check whether the effective role can use or create objects in public.
SELECT
has_schema_privilege(current_user, 'public', 'USAGE') AS can_use_public,
has_schema_privilege(current_user, 'public', 'CREATE') AS can_create_in_public;
Inspect the configured search path:
SHOW search_path;
Then inspect the resolved path PostgreSQL is actually using:
SELECT current_schemas(true);
Run current_schemas(true) in the same session and role that raises the error. A schema listed in search_path can be silently ignored if it does not exist or the role lacks USAGE on it. PostgreSQL’s official guidance on resolving the schema path explains why the resolved path can differ from the raw setting.
Step 2: Choose the Least-Privilege Grant
Choose privileges based on what the role is intended to do.
| Role Purpose | Schema Privileges | Object Privileges |
|---|---|---|
Migration role that creates tables in public |
USAGE, CREATE |
Normally owns the objects it creates |
| Runtime role that reads data | USAGE |
SELECT on required tables or views |
| Runtime role that writes data | USAGE |
Required combination of SELECT, INSERT, UPDATE, and DELETE |
| Runtime role inserting rows with sequences or identity columns | USAGE |
Table write privileges plus sequence USAGE when needed |
| Read-only reporting role | USAGE |
SELECT on only the required tables or views |
The role that runs migrations and the role that serves application requests usually should not have the same privileges.
A migration role may need to create or alter objects. A web application runtime role usually should not be able to create arbitrary tables.
Step 3: Grant Schema Access to the Correct Role
Run grants as the schema owner, a database administrator, or the provider-supported administrative role.
For a migration role that needs to create tables in public, grant CREATE. Granting USAGE too is appropriate for normal object access in that schema.
GRANT USAGE, CREATE ON SCHEMA public TO app_migrator;
For an application runtime role that only needs to use existing objects in public, grant schema usage without table creation rights.
GRANT USAGE ON SCHEMA public TO app_runtime;
Then grant the specific operations needed on an individual table. This example permits reads, inserts, and updates on public.orders.
GRANT SELECT, INSERT, UPDATE ON TABLE public.orders TO app_runtime;
If the runtime role genuinely needs those operations on all existing tables in the schema, use a schema-wide object grant instead.
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_runtime;
Applications that insert rows into tables backed by sequences may also need sequence privileges.
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO app_runtime;
Do not use GRANT ALL merely to make an error disappear. It can give an application permission to modify or delete data it never needed to touch.
Step 4: Set Privileges for Future Tables
Grants on “all tables” affect objects that already exist. They do not automatically apply to tables created by future migrations.
If app_migrator creates future tables and app_runtime needs full application data access, set default privileges for the migration role.
ALTER DEFAULT PRIVILEGES FOR ROLE app_migrator IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_runtime;
Set corresponding defaults for future sequences when the application needs them.
ALTER DEFAULT PRIVILEGES FOR ROLE app_migrator IN SCHEMA public
GRANT USAGE, SELECT ON SEQUENCES TO app_runtime;
Default privileges apply only to objects subsequently created by the specified owner. They do not change existing tables, and they do not affect tables created by a different role.
Step 5: Test With the Intended Connection
Test using the migration role if the failure occurred during a migration. In a disposable development or staging database, create and remove a probe table to confirm that the role can create objects in public.
CREATE TABLE public.permission_probe (id integer);
DROP TABLE public.permission_probe;
For a runtime role, reconnect with the application’s actual credentials and rerun the failed endpoint, query, or application startup step. Confirm that it can access required tables but still cannot create tables if it is not supposed to run migrations.
Why Database-Wide Grants Do Not Fix Schema Public
A common first attempt is to grant all database-level privileges to the runtime role:
GRANT ALL PRIVILEGES ON DATABASE app_db TO app_runtime;
It does not grant permission to create a table in public. Database privileges cover database-level actions such as connecting to the database, creating new schemas, and creating temporary tables. They do not grant access to objects inside an existing schema.
For example, database-level CREATE can allow a role to create a new schema, but it does not let that role create tables inside the existing public schema. The required privilege for that operation is CREATE ON SCHEMA public.
Fully qualifying an object name can help identify a search-path problem:
SELECT * FROM public.orders;
Qualifying the name directs PostgreSQL to public, but it does not bypass schema or table permissions. The role still needs USAGE on public and SELECT on orders.
Public Schema Changes in PostgreSQL 15 and Later
Fresh databases created with PostgreSQL 15 and later normally do not give every role permission to create objects in public. This is why an application that worked with a broadly permissive older installation can fail after moving to a fresh PostgreSQL 15, 16, or newer database.
Databases upgraded from earlier releases, restored from older backups, or created from customized templates can retain older access-control lists. Do not assume two databases in the same cluster have identical public schema permissions.
Restricting general creation in public is usually desirable. If your database still allows every role to create objects there, an administrator can remove that broad ability:
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
Apply that change only after confirming that no application, extension installer, or migration process relies on unrestricted creation in public.
Use a Private Application Schema Instead
For a new application, a dedicated schema is often safer and easier to manage than continually widening access to public. For example, let app_migrator own an app schema and grant the runtime role only the access it needs.
Create the schema as an authorized database administrator or role owner:
CREATE SCHEMA app AUTHORIZATION app_migrator;
Allow the runtime role to resolve objects in it:
GRANT USAGE ON SCHEMA app TO app_runtime;
Grant access to existing application tables:
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA app TO app_runtime;
Grant sequence access where inserts require it:
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA app TO app_runtime;
Set defaults for tables that app_migrator creates later:
ALTER DEFAULT PRIVILEGES FOR ROLE app_migrator IN SCHEMA app
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_runtime;
Set defaults for future sequences:
ALTER DEFAULT PRIVILEGES FOR ROLE app_migrator IN SCHEMA app
GRANT USAGE, SELECT ON SEQUENCES TO app_runtime;
Finally, configure both roles to look in the application schema first.
ALTER ROLE app_migrator IN DATABASE app_db SET search_path = app, pg_catalog;
ALTER ROLE app_runtime IN DATABASE app_db SET search_path = app, pg_catalog;
Existing tables remain in public until you deliberately move them, so do not change the search path without planning for existing object names and dependencies.
EF Core, Managed Databases, and Row-Level Security
EF Core migrations create tables and migration history records. If EF Core reports permission denied for schema public, inspect the role in the migration connection string, not just the role used by the running application. Give the migration role CREATE on its target schema, or configure EF Core to use a dedicated schema owned by that role.
On a managed PostgreSQL service, do not plan to solve this by becoming a superuser. Many providers do not allow true superuser access. Use the database owner, provider-provided admin role, or supported role-management workflow to run the necessary schema grants.
Row-level security is not a replacement for schema privileges. A REST API request can be denied access to public even when row-level security is enabled and table permissions appear broad. The API role must first have USAGE on the schema, then the applicable table privileges; row-level policies govern which permitted rows it can access.
Confirm and Prevent the Error From Returning
Use psql to inspect the schema owner and access-control list when troubleshooting interactively.
\dn+ public
You can also inspect effective schema privileges in SQL, which is more useful when role memberships or role switching are involved.
SELECT
has_schema_privilege('app_runtime', 'public', 'USAGE') AS runtime_can_use_public,
has_schema_privilege('app_migrator', 'public', 'CREATE') AS migrator_can_create_public;
Before shipping a migration or deployment change, confirm all of the following:
- The application and migration tools connect to the expected database and roles.
- Migration roles have
CREATEonly on schemas where they are meant to manage objects. - Runtime roles have
USAGEon the schema and only the table privileges they require. - Sequence privileges exist when inserts use sequences or generated values.
- Default privileges are configured for the role that creates new objects.
- The application search path points to the intended schema, or application SQL uses qualified names.
- The production deployment has been tested with non-administrator credentials.
Frequently Asked Questions
How do I view schema permissions in PostgreSQL?
Use \dn+ public in psql to view the schema owner and listed access-control entries. For effective permissions, test the actual role with has_schema_privilege, because inherited role membership and SET ROLE can make the active permissions differ from what an ACL listing first suggests.
SELECT has_schema_privilege(current_user, 'public', 'USAGE');
Why does EF Core 9 get permission denied for schema public?
EF Core migrations need to create or modify database objects, and the migration connection role lacks CREATE on public. Grant USAGE, CREATE on public to a dedicated migration role, or configure migrations to use an application-owned schema such as app; keep the normal runtime role restricted to USAGE and required table operations.
Why do I get a schema error when I already granted SELECT on the table?
SELECT on a table is not enough when the role lacks USAGE on the table’s schema. A read from public.orders requires both USAGE ON SCHEMA public and SELECT ON TABLE public.orders.
What does SQLSTATE 42501 mean in PostgreSQL?
SQLSTATE 42501 is PostgreSQL’s insufficient-privilege error class. With the message permission denied for schema public, it indicates a missing privilege on the schema, commonly CREATE for DDL or USAGE for resolving objects inside the schema.
Can I fix this on a managed PostgreSQL database without superuser access?
Yes, if your provider gives you a database owner or administrative role allowed to manage schema permissions. Use that supported role to grant USAGE or CREATE on the relevant schema; if it cannot run the grant, use the provider’s access-control tools or ask the database administrator to apply the least-privilege change.
