Skip to content

Instantly share code, notes, and snippets.

@JeffreyMFarley
Created September 1, 2023 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyMFarley/fad371ecba70eb3906e992c76fe728f0 to your computer and use it in GitHub Desktop.
Save JeffreyMFarley/fad371ecba70eb3906e992c76fe728f0 to your computer and use it in GitHub Desktop.
Postgres Table-Column-ForeignKeys
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