Created
September 1, 2023 18:27
-
-
Save JeffreyMFarley/fad371ecba70eb3906e992c76fe728f0 to your computer and use it in GitHub Desktop.
Postgres Table-Column-ForeignKeys
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 | |
a.table_name, | |
b.column_name, | |
c.constraint_name | |
from information_schema.tables a | |
inner join information_schema.columns b | |
on a.table_name = b.table_name | |
inner join information_schema.key_column_usage c | |
on b.column_name = c.column_name and a.table_name = c.table_name | |
inner join information_schema.table_constraints d | |
on c.constraint_name = d.constraint_name | |
where a.table_schema = 'public' | |
and a.table_type = 'BASE TABLE' | |
and d.constraint_type = 'FOREIGN KEY' | |
order by a.table_name, b.column_name; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment