Incident Response

PostgreSQL "permission denied for table": How to Fix It

The role is connected, the schema is visible, and the query still fails. Here is how to see which privilege is missing, why GRANT on all tables does not cover tomorrow's tables, and what views, sequences, columns and partitions add to the picture.

· 11 min read · AG Data Team

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

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

Key takeaways

PostgreSQL Errors Security Administration
Work with us

Privileges a mess after years of quick fixes?

We design and clean up PostgreSQL role models for production: least-privilege application roles, read-only access for analytics and grants that survive migrations.

Get in touch More from the blog