2 .\" Title: ALTER 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 "ALTER 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 ALTER_DOMAIN \- change the definition of a domain
35 ALTER DOMAIN \fIname\fR
36 { SET DEFAULT \fIexpression\fR | DROP DEFAULT }
37 ALTER DOMAIN \fIname\fR
38 { SET | DROP } NOT NULL
39 ALTER DOMAIN \fIname\fR
40 ADD \fIdomain_constraint\fR [ NOT VALID ]
41 ALTER DOMAIN \fIname\fR
42 DROP CONSTRAINT [ IF EXISTS ] \fIconstraint_name\fR [ RESTRICT | CASCADE ]
43 ALTER DOMAIN \fIname\fR
44 RENAME CONSTRAINT \fIconstraint_name\fR TO \fInew_constraint_name\fR
45 ALTER DOMAIN \fIname\fR
46 VALIDATE CONSTRAINT \fIconstraint_name\fR
47 ALTER DOMAIN \fIname\fR
48 OWNER TO { \fInew_owner\fR | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
49 ALTER DOMAIN \fIname\fR
50 RENAME TO \fInew_name\fR
51 ALTER DOMAIN \fIname\fR
52 SET SCHEMA \fInew_schema\fR
54 where \fIdomain_constraint\fR is:
56 [ CONSTRAINT \fIconstraint_name\fR ]
57 { NOT NULL | CHECK (\fIexpression\fR) }
62 changes the definition of an existing domain\&. There are several sub\-forms:
66 These forms set or remove the default value for a domain\&. Note that defaults only apply to subsequent
68 commands; they do not affect rows already in a table using the domain\&.
73 These forms change whether a domain is marked to allow NULL values or to reject NULL values\&. You can only
75 when the columns using the domain contain no null values\&.
78 ADD \fIdomain_constraint\fR [ NOT VALID ]
80 This form adds a new constraint to a domain\&. When a new constraint is added to a domain, all columns using that domain will be checked against the newly added constraint\&. These checks can be suppressed by adding the new constraint using the
82 option; the constraint can later be made valid using
83 \fBALTER DOMAIN \&.\&.\&. VALIDATE CONSTRAINT\fR\&. Newly inserted or updated rows are always checked against all constraints, even those marked
91 DROP CONSTRAINT [ IF EXISTS ]
93 This form drops constraints on a domain\&. If
95 is specified and the constraint does not exist, no error is thrown\&. In this case a notice is issued instead\&.
100 This form changes the name of a constraint on a domain\&.
105 This form validates a constraint previously added as
106 NOT VALID, that is, it verifies that all values in table columns of the domain type satisfy the specified constraint\&.
111 This form changes the owner of the domain to the specified user\&.
116 This form changes the name of the domain\&.
121 This form changes the schema of the domain\&. Any constraints associated with the domain are moved into the new schema as well\&.
124 You must own the domain to use
125 \fBALTER DOMAIN\fR\&. To change the schema of a domain, you must also have
127 privilege on the new schema\&. To alter the owner, you must be able to
129 to the new owning role, and that role must have
131 privilege on the domain\*(Aqs schema\&. (These restrictions enforce that altering the owner doesn\*(Aqt do anything you couldn\*(Aqt do by dropping and recreating the domain\&. However, a superuser can alter ownership of any domain anyway\&.)
137 The name (possibly schema\-qualified) of an existing domain to alter\&.
140 \fIdomain_constraint\fR
142 New domain constraint for the domain\&.
145 \fIconstraint_name\fR
147 Name of an existing constraint to drop or rename\&.
152 Do not verify existing stored data for constraint validity\&.
157 Automatically drop objects that depend on the constraint, and in turn all objects that depend on those objects (see
163 Refuse to drop the constraint if there are any dependent objects\&. This is the default behavior\&.
168 The new name for the domain\&.
171 \fInew_constraint_name\fR
173 The new name for the constraint\&.
178 The user name of the new owner of the domain\&.
183 The new schema for the domain\&.
188 \fBALTER DOMAIN ADD CONSTRAINT\fR
189 attempts to verify that existing stored data satisfies the new constraint, this check is not bulletproof, because the command cannot
191 table rows that are newly inserted or updated and not yet committed\&. If there is a hazard that concurrent operations might insert bad data, the way to proceed is to add the constraint using the
193 option, commit that command, wait until all transactions started before that commit have finished, and then issue
194 \fBALTER DOMAIN VALIDATE CONSTRAINT\fR
195 to search for data violating the constraint\&. This method is reliable because once the constraint is committed, all new transactions are guaranteed to enforce it against new values of the domain type\&.
198 \fBALTER DOMAIN ADD CONSTRAINT\fR,
199 \fBALTER DOMAIN VALIDATE CONSTRAINT\fR, and
200 \fBALTER DOMAIN SET NOT NULL\fR
201 will fail if the named domain or any derived domain is used within a container\-type column (a composite, array, or range column) in any table in the database\&. They should eventually be improved to be able to verify the new constraint for such nested values\&.
206 constraint to a domain:
212 ALTER DOMAIN zipcode SET NOT NULL;
220 constraint from a domain:
226 ALTER DOMAIN zipcode DROP NOT NULL;
232 To add a check constraint to a domain:
238 ALTER DOMAIN zipcode ADD CONSTRAINT zipchk CHECK (char_length(VALUE) = 5);
244 To remove a check constraint from a domain:
250 ALTER DOMAIN zipcode DROP CONSTRAINT zipchk;
256 To rename a check constraint on a domain:
262 ALTER DOMAIN zipcode RENAME CONSTRAINT zipchk TO zip_check;
268 To move the domain into a different schema:
274 ALTER DOMAIN zipcode SET SCHEMA customers;
284 standard, except for the
299 CREATE DOMAIN (\fBCREATE_DOMAIN\fR(7)), DROP DOMAIN (\fBDROP_DOMAIN\fR(7))