2 .\" Title: CREATE DOMAIN
3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "CREATE DOMAIN" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 CREATE_DOMAIN \- define a new domain
35 CREATE DOMAIN \fIname\fR [ AS ] \fIdata_type\fR
36 [ COLLATE \fIcollation\fR ]
37 [ DEFAULT \fIexpression\fR ]
38 [ \fIdomain_constraint\fR [ \&.\&.\&. ] ]
40 where \fIdomain_constraint\fR is:
42 [ CONSTRAINT \fIconstraint_name\fR ]
43 { NOT NULL | NULL | CHECK (\fIexpression\fR) }
48 creates a new domain\&. A domain is essentially a data type with optional constraints (restrictions on the allowed set of values)\&. The user who defines a domain becomes its owner\&.
50 If a schema name is given (for example,
51 CREATE DOMAIN myschema\&.mydomain \&.\&.\&.) then the domain is created in the specified schema\&. Otherwise it is created in the current schema\&. The domain name must be unique among the types and domains existing in its schema\&.
53 Domains are useful for abstracting common constraints on fields into a single location for maintenance\&. For example, several tables might contain email address columns, all requiring the same CHECK constraint to verify the address syntax\&. Define a domain rather than setting up each table\*(Aqs constraint individually\&.
55 To be able to create a domain, you must have
57 privilege on the underlying type\&.
62 The name (optionally schema\-qualified) of a domain to be created\&.
67 The underlying data type of the domain\&. This can include array specifiers\&.
72 An optional collation for the domain\&. If no collation is specified, the domain has the same collation behavior as its underlying data type\&. The underlying type must be collatable if
77 DEFAULT \fIexpression\fR
81 clause specifies a default value for columns of the domain data type\&. The value is any variable\-free expression (but subqueries are not allowed)\&. The data type of the default expression must match the data type of the domain\&. If no default value is specified, then the default value is the null value\&.
83 The default expression will be used in any insert operation that does not specify a value for the column\&. If a default value is defined for a particular column, it overrides any default associated with the domain\&. In turn, the domain default overrides any default value associated with the underlying data type\&.
86 CONSTRAINT \fIconstraint_name\fR
88 An optional name for a constraint\&. If not specified, the system generates a name\&.
93 Values of this domain are prevented from being null (but see notes below)\&.
98 Values of this domain are allowed to be null\&. This is the default\&.
100 This clause is only intended for compatibility with nonstandard SQL databases\&. Its use is discouraged in new applications\&.
103 CHECK (\fIexpression\fR)
106 clauses specify integrity constraints or tests which values of the domain must satisfy\&. Each constraint must be an expression producing a Boolean result\&. It should use the key word
108 to refer to the value being tested\&. Expressions evaluating to TRUE or UNKNOWN succeed\&. If the expression produces a FALSE result, an error is reported and the value is not allowed to be converted to the domain type\&.
112 expressions cannot contain subqueries nor refer to variables other than
115 When a domain has multiple
117 constraints, they will be tested in alphabetical order by name\&. (PostgreSQL
118 versions before 9\&.5 did not honor any particular firing order for
124 Domain constraints, particularly
125 NOT NULL, are checked when converting a value to the domain type\&. It is possible for a column that is nominally of the domain type to read as null despite there being such a constraint\&. For example, this can happen in an outer\-join query, if the domain column is on the nullable side of the outer join\&. A more subtle example is
131 INSERT INTO tab (domcol) VALUES ((SELECT domcol FROM tab WHERE false));
137 The empty scalar sub\-SELECT will produce a null value that is considered to be of the domain type, so no further constraint checking is applied to it, and the insertion will succeed\&.
139 It is very difficult to avoid such problems, because of SQL\*(Aqs general assumption that a null value is a valid value of every data type\&. Best practice therefore is to design a domain\*(Aqs constraints so that a null value is allowed, and then to apply column
141 constraints to columns of the domain type as needed, rather than directly to the domain type\&.
146 constraints\*(Aq conditions are immutable, that is, they will always give the same result for the same input value\&. This assumption is what justifies examining
148 constraints only when a value is first converted to be of a domain type, and not at other times\&. (This is essentially the same as the treatment of table
150 constraints, as described in
153 An example of a common way to break this assumption is to reference a user\-defined function in a
155 expression, and then change the behavior of that function\&.
157 does not disallow that, but it will not notice if there are stored values of the domain type that now violate the
159 constraint\&. That would cause a subsequent database dump and restore to fail\&. The recommended way to handle such a change is to drop the constraint (using
160 \fBALTER DOMAIN\fR), adjust the function definition, and re\-add the constraint, thereby rechecking it against stored data\&.
162 It\*(Aqs also good practice to ensure that domain
164 expressions will not throw errors\&.
167 This example creates the
169 data type and then uses the type in a table definition\&. A regular expression test is used to verify that the value looks like a valid US postal code:
175 CREATE DOMAIN us_postal_code AS TEXT
177 VALUE ~ \*(Aq^\ed{5}$\*(Aq
178 OR VALUE ~ \*(Aq^\ed{5}\-\ed{4}$\*(Aq
181 CREATE TABLE us_snail_addy (
182 address_id SERIAL PRIMARY KEY,
183 street1 TEXT NOT NULL,
187 postal us_postal_code NOT NULL
197 conforms to the SQL standard\&.
203 extension\&. (A standard\-conforming way to write the same for non\-composite data types would be
204 CHECK (VALUE IS NOT NULL)\&. However, per
205 the section called \(lqNOTES\(rq, such constraints are best avoided in practice anyway\&.) The
213 ALTER DOMAIN (\fBALTER_DOMAIN\fR(7)), DROP DOMAIN (\fBDROP_DOMAIN\fR(7))