Skip to content

Instantly share code, notes, and snippets.

@jarnaldich
Last active October 15, 2024 18:11
Show Gist options
  • Save jarnaldich/d5952a134d89dfac48d034ed141e86c5 to your computer and use it in GitHub Desktop.
Save jarnaldich/d5952a134d89dfac48d034ed141e86c5 to your computer and use it in GitHub Desktop.
[Introspect Table Column DataTypes in PostgreSQL] View listing all tables joined with columns and data types for PostgreSQL
CREATE OR REPLACE VIEW table_columns AS
WITH table_oids AS (
SELECT c.relname, c.oid
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE
pg_catalog.pg_table_is_visible(c.oid)),
column_types AS (
SELECT
toids.relname AS "tablename",
a.attname as "column",
pg_catalog.format_type(a.atttypid, a.atttypmod) as "datatype"
FROM
pg_catalog.pg_attribute a, table_oids toids
WHERE
a.attnum > 0
AND NOT a.attisdropped
AND a.attrelid = toids.oid)
SELECT * FROM column_types;
-- Example
SELECT * FROM table_columns WHERE tablename ~ 'dic_'
@jarnaldich
Copy link
Author

That's awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment