CREATE_DOMAIN(7pg16)
β¨ CREATE DOMAIN π
(a.k.a. the CREATE DOMAIN(7) page, PostgreSQL 16.15 docs bestie)
okay so we're doing this β welcome to the CREATE DOMAIN glow-up π this is straight out of the PostgreSQL 16.15 Documentation and it's lowkey one of the most underrated pages, fr.
πΈ NAME πΈ
CREATE_DOMAIN β define a new domain β¨ (yes bestie, you're literally creating a whole new type vibe)
π¦ 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, periodt. And what even is a domain? it's essentially a data type with optional constraints β like, restrictions on the allowed set of values π«Ά. The user who defines a domain becomes its owner, main character energy and all that π.
If you give a schema name (like, for example, CREATE DOMAIN myschema.mydomain ...) then the domain gets created in that specified schema. Otherwise it's created in the current schema. The domain name must be unique among the types and domains existing in its schema β no name-stealing, bestie π.
Domains are so useful for abstracting common constraints on fields into a single location for maintenance. Real talk: several tables might contain email address columns, all requiring the same CHECK constraint to verify the address syntax. Define a domain instead of setting up each table's constraint individually β that's the whole point, it's giving DRY π§.
To be able to create a domain, you must have USAGE privilege on the underlying type. that's the vibe check, no skipping it β
π PARAMETERS π
nameβ the name (optionally schema-qualified) of a domain to be created. that's it, that's the name πdata_typeβ the underlying data type of the domain. This can include array specifiers, so yes, arrays are invited to the party π₯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 β non-negotiable π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, sorry π). 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 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. it's giving hierarchy of defaults, iykyk π
CONSTRAINT constraint_nameβ an optional name for a constraint. If not specified, the system generates a name. the system's got you, bestie π«ΆNOT NULLβ values of this domain are prevented from being null (but see the notes below, there's β¨loreβ¨ π)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 β like, it's a legacy vibe, not a serve π
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. harsh but fair π
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 something ancient, that ordering tea doesn't apply β
π NOTES π
okay this section is the one that matters, bestie, listen up π§
Domain constraints, particularly NOT NULL, are checked when converting a value to the domain type. Which means 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!! π
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. that's the move, fr fr π«Ά
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. chef's kiss, clean, iconic π
It's also good practice to ensure that domain CHECK expressions will not throw errors. ngl that's just self-care π
π 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
);
ate, honestly π β¨
π COMPATIBILITY π
The command CREATE DOMAIN conforms to the SQL standard. slay, no notes π
π SEE ALSO π
ALTER DOMAIN(ALTER_DOMAIN(7))DROP DOMAIN(DROP_DOMAIN(7))
go read those too, bestie, they're literally the sequel π
PostgreSQL 16.15 β 2026 β and that's the whole CREATE DOMAIN(7) page!! you made it to the end, so proud of you π«Άβ¨ now go define some domains and be iconic ππ
π¦ 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