Query below lists check constraints defined in the database ordered by constraint name.
Check this query to see them organized by table.
Query
select pgc.conname as constraint_name,
ccu.table_schema as table_schema,
ccu.table_name,
ccu.column_name,
pgc.consrc as definition
from pg_constraint pgc
join pg_namespace nsp on nsp.oid = pgc.connamespace
join pg_class cls on pgc.conrelid = cls.oid
left join information_schema.constraint_column_usage ccu
on pgc.conname = ccu.constraint_name
and nsp.nspname = ccu.constraint_schema
where contype ='c'
order by pgc.conname;
Columns
- constraint_name - name of the constraint in the database
- table_schema - table's schema name constraint is defined for, null for not table-level check constraints
- table_name - table name constraint is defined for, null for not table-level check constraints
- column_name - name of the column for column-level check constraints, null for not table-level check constraints
- is_deffered - indicate if the constraint deferred by default
- definition - SQL expression that defines this check constraint
Rows
- One row represents one check constraint
- Scope of rows: query returns all check constraints in the database
- Ordered by constraint name
Sample results
List of check constraints in pagila database.