ERROR: permission denied for table in PostgreSQL means the current role is missing the privilege the statement needs on that table: SELECT for reads, INSERT, UPDATE or DELETE for writes. Login and schema access already worked, otherwise you would have hit a different error. In our experience, most often the table was created after the last GRANT, or by a role whose default privileges nobody configured.
What the error tells you
PostgreSQL names the object type in the message: table, view, materialized view, sequence or column. Since PostgreSQL 11, tables say "table"; version 10 and older said "relation" (aclchk.c). The SQLSTATE is 42501, insufficient_privilege.
app=> SELECT * FROM orders LIMIT 1;
ERROR: permission denied for table orders
-- PostgreSQL 10 and older
ERROR: permission denied for relation orders
Two facts explain most cases. First, the owner of a table has all privileges on it, but nobody else gets anything by default: PostgreSQL grants PUBLIC no privileges on tables, columns or sequences (Privileges). Second, the role also needs USAGE on the schema. If that is missing, you get "permission denied for schema" instead, which we cover in permission denied for schema public.
Step 1: check who you are and what is granted
SELECT current_user, session_user;
-- who owns the table and what is granted
\dp public.orders
SELECT has_schema_privilege('app_ro', 'public', 'USAGE') AS schema_usage,
has_table_privilege('app_ro', 'public.orders', 'SELECT') AS can_select,
has_table_privilege('app_ro', 'public.orders', 'INSERT') AS can_insert;
Check current_user first. Applications often connect as one role and then switch with SET ROLE, or a pooler connects as a different user than you think. In \dp, each entry reads grantee=privileges/grantor: r is SELECT, a INSERT, w UPDATE, d DELETE, and an empty grantee means PUBLIC (ACL abbreviations).
Step 2: grant on the tables that exist now
-- read-only role for one schema
GRANT USAGE ON SCHEMA public TO app_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO app_ro;
-- read-write application role
GRANT USAGE ON SCHEMA public TO app_rw;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_rw;
GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO app_rw;
ALL TABLES IN SCHEMA also covers views and foreign tables (GRANT). It only touches objects that exist when you run it. A table created ten minutes later has no grant, and the error comes back after the next deployment. That is the most common reason we see this ticket twice.
Step 3: fix future tables with ALTER DEFAULT PRIVILEGES
ALTER DEFAULT PRIVILEGES sets the privileges applied to objects created in the future; it does not change existing objects (ALTER DEFAULT PRIVILEGES). The trap is FOR ROLE. Defaults belong to the role that creates the object, and if you leave out FOR ROLE, they apply to the current role only. If an admin runs the command as themselves but migrations create tables as migrator, nothing happens.
-- tables that the role "migrator" creates later in schema public
ALTER DEFAULT PRIVILEGES FOR ROLE migrator IN SCHEMA public
GRANT SELECT ON TABLES TO app_ro;
ALTER DEFAULT PRIVILEGES FOR ROLE migrator IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_rw;
ALTER DEFAULT PRIVILEGES FOR ROLE migrator IN SCHEMA public
GRANT USAGE ON SEQUENCES TO app_rw;
-- check what is configured
\ddp
Defaults are not inherited from group roles. At creation time, PostgreSQL applies only the default privileges of the role that is actually creating the object, not those of roles it is a member of (docs). If two deploy roles create tables, configure defaults for both, or make migrations run SET ROLE to a single owner role first.
Other places the error comes from
- Sequences. An
INSERTinto a table with aserialcolumn callsnextval(), and that needsUSAGEorUPDATEon the sequence (sequence functions). The error says "permission denied for sequence", but people often read it as a table problem. - Column privileges. A role can hold privileges on individual columns.
SELECT *then fails, while selecting only the granted columns works. Revoking one column from a table-level grant does not work either: the table-level grant still covers it (GRANT). - Views. By default, the base tables behind a view are checked with the privileges of the view owner, so the error can name a table you never queried. With
security_invoker = true(PostgreSQL 15+), the caller needs privileges on the base tables too (CREATE VIEW, PostgreSQL 15 notes). - Row-level security is not this error. With RLS enabled and no policy, the query returns no rows instead of failing (Row Security Policies). If a query "works" but returns nothing, see our row-level security guide.
app=> INSERT INTO orders (customer_id) VALUES (42);
ERROR: permission denied for sequence orders_id_seq
GRANT USAGE ON SEQUENCE orders_id_seq TO app_rw;
GRANT SELECT (id, status, created_at) ON orders TO support;
support=> SELECT * FROM orders; -- fails: not all columns granted
support=> SELECT id, status FROM orders; -- works
Don't fix it by making everyone the owner
The quick fix people reach for is ALTER TABLE … OWNER TO app or making the application a superuser. It works, and it also lets the application drop tables, change columns and turn off RLS (owners bypass it by default). Keep three roles: an owner that runs migrations, a read-write role for the application and a read-only role for people and BI tools. For broad read access, PostgreSQL 14 added pg_read_all_data, which acts as SELECT on all tables, views and sequences plus USAGE on all schemas, without bypassing RLS (predefined roles, PostgreSQL 14 notes).
-- PostgreSQL 14+: read everything in the database, for BI or backups
GRANT pg_read_all_data TO reporting;
What changes on large databases
- Partitions are separate tables. Queries through a parent are checked on the parent only, the same rule as for inheritance (Inheritance). Querying a partition directly needs a grant on that partition. In our experience, monthly partitions created by a job run into this every month for tools that read partitions directly, unless default privileges cover the job's role.
- Thousands of tables or tenant schemas.
ALL TABLES IN SCHEMAworks per schema, so script it across schemas and run it in a transaction. Then set default privileges per schema or globally, so the work does not need repeating. - Several deploy paths. Migrations, ETL jobs and manual hotfixes often create tables as different roles. In our experience, that is where "works in staging, fails in production" comes from. Audit with
\ddpand owner queries, and make every path create objects as the same owner role. - Restores and new clusters. Grants travel inside a dump only if the roles exist on the target and you do not restore with
--no-privileges. Check privileges on a restored copy before you switch traffic to it. - Monitor 42501 by role. A burst of insufficient_privilege errors right after a deployment usually means a new table without grants. Alert on it before users report "the app is broken".
Key takeaways
- The role lacks a privilege on that table; schema access and login are already fine.
- Check
current_user,\dpandhas_table_privilege()before granting anything. GRANT … ON ALL TABLES IN SCHEMAcovers existing objects only; useALTER DEFAULT PRIVILEGES FOR ROLE <creator>for future ones.- Inserts into serial columns also need
USAGEon the sequence. - Views check the owner's privileges by default; column grants break
SELECT *. - Separate owner, read-write and read-only roles instead of handing out ownership.