how to see constraints in postgresql 1

how to see constraints in postgresql

SELECT con.*
       FROM pg_catalog.pg_constraint con
            INNER JOIN pg_catalog.pg_class rel
                       ON rel.oid = con.conrelid
            INNER JOIN pg_catalog.pg_namespace nsp
                       ON nsp.oid = connamespace
       WHERE nsp.nspname = ''
             AND rel.relname = '';

Here is what the above code is Doing:
1. We’re selecting all the constraints from the pg_constraint table.
2. We’re joining the pg_class table to the pg_constraint table on the conrelid column.
3. We’re joining the pg_namespace table to the pg_constraint table on the connamespace column.
4. We’re filtering the results to only show constraints that are in the schema we’re interested in.
5. We’re filtering the results to only show constraints that are on the table we’re interested in.

Similar Posts