ALTER_DOMAIN(7pg16)
π ALTER DOMAIN π
okay bestie, so this is ALTER DOMAIN(7) from the PostgreSQL 16.15 Documentation, and it's literally the glow-up page for domains. you already made a domain with CREATE DOMAIN, and now you wanna restyle it? this is your page. π
β¨ SYNOPSIS β¨
ALTER DOMAIN name
{ SET DEFAULT expression | DROP DEFAULT }
ALTER DOMAIN name
{ SET | DROP } NOT NULL
ALTER DOMAIN name
ADD domain_constraint [ NOT VALID ]
ALTER DOMAIN name
DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ]
ALTER DOMAIN name
RENAME CONSTRAINT constraint_name TO new_constraint_name
ALTER DOMAIN name
VALIDATE CONSTRAINT constraint_name
ALTER DOMAIN name
OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
ALTER DOMAIN name
RENAME TO new_name
ALTER DOMAIN name
SET SCHEMA new_schema
where domain_constraint is:
[ CONSTRAINT constraint_name ]
{ NOT NULL | CHECK (expression) }
like, it's giving "many sub-forms, one command." periodt. π
πΈ DESCRIPTION πΈ
ALTER DOMAIN changes the definition of an existing domain. (key word: existing. this is not a makeover-from-scratch moment.) there are several sub-forms, and honestly each one is kind of iconic in its own way:
π¦ SET/DROP DEFAULT
these forms set or remove the default value for a domain. ok but important: defaults only apply to subsequent INSERT commands β they do not affect rows already chilling in a table that uses the domain. so no retroactive magic, sorry bestie π
π¦ SET/DROP NOT NULL
these forms change whether a domain is marked to allow NULL values or to reject NULL values. and here's the vibe check: you can only SET NOT NULL when the columns using the domain contain no null values. (you can't just β¨manifestβ¨ it, the data has to actually cooperate.)
π¦ ADD domain_constraint [ NOT VALID ]
this form adds a new constraint to a domain. when a new constraint gets 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 NOT VALID option; the constraint can later be made valid using ALTER DOMAIN ... VALIDATE CONSTRAINT. and listen β newly inserted or updated rows are always checked against all constraints, even those marked NOT VALID. fr fr, no skipping the line. NOT VALID is only accepted for CHECK constraints. π«Ά
π¦ DROP CONSTRAINT [ IF EXISTS ]
this form drops constraints on a domain. if IF EXISTS is specified and the constraint does not exist, no error is thrown β in that case a notice is issued instead. very "no big deal, moving on" of it. π
π¦ RENAME CONSTRAINT
this form changes the name of a constraint on a domain. that's it, that's the whole vibe. π
π¦ VALIDATE CONSTRAINT
this form validates a constraint previously added as NOT VALID, that is, it verifies that all values in table columns of the domain type satisfy the specified constraint. the receipts-check of SQL, honestly. π§
π¦ OWNER
this form changes the owner of the domain to the specified user.
π¦ RENAME
this form changes the name of the domain.
π¦ SET SCHEMA
this form changes the schema of the domain. any constraints associated with the domain are moved into the new schema as well β they're coming with you, no domain left behind. π
β οΈ permissions check, bestie β you must own the domain to useALTER DOMAIN. to change the schema of a domain, you must also haveCREATEprivilege on the new schema. to alter the owner, you must be able toSET ROLEto the new owning role, and that role must haveCREATEprivilege on the domain's schema. these restrictions enforce that altering the owner doesn't do anything you couldn't do by dropping and recreating the domain. however, a superuser can alter ownership of any domain anyway β main character energy, genuinely. π
π PARAMETERS π
nameβ the name (possibly schema-qualified) of an existing domain to alter.domain_constraintβ new domain constraint for the domain.constraint_nameβ name of an existing constraint to drop or rename.NOT VALIDβ do not verify existing stored data for constraint validity. (the "we'll deal with it later" flag π)CASCADEβ automatically drop objects that depend on the constraint, and in turn all objects that depend on those objects (see Section 5.14). it's the domino-effect option π’RESTRICTβ refuse to drop the constraint if there are any dependent objects. this is the default behavior. the cautious queen option πnew_nameβ the new name for the domain.new_constraint_nameβ the new name for the constraint.new_ownerβ the user name of the new owner of the domain.new_schemaβ the new schema for the domain.
π NOTES π
ok so this part is the important lore. π
although ALTER DOMAIN ADD CONSTRAINT attempts to verify that existing stored data satisfies the new constraint, this check is not bulletproof, because the command cannot "see" table rows that are newly inserted or updated and not yet committed. (it's not being shady, it just literally can't see them. π)
if there is a hazard that concurrent operations might insert bad data, the way to proceed is:
- add the constraint using the
NOT VALIDoption - commit that command
- wait until all transactions started before that commit have finished
- then issue
ALTER DOMAIN VALIDATE CONSTRAINTto 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. slay, consistency. β¨
also, currently: ALTER DOMAIN ADD CONSTRAINT, ALTER DOMAIN VALIDATE CONSTRAINT, and ALTER DOMAIN SET NOT NULL 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 β but as of rn, that's the situation. π
π₯ EXAMPLES π₯
to add a NOT NULL constraint to a domain:
ALTER DOMAIN zipcode SET NOT NULL;
to remove a NOT NULL constraint from a domain:
ALTER DOMAIN zipcode DROP NOT NULL;
to add a check constraint to a domain:
ALTER DOMAIN zipcode ADD CONSTRAINT zipchk CHECK (char_length(VALUE) = 5);
to remove a check constraint from a domain:
ALTER DOMAIN zipcode DROP CONSTRAINT zipchk;
to rename a check constraint on a domain:
ALTER DOMAIN zipcode RENAME CONSTRAINT zipchk TO zip_check;
to move the domain into a different schema:
ALTER DOMAIN zipcode SET SCHEMA customers;
literally so clean, so readable, we love a domain with range. π¦
π COMPATIBILITY π
ALTER DOMAIN conforms to the SQL standard, except for the OWNER, RENAME, SET SCHEMA, and VALIDATE CONSTRAINT variants, which are PostgreSQL extensions. the NOT VALID clause of the ADD CONSTRAINT variant is also a PostgreSQL extension. so basically most of it is standard and a few bits are our faves being extra. π
π SEE ALSO π
CREATE DOMAIN(CREATE_DOMAIN(7))DROP DOMAIN(DROP_DOMAIN(7))
and that's the page bestie!! π«Ά go forth and alter responsibly. mwah. π
PostgreSQL 16.15 Β· 2026 Β· ALTER DOMAIN(7) β stan the docs, see u next page πΈβ¨
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms