{"id":4395,"date":"2025-07-14T16:12:36","date_gmt":"2025-07-14T16:12:36","guid":{"rendered":"https:\/\/nenadnoveljic.com\/blog\/?p=4395"},"modified":"2025-07-14T16:14:37","modified_gmt":"2025-07-14T16:14:37","slug":"collecting-table-privileges-in-postgresql","status":"publish","type":"post","link":"https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/","title":{"rendered":"Collecting table privileges in PostgreSQL"},"content":{"rendered":"<h1>information_schema.table_privileges<\/h1>\n<p>In PostgreSQL, viewing <code>information_schema.table_privileges<\/code> only shows privileges granted to or by the current role. This is by design and is documented in the <a href=\"https:\/\/www.postgresql.org\/docs\/current\/infoschema-table-privileges.html\">information_schema.table_privileges<\/a> reference.<\/p>\n<pre><code>\npostgres=# set role admin_user;\nSET\n\npostgres=# create user u;\nCREATE ROLE\n\npostgres=# create table t (n integer);\nCREATE TABLE\n\npostgres=# grant select on t to u;\nGRANT\n\npostgres=# select * from information_schema.table_privileges \n           where table_name = 't' and grantee = 'u';\n grantor     | grantee | table_catalog | table_schema | table_name | privilege_type | is_grantable | with_hierarchy \n-------------+---------+---------------+--------------+------------+----------------+--------------+----------------\n admin_user | u     | postgres      | public       | t          | SELECT         | NO           | YES\n<\/code><\/pre>\n<p>However, when executed under a low-privileged user, the query returns no rows:<\/p>\n<pre><code>\npostgres=# create user monitoring;\nCREATE ROLE\n\npostgres=# set role monitoring;\nSET\n\npostgres=> select * from information_schema.table_privileges \n            where table_name = 't' and grantee = 'u';\n(0 rows)\n<\/code><\/pre>\n<p>This behavior is similar to Oracle\u2019s <a href=\"https:\/\/docs.oracle.com\/en\/database\/oracle\/oracle-database\/19\/refrn\/ALL_TAB_PRIVS.html\">ALL_TAB_PRIVS<\/a>, which only shows privileges related to the current user. For full visibility, we want behavior like Oracle\u2019s <a href=\"https:\/\/docs.oracle.com\/en\/database\/oracle\/oracle-database\/19\/refrn\/DBA_TAB_PRIVS.html\">DBA_TAB_PRIVS<\/a>.<\/p>\n<p>To do this in PostgreSQL, we must solve two issues:<\/p>\n<ol>\n<li>The non-privileged user must be granted <code>SELECT<\/code> access on safe columns from <code>pg_authid<\/code>.<\/li>\n<li>We must bypass the current-user-only filter used by <code>information_schema.table_privileges<\/code>.<\/li>\n<\/ol>\n<h1>pg_authid access<\/h1>\n<p><code>pg_authid<\/code> contains sensitive information (like password hashes), so we limit access to just the necessary columns:<\/p>\n<pre><code>\nGRANT SELECT(oid, rolname) ON pg_authid TO monitoring;\n<\/code><\/pre>\n<h1>Remove current user filter<\/h1>\n<p>The default filter in the view definition limits results to grants involving the current user or PUBLIC. This can be seen in the <a href=\"https:\/\/github.com\/postgres\/postgres\/blob\/990571a08b66c76be85b077ddcba419fd4524952\/src\/backend\/catalog\/information_schema.sql#L1915-L1917\">PostgreSQL source code<\/a>:\n<\/p>\n<pre><code>\nAND (pg_has_role(u_grantor.oid, 'USAGE')\n     OR pg_has_role(grantee.oid, 'USAGE')\n     OR grantee.rolname = 'PUBLIC');\n<\/code><\/pre>\n<h1>Final SELECT<\/h1>\n<p>This query returns all privileges granted on tables in the cluster, regardless of the current role:<\/p>\n<pre><code>\nSELECT CAST(u_grantor.rolname AS text) AS grantor,\n       CAST(grantee.rolname AS text) AS grantee,\n       CAST(current_database() AS text) AS table_catalog,\n       CAST(nc.nspname AS text) AS table_schema,\n       CAST(c.relname AS text) AS table_name,\n       CAST(c.prtype AS text) AS privilege_type,\n       CAST(CASE WHEN pg_has_role(grantee.oid, c.relowner, 'USAGE') OR c.grantable\n                 THEN 'YES' ELSE 'NO' END AS text) AS is_grantable,\n       CAST(CASE WHEN c.prtype = 'SELECT' THEN 'YES' ELSE 'NO' END AS text) AS with_hierarchy\nFROM (\n      SELECT oid, relname, relnamespace, relkind, relowner,\n             (aclexplode(coalesce(relacl, acldefault('r', relowner)))).*\n      FROM pg_class\n     ) AS c (oid, relname, relnamespace, relkind, relowner, grantor, grantee, prtype, grantable),\n     pg_namespace nc,\n     pg_authid u_grantor,\n     (\n       SELECT oid, rolname FROM pg_authid\n       UNION ALL\n       SELECT 0::oid, 'PUBLIC'\n     ) AS grantee (oid, rolname)\nWHERE c.relnamespace = nc.oid\n      AND c.relkind IN ('r', 'v', 'f', 'p')\n      AND c.grantee = grantee.oid\n      AND c.grantor = u_grantor.oid\n      AND c.prtype IN ('INSERT', 'SELECT', 'UPDATE', 'DELETE', 'TRUNCATE', 'REFERENCES', 'TRIGGER');\n<\/code><\/pre>\n<p>When executed by the low-privileged <code>monitoring<\/code> role, this query returns:<\/p>\n<pre><code>\n grantor   | grantee | table_catalog | table_schema | table_name | privilege_type | is_grantable | with_hierarchy \n-----------+---------+---------------+--------------+------------+----------------+--------------+----------------\n admin_user | u       | postgres      | public       | t          | SELECT         | NO           | YES\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Collecting privileges under a low-privileged user <a href=\"https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/\" class=\"more-link\">Continue Reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[60],"tags":[],"class_list":["post-4395","post","type-post","status-publish","format-standard","hentry","category-postgresql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Collecting table privileges in PostgreSQL - All-round Database Topics<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Collecting table privileges in PostgreSQL - All-round Database Topics\" \/>\n<meta property=\"og:description\" content=\"Collecting privileges under a low-privileged user Continue Reading &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/\" \/>\n<meta property=\"og:site_name\" content=\"All-round Database Topics\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-14T16:12:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-14T16:14:37+00:00\" \/>\n<meta name=\"author\" content=\"Nenad Noveljic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@NenadNoveljic\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nenad Noveljic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/collecting-table-privileges-in-postgresql\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/collecting-table-privileges-in-postgresql\\\/\"},\"author\":{\"name\":\"Nenad Noveljic\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"headline\":\"Collecting table privileges in PostgreSQL\",\"datePublished\":\"2025-07-14T16:12:36+00:00\",\"dateModified\":\"2025-07-14T16:14:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/collecting-table-privileges-in-postgresql\\\/\"},\"wordCount\":183,\"commentCount\":0,\"articleSection\":[\"PostgreSQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/collecting-table-privileges-in-postgresql\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/collecting-table-privileges-in-postgresql\\\/\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/collecting-table-privileges-in-postgresql\\\/\",\"name\":\"Collecting table privileges in PostgreSQL - All-round Database Topics\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\"},\"datePublished\":\"2025-07-14T16:12:36+00:00\",\"dateModified\":\"2025-07-14T16:14:37+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/collecting-table-privileges-in-postgresql\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/collecting-table-privileges-in-postgresql\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/collecting-table-privileges-in-postgresql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Collecting table privileges in PostgreSQL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\",\"name\":\"All-round Database Topics\",\"description\":\"Nenad Noveljic\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\",\"name\":\"Nenad Noveljic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"caption\":\"Nenad Noveljic\"},\"sameAs\":[\"nenad-noveljic-9b746a6\",\"https:\\\/\\\/x.com\\\/NenadNoveljic\"],\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/author\\\/nenad\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Collecting table privileges in PostgreSQL - All-round Database Topics","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/","og_locale":"en_US","og_type":"article","og_title":"Collecting table privileges in PostgreSQL - All-round Database Topics","og_description":"Collecting privileges under a low-privileged user Continue Reading &rarr;","og_url":"https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/","og_site_name":"All-round Database Topics","article_published_time":"2025-07-14T16:12:36+00:00","article_modified_time":"2025-07-14T16:14:37+00:00","author":"Nenad Noveljic","twitter_card":"summary_large_image","twitter_creator":"@NenadNoveljic","twitter_misc":{"Written by":"Nenad Noveljic","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/#article","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/"},"author":{"name":"Nenad Noveljic","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"headline":"Collecting table privileges in PostgreSQL","datePublished":"2025-07-14T16:12:36+00:00","dateModified":"2025-07-14T16:14:37+00:00","mainEntityOfPage":{"@id":"https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/"},"wordCount":183,"commentCount":0,"articleSection":["PostgreSQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/","url":"https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/","name":"Collecting table privileges in PostgreSQL - All-round Database Topics","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#website"},"datePublished":"2025-07-14T16:12:36+00:00","dateModified":"2025-07-14T16:14:37+00:00","author":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"breadcrumb":{"@id":"https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nenadnoveljic.com\/blog\/collecting-table-privileges-in-postgresql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nenadnoveljic.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Collecting table privileges in PostgreSQL"}]},{"@type":"WebSite","@id":"https:\/\/nenadnoveljic.com\/blog\/#website","url":"https:\/\/nenadnoveljic.com\/blog\/","name":"All-round Database Topics","description":"Nenad Noveljic","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nenadnoveljic.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa","name":"Nenad Noveljic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","caption":"Nenad Noveljic"},"sameAs":["nenad-noveljic-9b746a6","https:\/\/x.com\/NenadNoveljic"],"url":"https:\/\/nenadnoveljic.com\/blog\/author\/nenad\/"}]}},"_links":{"self":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/4395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/comments?post=4395"}],"version-history":[{"count":1,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/4395\/revisions"}],"predecessor-version":[{"id":4404,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/4395\/revisions\/4404"}],"wp:attachment":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/media?parent=4395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/categories?post=4395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/tags?post=4395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}