Last active
May 4, 2017 10:38
Star
You must be signed in to star a gist
Get postgres table schema including keys/constraints
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
SELECT | |
cols.column_name, | |
cols.data_type, | |
cols.character_maximum_length, | |
cols.column_default, | |
cols.is_nullable::boolean, | |
constr.constraint_type, | |
pg_catalog.col_description(cls.oid, cols.ordinal_position::int)::jsonb | |
FROM | |
pg_catalog.pg_class cls, | |
information_schema.columns cols | |
LEFT JOIN | |
information_schema.key_column_usage keys | |
ON keys.column_name = cols.column_name | |
AND keys.table_catalog = cols.table_catalog | |
AND keys.table_schema = cols.table_schema | |
AND keys.table_name = cols.table_name | |
LEFT JOIN | |
information_schema.table_constraints constr | |
ON constr.constraint_name = keys.constraint_name | |
WHERE | |
cols.table_catalog = 'postgres' AND | |
cols.table_schema = 'public' AND | |
cols.table_name = table_name_param AND | |
cols.table_name = cls.relname; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment