Table Permissions

Created at: 2025-05-09

Basics

Ownership

Granting Specific Privileges

Table Privileges

The full list of privileges can be seen here: https://www.postgresql.org/docs/current/ddl-priv.html

But the interesting bits are:

Can the user be blocked from querying a table even when they have CREATE

permission in the schema?

CREATE USER user1;
CREATE USER user2;

DROP TABLE IF EXISTS public.foo;
CREATE TABLE public.foo (id SERIAL PRIMARY KEY);
INSERT INTO public.foo SELECT generate_series(1, 1000);

SET ROLE user1;
SELECT * FROM public.foo;
-- ERROR:  permission denied for table foo

SET ROLE user2;
SELECT * FROM public.foo;
-- ERROR:  permission denied for table foo

SET ROLE "marcelo.fernandes";

-- User 1 has permission to the table
GRANT SELECT ON public.foo TO user1;
-- User 2 has permission to the schema
GRANT CREATE ON SCHEMA public TO user2;

SET ROLE user1;
SELECT count(*) FROM public.foo; -- 1,000
SET ROLE "marcelo.fernandes";

-- Still permission denied!
SET ROLE user2;
SELECT count(*) FROM public.foo;
-- ERROR:  permission denied for table foo
SET ROLE "marcelo.fernandes";

Tips

For testing permissions, you can always:

SET ROLE sweet_user;
-- test stuff here
SET ROLE "marcelo.fernandes";