DOMAIN data type is based on an existing data type, with optional constraints that restrict its allowed values and an optional DEFAULT value.
Syntax
To declare a new domain data type, useCREATE DOMAIN:
<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 database and schema name (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 INSERT, UPDATE, 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 SHOW CREATE ALL TYPES:
pg_catalog system catalog, pg_type reports domains with typtype = 'd', and pg_constraint includes domain constraints.
To modify a domain, use ALTER DOMAIN:
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 role specification.
To drop the domain, use DROP DOMAIN:
Required privileges
- To create a domain, a user must have the
CREATEprivilege on the parent database and the schema in which the domain is being created. - To drop a domain, a user must be the owner of the domain.
- To alter a domain, a user must be the owner of the domain.
- To grant privileges 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 cast 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, user-defined functions, or user-defined types. ALTER DOMAIN ... VALIDATE CONSTRAINTis not supported.DROP DOMAIN ... CASCADEis not supported.SHOW TYPESdoes not list domains.- The
domains,domain_constraints,domain_udt_usage, andcolumn_domain_usagetables ininformation_schemaare not populated.

