Skip to content

Instantly share code, notes, and snippets.

@gareth-cheeseman
Created April 20, 2023 21:13
Show Gist options
  • Save gareth-cheeseman/f1be4ce20090ff18aad52bca4f0c5c58 to your computer and use it in GitHub Desktop.
Save gareth-cheeseman/f1be4ce20090ff18aad52bca4f0c5c58 to your computer and use it in GitHub Desktop.
Table owners postgres
-- Get owners of schema
SELECT
nsp.nspname AS the_schema,
rol.rolname AS the_owner
FROM
pg_namespace nsp
JOIN pg_roles rol ON rol.oid = nsp.nspowner
WHERE
nsp.nspname = 'public';
-- Get owners of tables
select nsp.nspname as object_schema,
cls.relname as object_name,
rol.rolname as owner,
case cls.relkind
when 'r' then 'TABLE'
when 'm' then 'MATERIALIZED_VIEW'
when 'i' then 'INDEX'
when 'S' then 'SEQUENCE'
when 'v' then 'VIEW'
when 'c' then 'TYPE'
else cls.relkind::text
end as object_type
from pg_class cls
join pg_roles rol on rol.oid = cls.relowner
join pg_namespace nsp on nsp.oid = cls.relnamespace
where nsp.nspname not in ('information_schema', 'pg_catalog')
and nsp.nspname not like 'pg_toast%'
-- and rol.rolname = current_user --- remove this if you want to see all objects
order by nsp.nspname, cls.relname;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment