Skip to content

Instantly share code, notes, and snippets.

@GaNiziolek
Last active June 6, 2022 14:55
Show Gist options
  • Save GaNiziolek/5a82d3b6c3bc81e2b8106c2d1a662f5d to your computer and use it in GitHub Desktop.
Save GaNiziolek/5a82d3b6c3bc81e2b8106c2d1a662f5d to your computer and use it in GitHub Desktop.
Postgresql get all columns info
SELECT
cols.table_catalog as database,
cols.table_schema as schema,
cols.table_name,
_table.description,
cols.column_name,
cols.is_nullable,
cols.data_type,
cols.character_maximum_length,
(
SELECT
pg_catalog.col_description(c.oid, cols.ordinal_position::int)
FROM pg_catalog.pg_class c
WHERE
c.oid = (SELECT cols.table_name::regclass::oid) AND
c.relname = cols.table_name
) as column_comment
FROM information_schema.columns cols
LEFT JOIN (
SELECT pgc.*,t.*,
pg_catalog.obj_description(pgc.oid, 'pg_class') as description
FROM information_schema.tables t
INNER JOIN pg_catalog.pg_class pgc
ON t.table_name = pgc.relname
) _table
ON _table.table_catalog = cols.table_catalog
AND _table.table_schema = cols.table_schema
AND _table.table_name = cols.table_name
AND _table.description IS NOT NULL
WHERE
cols.table_catalog = 'gtrp-teste' AND
cols.table_schema = 'inusitta'
ORDER BY cols.table_catalog,
cols.table_schema,
cols.table_name,
cols.ordinal_position
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment