PostgreSQL Tips: Get tablenames and column in database

8 04 2009

I have requirement need script. I did it and i share it, hope it’ll useful for someone.

SELECT
t.tablename,
a.attname AS "Column"
--,pg_catalog.format_type (a.atttypid, a.atttypmod) AS "Datatype"
FROM
pg_tables t,
pg_catalog.pg_attribute a
WHERE
a.attnum > 0
AND NOT a.attisdropped
AND a.attrelid = (
SELECT
c.oid
FROM
pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n
ON   n.oid = c.relnamespace
WHERE
c.relname = t.tablename
AND pg_catalog.pg_table_is_visible (c.oid)
)
AND t.tablename NOT LIKE 'pg%'
AND t.tablename NOT LIKE 'sql%'