Skip to content

Instantly share code, notes, and snippets.

@rizalgowandy
Created May 3, 2020 10:15
Show Gist options
  • Save rizalgowandy/981a78a6984ec97941ae9638ff0737ad to your computer and use it in GitHub Desktop.
Save rizalgowandy/981a78a6984ec97941ae9638ff0737ad to your computer and use it in GitHub Desktop.
PostgreSQL Get Table Schema
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