CREATE_DOMAIN(7pg18)
๐ CREATE DOMAIN(7) ๐
okay bestie, sit DOWN, because we're talking about CREATE DOMAIN from the PostgreSQL 18.6 Documentation and honestly? it's giving underrated icon. โจ
๐ธ NAME ๐ธ
CREATE_DOMAIN โ define a new domain. that's it, that's the whole vibe. a whole new domain, made by you, main character energy only. ๐
โจ SYNOPSIS โจ
CREATE DOMAIN name [ AS ] data_type
[ COLLATE collation ]
[ DEFAULT expression ]
[ domain_constraint [ ... ] ]
and domain_constraint is literally this:
[ CONSTRAINT constraint_name ]
{ NOT NULL | NULL | CHECK (expression) }
no notes, it's exact, copy it character for character bestie ๐
๐ฆ DESCRIPTION ๐ฆ
CREATE DOMAIN creates a new domain. so what even IS a domain? it's essentially a data type with optional constraints โ like, restrictions on the allowed set of values. a data type but with rules. and the user who defines a domain becomes its owner. yes, that's you. you own it. ๐ซถ
if a schema name is given (for example, CREATE DOMAIN myschema.mydomain ...) then the domain gets created in the specified schema. otherwise it's created in the current schema. also important: the domain name must be unique among the types and domains existing in its schema. no duplicates, no copycats. ๐
domains are useful for abstracting common constraints on fields into a single location for maintenance. like, 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 a DRY queen. โจ
to be able to create a domain, you must have USAGE privilege on the underlying type. no privilege, no domain. periodt.
๐ PARAMETERS ๐
nameโ the name (optionally schema-qualified) of a domain to be created. cute, pick a good one.data_typeโ the underlying data type of the domain. this can include array specifiers. yes, arrays count, we love a flexible base.collationโ an optional collation for the domain. if no collation is specified, the domain has the same collation behavior as its underlying data type. heads up though: the underlying type must be collatable ifCOLLATEis specified. no collatable base, no collation, that's the rule. ๐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, bestie). 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 a whole hierarchy โ column > domain > underlying type. remember that order. ๐ง
CONSTRAINT constraint_nameโ an optional name for a constraint. if not specified, the system generates a name. the system's got you, it'll name it for you. ๐NOT NULLโ values of this domain are prevented from being null (but see notes below โ the notes are LOWKEY important, don't skip them).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, it exists, but it's not the moment. ๐
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. no cap.
currently, CHECK expressions cannot contain subqueries nor refer to variables other than VALUE. that's the limit, we don't make the rules.
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.) alphabetically, bestie. sort your names accordingly. ๐ค
๐ NOTES ๐
domain constraints, particularly NOT NULL, are checked when converting a value to the domain type. but here's the plot twist: 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. yeah. it just. goes through. ๐ญ
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. put the NOT NULL on the column, not the domain. that's the move. โจ
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.5.1.) so like, same energy as table CHECK constraints. iykyk. ๐
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. drop, adjust, re-add. in that order. don't freestyle it.
it's also good practice to ensure that domain CHECK expressions will not throw errors. just. don't let them throw. be chill, be safe. ๐ซถ
๐ 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. domain defined, table defined, regex doing its little thing. slay. ๐ฅ
โญ COMPATIBILITY โญ
the command CREATE DOMAIN conforms to the SQL standard. we love a standards-compliant moment. โจ
the syntax NOT NULL in this command is a PostgreSQL extension. (a standard-conforming way to write the same for non-composite data types would be CHECK (VALUE IS NOT NULL). however, per the section called โNOTESโ, such constraints are best avoided in practice anyway.) the NULL โconstraintโ is a PostgreSQL extension (see also Compatibility). yeah, quoted โconstraintโ โ that's how the original says it, and honestly? valid. ๐
๐ธ SEE ALSO ๐ธ
ALTER DOMAIN(ALTER_DOMAIN(7))DROP DOMAIN(DROP_DOMAIN(7))
go read those too, bestie, they're the sequel. ๐
and that's the whole page!! ๐ huge thanks to the PostgreSQL 18.6 crew โ this doc is from PostgreSQL 18.6, dated 2026, and the page itself is CREATE DOMAIN(7). love that for us. go forth and define your domains, you absolute legend. ๐โจ๐ฆ
๐ฆ where this page comes from
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms