CREATE_DOMAIN(7pg14)
β¨ CREATE DOMAIN (7pg14) β¨
okay bestie, so this is the PostgreSQL 14.24 documentation for CREATE DOMAIN, and honestly? it's giving "define your own data type with rules" and we're obsessed π
this page is literally your guide to making a domain, which is like a data type but with opinions (constraints, defaults, the whole vibe). let's get into it π«Ά
πΈ SYNOPSIS πΈ
CREATE DOMAIN name [ AS ] data_type
[ COLLATE collation ]
[ DEFAULT expression ]
[ constraint [ ... ] ]
where constraint is:
[ CONSTRAINT constraint_name ]
{ NOT NULL | NULL | CHECK (expression) }
π DESCRIPTION π
CREATE DOMAIN creates a new domain. a domain is basically a data type with optional constraints (aka restrictions on the allowed set of values). and the user who defines a domain becomes its owner β main character energy periodt π
if you give a schema name (like CREATE DOMAIN myschema.mydomain ...) then the domain gets created in that specified schema. otherwise it's created in the current schema. the domain name has to be unique among the types and domains already existing in its schema β no duplicate names bestie, that's the rule π«
domains are lowkey iconic for abstracting common constraints on fields into a single location for maintenance. example: several tables might contain email address columns, all requiring the same CHECK constraint to verify the address syntax. so instead of setting up each table's constraint individually (so much work π), you define a domain once and slay.
to be able to create a domain, you must have USAGE privilege on the underlying type. iykyk π
π PARAMETERS π
nameβ the name (optionally schema-qualified) of a domain to be created. that's literally the name, bestie.data_typeβ the underlying data type of the domain. this can include array specifiers. yes, arrays are valid, we love versatility πcollationβ 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 ifCOLLATEis specified β so don't try this with non-collatable types, it won't end well πDEFAULT expressionβ theDEFAULTclause specifies a default value for columns of the domain data type. the value is any variable-free expression (but subqueries are not allowed β no sneaky subqueries). 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.
the default expression will be used in any insert operation that doesn't 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 β it's giving hierarchy, and the domain is highkey in charge π€
CONSTRAINT constraint_nameβ an optional name for a constraint. if you don't specify one, the system generates a name for you. no stress π«ΆNOT NULLβ values of this domain are prevented from being null (but see notes below β there's a plot twist π).NULLβ values of this domain are allowed to be null. this is the default.
this clause is only intended for compatibility with nonstandard SQL databases. its use is discouraged in new applications. so like, don't use it unless you have to, bestie π
CHECK (expression)βCHECKclauses 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 wordVALUEto refer to the value being tested. expressions evaluating toTRUEorUNKNOWNsucceed. if the expression produces aFALSEresult, an error is reported and the value is not allowed to be converted to the domain type β periodt, the rules are the rules π
currently, CHECK expressions cannot contain subqueries nor refer to variables other than VALUE.
when a domain has multiple CHECK constraints, they will be tested in alphabetical order by name. (PostgreSQL versions before 9.5 did not honor any particular firing order for CHECK constraints β so if you're on an ancient version, it's giving chaos π)
π NOTES π
domain constraints, particularly 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:
INSERT INTO tab (domcol) VALUES ((SELECT domcol FROM tab WHERE false));
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 β sneaky nulls slipping through, and that's valid π³
it is very difficult to avoid such problems, because of SQL's general assumption that a null value is a valid value of every data type. best practice therefore is to design a domain's constraints so that a null value is allowed, and then to apply column NOT NULL constraints to columns of the domain type as needed, rather than directly to the domain type. ngl this is the way to go, bestie π«Ά
PostgreSQL assumes that CHECK constraints' conditions are immutable, that is, they will always give the same result for the same input value. this assumption is what justifies examining CHECK 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 CHECK constraints, as described in Section 5.4.1.)
an example of a common way to break this assumption is to reference a user-defined function in a CHECK expression, and then change the behavior of that function. PostgreSQL does not disallow that, but it will not notice if there are stored values of the domain type that now violate the CHECK 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 ALTER DOMAIN), adjust the function definition, and re-add the constraint, thereby rechecking it against stored data β so like, if you're gonna break the rules, at least fix them after, okay? π
π EXAMPLES π
this example creates the us_postal_code 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:
CREATE DOMAIN us_postal_code AS TEXT
CHECK(
VALUE ~ '^\d{5}$'
OR VALUE ~ '^\d{5}-\d{4}$'
);
CREATE TABLE us_snail_addy (
address_id SERIAL PRIMARY KEY,
street1 TEXT NOT NULL,
street2 TEXT,
street3 TEXT,
city TEXT NOT NULL,
postal us_postal_code NOT NULL
);
look at that, a whole custom postal code type and a table using it β slay π β¨
π¦ COMPATIBILITY π¦
the command CREATE DOMAIN conforms to the SQL standard. we love standards, bestie π
π₯ SEE ALSO π₯
ALTER DOMAIN(ALTER_DOMAIN(7))DROP DOMAIN(DROP_DOMAIN(7))
and that's the whole page, bestie! this was pulled from the PostgreSQL 14.24 documentation, and the date on the original footer is 2026 (the future is now π). you're officially domain-savvy β go forth and define your own data types with confidence and main character energy ππ πΈβ¨
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-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