Skip to content

Instantly share code, notes, and snippets.

@kad1r
Created November 16, 2023 14:58
Show Gist options
  • Save kad1r/80ccae8b9b567290143b7a7a304ffe8f to your computer and use it in GitHub Desktop.
Save kad1r/80ccae8b9b567290143b7a7a304ffe8f to your computer and use it in GitHub Desktop.
Get FK and Indexes with PostgreSQL
select * from pg_indexes where tablename='your_table_name';
select tc.table_schema, tc.constraint_name, tc.table_name, kcu.column_name,ccu.table_schema as foreign_table_schema,ccu.table_name as foreign_table_name,ccu.column_name as foreign_column_name
from information_schema.table_constraints as tc
join information_schema.key_column_usage as kcu on tc.constraint_name = kcu.constraint_name and tc.table_schema = kcu.table_schema
join information_schema.constraint_column_usage as ccu on ccu.constraint_name = tc.constraint_name
where tc.constraint_type = 'foreign key' and tc.table_schema='your_db_schema' and tc.table_name='your_table_name';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment