Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
42501 means the role you are connected as does not have the privilege it needs. The message names the kind of object: permission denied for table, for schema, or for sequence. Each needs a different GRANT, run by the object's owner (or a superuser).
Accessing another schema's table needs two privileges: USAGE on the schema and SELECT/INSERT on the table. A table with a serial column also needs USAGE on its sequence for INSERT.
01No SELECT privilege on the table
lab_b already has USAGE on schema lab_a, but reading lab_a.orders fails with permission denied for table orders. Owning the schema or seeing the table is not enough; each table needs its own privilege.
The owner runs GRANT SELECT ON lab_a.orders TO lab_b; and the query returns both rows.
Setup
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);
INSERT INTO orders VALUES (1, 1200), (2, 500);
GRANT USAGE ON SCHEMA lab_a TO lab_b;CREATE TABLE
INSERT 0 2
GRANTSELECT id, amount FROM lab_a.orders;ERROR: 42501: permission denied for table ordersGRANT SELECT ON lab_a.orders TO lab_b;GRANTSELECT id, amount FROM lab_a.orders ORDER BY id; id | amount
----+--------
1 | 1200
2 | 500
(2 rows)02No USAGE privilege on the schema
Here lab_b already has SELECT on lab_a.products, yet the query fails with permission denied for schema lab_a. Without USAGE on the schema, PostgreSQL does not let the role look up objects inside it, whatever table privileges it has.
After GRANT USAGE ON SCHEMA lab_a TO lab_b; the same query works.
Setup
CREATE TABLE products (id int PRIMARY KEY, name text);
INSERT INTO products VALUES (1, 'Pen');
GRANT SELECT ON lab_a.products TO lab_b;CREATE TABLE
INSERT 0 1
GRANTSELECT id, name FROM lab_a.products;ERROR: 42501: permission denied for schema lab_a
LINE 1: SELECT id, name FROM lab_a.products;
^GRANT USAGE ON SCHEMA lab_a TO lab_b;GRANTSELECT id, name FROM lab_a.products; id | name
----+------
1 | Pen
(1 row)03INSERT into a serial table without sequence privilege
lab_b has SELECT, INSERT on lab_a.logs, but the INSERT fails with permission denied for sequence logs_id_seq. The serial column fills id from the sequence, and calling it needs its own privilege.
After GRANT USAGE ON SEQUENCE lab_a.logs_id_seq TO lab_b; the INSERT returns id 1. The failed attempts did not use up sequence numbers because they were rejected before the sequence was called.
Setup
CREATE TABLE logs (id serial PRIMARY KEY, msg text);
GRANT USAGE ON SCHEMA lab_a TO lab_b;
GRANT SELECT, INSERT ON lab_a.logs TO lab_b;CREATE TABLE
GRANT
GRANTINSERT INTO lab_a.logs (msg) VALUES ('login');ERROR: 42501: permission denied for sequence logs_id_seqGRANT USAGE ON SEQUENCE lab_a.logs_id_seq TO lab_b;GRANTINSERT INTO lab_a.logs (msg) VALUES ('login') RETURNING id, msg; id | msg
----+-------
1 | login
(1 row)
INSERT 0 104Creating a table in public on PostgreSQL 15 or later
Since PostgreSQL 15, ordinary users can no longer create objects in the public schema. CREATE TABLE public.notes fails with permission denied for schema public.
The check shows why: public is owned by pg_database_owner, and its ACL =U/pg_database_owner gives everyone only U (USAGE), not C (CREATE). A superuser or the database owner can run GRANT CREATE ON SCHEMA public TO lab_b;, after which the table is created. Creating tables in your own schema instead also avoids the problem.
CREATE TABLE public.notes (id int PRIMARY KEY, body text);ERROR: 42501: permission denied for schema public
LINE 1: CREATE TABLE public.notes (id int PRIMARY KEY, body text);
^SELECT nspname, pg_get_userbyid(nspowner) AS owner, nspacl FROM pg_namespace WHERE nspname = 'public'; nspname | owner | nspacl
---------+-------------------+---------------------------------------------------------------
public | pg_database_owner | {pg_database_owner=UC/pg_database_owner,=U/pg_database_owner}
(1 row)GRANT CREATE ON SCHEMA public TO lab_b;GRANTCREATE TABLE public.notes (id int PRIMARY KEY, body text);CREATE TABLE05No CONNECT privilege on the database (fails at login)
Here the error comes at connect time, not from a query: FATAL: permission denied for database "shop" with DETAIL: User does not have CONNECT privilege. The shop database had REVOKE CONNECT ... FROM PUBLIC, a common hardening step, and app_user was never granted CONNECT.
Setup
CREATE DATABASE shop;
REVOKE CONNECT ON DATABASE shop FROM PUBLIC;
CREATE ROLE app_user LOGIN PASSWORD 'Shop2026';CREATE DATABASE
REVOKE
CREATE ROLE$ PGPASSWORD='Shop2026' psql -h 127.0.0.1 -U app_user -d shopSELECT current_user, current_database();psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: permission denied for database "shop"
DETAIL: User does not have CONNECT privilege.FATAL: 42501: permission denied for database "shop"
DETAIL: User does not have CONNECT privilege.SELECT datname, datacl FROM pg_database WHERE datname = 'shop'; datname | datacl
---------+-------------------------------------
shop | {=T/postgres,postgres=CTc/postgres}
(1 row)GRANT CONNECT ON DATABASE shop TO app_user;GRANTdatacl shows only =T/postgres for PUBLIC (no c). Run GRANT CONNECT ON DATABASE shop TO app_user; as the owner or a superuser; the same login then connects to shop.
$ PGPASSWORD='Shop2026' psql -h 127.0.0.1 -U app_user -d shopSELECT current_user, current_database(); current_user | current_database
--------------+------------------
app_user | shop
(1 row)FAQ
How do I fix permission denied for table in PostgreSQL?
Have the table owner or a superuser grant the privilege, for example GRANT SELECT ON schema.table TO role;. If the table is in another schema, the role also needs GRANT USAGE ON SCHEMA schema TO role;.
Why do I get permission denied for schema public?
PostgreSQL 15 removed the default CREATE privilege on public for all users. Grant it with GRANT CREATE ON SCHEMA public TO role; or create tables in a schema the role owns.
Why does INSERT give permission denied for sequence?
A serial or default nextval() column calls a sequence. Grant USAGE on it: GRANT USAGE ON SEQUENCE table_id_seq TO role;.
I granted SELECT on the table but still get permission denied. Why?
Check the object named in the message. If it says for schema, the role is missing USAGE on the schema, which is required in addition to the table privilege.
How do I fix permission denied for database in PostgreSQL?
The role lacks CONNECT on that database (DETAIL: User does not have CONNECT privilege). Run GRANT CONNECT ON DATABASE name TO role; as the owner or a superuser.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12