DOMAIN data type is based on an existing data type, with optional that restrict its allowed values and an optional value.
Syntax
To declare a new domain data type, use :<name> is the name of the new domain and <type> is the base data type. In a CHECK expression, the VALUE keyword references the value being tested.
You can qualify the <name> of a domain with a (for example, db.domainname). After the domain is created, it can only be referenced from the database that contains the domain.
Columns that use a domain data type enforce the domain’s constraints on , , and cast operations. A column inherits the domain’s DEFAULT value unless the column defines its own.
To view the domains in the current database, use :
pg_catalog system catalog, pg_type reports domains with typtype = 'd', and pg_constraint includes domain constraints.
To modify a domain, use :
ALTER DOMAIN to set or drop the default value or NOT NULL constraint, rename or drop constraints, rename the domain, set the domain’s schema, or change the domain owner’s .
To drop the domain, use :
Required privileges
- To , a user must have the
CREATEon the parent database and the schema in which the domain is being created. - To , a user must be the owner of the domain.
- To , a user must be the owner of the domain.
- To on a domain, a user must have the
GRANTprivilege and the privilege that they want to grant. - To create an object that depends on a domain, a user must have the
USAGEprivilege on the domain.
Examples
Create and use a domain
Create a domain that requires a 5-digit string value:CHECK constraint:
INSERT that violates the domain’s CHECK constraint returns an error:
Default value inheritance
A column that uses a domain inherits the domain’sDEFAULT value. A DEFAULT defined on the column takes precedence.
Create a domain with a default value:
qty column inherits the domain’s default value, while restock_qty uses the column’s default value:
Supported casting and conversion
Values of the base type can be to the domain. The cast validates the value against the domain’s constraints:Known limitations
- The base type of a domain cannot be an array, composite, or domain data type.
- Domain
DEFAULTandCHECKexpressions cannot reference database objects such as tables, sequences, , or user-defined types. ALTER DOMAIN ... VALIDATE CONSTRAINTis not supported.DROP DOMAIN ... CASCADEis not supported.- does not list domains.
- The
domains,domain_constraints,domain_udt_usage, andcolumn_domain_usagetables in are not populated.

