Skip to content
Advertisement

List all foreign keys PostgreSQL

I need a query that returns:

“table_name”, “field_name”, “field_type”, “contraint_name”

until now i have:

Advertisement

Answer

A foreign key may be based on multiple columns, so conkey and confkey of pg_constraint are arrays. You have to unnest the arrays to get a list of column names or types. You can use these functions:

The functions may be very handy when querying constraints and indexes. Your query is nice and simple with them:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement