Learning center

Check PostgreSQL schema USAGE when a table grant already exists

A table grant and permission to look up that table's schema are separate checks. When an error names the schema, check schema USAGE for the effective role before changing the table ACL.

In a fictional reporting database, report_job has SELECT on reporting.daily_sales. A newly prepared query is rejected at schema lookup. An authorized operator can inspect the grants by role. Use the table OID from the catalogs to avoid relying on the job role to resolve the relation by name.

SELECT has_schema_privilege('report_job', n.oid, 'USAGE') AS schema_usage,
       has_table_privilege('report_job', c.oid, 'SELECT') AS table_select
FROM pg_catalog.pg_class AS c
JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
WHERE n.nspname = 'reporting' AND c.relname = 'daily_sales';

False and true support a schema-usage diagnosis for that role. Ask the schema owner to consider USAGE if the role should resolve objects there. Object privileges still apply. If the query uses an unqualified table name, check search_path and the intended relation as well. PostgreSQL schema privileges ยท Privilege inquiry functions

Trace another denied request.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading