[Introspect Table Column DataTypes in PostgreSQL] View listing all tables joined with columns and data types for PostgreSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.postgresql.org/docs/current/catalog-pg-attribute.html
https://www.postgresql.org/docs/current/catalog-pg-class.html
pgsql views for introspection. I have picked out a lot of the columns that think may be useful for developers to do stuff
also note this thing may simplify some of the below at some point. SO post
Note that this set of views does not filter out index tables and the pg_catolog some modification may be required to limit the results to exactly what you want. parhaps joining with information_schema.tables left join v_introspected_tables.
Usage
Creation
Sample row from v_introspect_tables