Skip to main content
A user-defined 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 :
where <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 :
In the pg_catalog system catalog, pg_type reports domains with typtype = 'd', and pg_constraint includes domain constraints. To modify a domain, use :
You can also 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 CREATE on 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 GRANT privilege and the privilege that they want to grant.
  • To create an object that depends on a domain, a user must have the USAGE privilege on the domain.

Examples

Create and use a domain

Create a domain that requires a 5-digit string value:
View the domain definition:
Create a table that uses the domain as a column type:
Insert a value that satisfies the domain’s CHECK constraint:
An INSERT that violates the domain’s CHECK constraint returns an error:
Only the valid row was inserted:
The column definition references the domain:

Default value inheritance

A column that uses a domain inherits the domain’s DEFAULT value. A DEFAULT defined on the column takes precedence. Create a domain with a default value:
Create a table where one column defines its own default value:
Insert a row that uses only default values:
The 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:
A cast that violates the domain’s constraints returns an error:
In expressions, a domain value behaves as a value of its base type.

Known limitations

  • The base type of a domain cannot be an array, composite, or domain data type.
  • Domain DEFAULT and CHECK expressions cannot reference database objects such as tables, sequences, , or user-defined types.
  • ALTER DOMAIN ... VALIDATE CONSTRAINT is not supported.
  • DROP DOMAIN ... CASCADE is not supported.
  • does not list domains.
  • The domains, domain_constraints, domain_udt_usage, and column_domain_usage tables in are not populated.

See also