Question

Consider the following scenario:

CREATE DOMAIN dom_zipcode AS text;
ALTER DOMAIN dom_zipcode
  ADD CONSTRAINT zipchk CHECK (char_length(VALUE) = 5);

Now, if I want to drop that constraint with ALTER DOMAIN, The manual says:

ALTER DOMAIN name
    DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ]

But how can we find constraint_name? \dD only shows the constraint's definition (CHECK statement).

\dD dom_zipcode ;
                             List of domains
 Schema |    Name     | Type | Modifier |             Check
--------+-------------+------+----------+--------------------------------
 public | dom_zipcode | text |          | CHECK (char_length(VALUE) = 5)
(1 row)

I can dump the schema using pg_dump, but I believe there must exist a more elegant way to establish this using the psql terminal.

Was it helpful?

Solution

PostgreSQL's System Catalog

SELECT conname
FROM pg_constraint
WHERE contypid = 'dom_zipcode'::regtype;

OTHER TIPS

SQL Spec's INFORMATION_SCHEMA.DOMAIN_CONSTRAINTS

You can also query this in domain_constraints table of the information_schema

SELECT FORMAT('%I.%I.%I', constraint_schema, constraint_catalog, constraint_name)
FROM information_schema.domain_constraints
WHERE (domain_catalog,domain_schema,domain_name) = ('test','public','dom_zipcode');
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top