Postgres: list of constraints along with their owners
Here is a PostgreSQL SQL query to retrieve a list of constraints along with their owners:
SELECT
conname AS constraint_name,
conrelid::regclass AS table_name,
pg_catalog.pg_get_userbyid(pg_class.relowner) AS owner
FROM
pg_constraint
JOIN
pg_class ON conrelid = pg_class.oid
WHERE
contype IN ('p', 'u', 'f', 'c') -- p: primary key, u: unique, f: foreign key, c: check
ORDER BY
table_name, constraint_name;
Explanation:
pg_constraint
: Contains information about constraints.pg_class
: Provides details about tables and their owners.pg_get_userbyid(pg_class.relowner)
: Fetches the owner of the table associated with the constraint.contype
: Filters constraints by type:p
: Primary Keyu
: Uniquef
: Foreign Keyc
: Check Constraint
This query will give you a clear list of constraints, the tables they belong to, and their respective owners.
Comments
Post a Comment