CREATE_DOMAIN(7pg17)

πŸ“¦ version 17.11, in opensuse-leap 16.0

πŸ’… CREATE DOMAIN (7pg17) β€” like, the whole PostgreSQL 17.11 documentation moment

okay bestie, so this is the CREATE DOMAIN(7) page from the PostgreSQL 17.11 docs and I'm gonna walk you through it like we're texting at 2am, but every single flag and caveat is staying in. no cap. 🫢


✨ NAME ✨

CREATE_DOMAIN β€” literally define a new domain. that's the whole vibe. 🌸


πŸŽ€ SYNOPSIS πŸŽ€

CREATE DOMAIN name [ AS ] data_type
    [ COLLATE collation ]
    [ DEFAULT expression ]
    [ domain_constraint [ ... ] ]

where domain_constraint is:

[ CONSTRAINT constraint_name ]
{ NOT NULL | NULL | CHECK (expression) }

copy it exactly, bestie, don't freestyle it πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

CREATE DOMAIN creates a new domain. and a domain is basically a data type with optional constraints β€” meaning restrictions on the allowed set of values. cute, right? and the user who defines a domain becomes its owner. main character energy, honestly πŸ‘‘.

if you give a schema name (like CREATE DOMAIN myschema.mydomain ...) then the domain gets created in that specified schema. otherwise it lands in the current schema. and the domain name must be unique among the types and domains that already exist in its schema β€” no duplicate names, that's giving chaos πŸ™ˆ.

so why do domains even exist? they're useful for abstracting common constraints on fields into a single location for maintenance. like β€” several tables might have email address columns, all needing the same CHECK constraint to verify the address syntax. define a domain instead of setting up each table's constraint individually. literally the definition of work smarter not harder ✨.

to be able to create a domain, you must have USAGE privilege on the underlying type. that's the entry fee, bestie πŸ”₯


πŸ’– PARAMETERS πŸ’–

  • name β€” the name of a domain to be created, optionally schema-qualified. yes you can prefix it with the schema, go off πŸŽ€
  • data_type β€” the underlying data type of the domain. this can include array specifiers. arrays! iconic.
  • 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 if COLLATE is specified β€” that's a hard requirement, not a suggestion πŸ’…
  • DEFAULT expression β€” the DEFAULT clause specifies a default value for columns of the domain data type. the value is any variable-free expression (but subqueries are not allowed 🚫). 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 gets used in any insert operation that doesn't specify a value for the column. and 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 layered priority system, and honestly? slay.

  • CONSTRAINT constraint_name β€” an optional name for a constraint. if not specified, the system generates a name. the system's got you 😌
  • NOT NULL β€” values of this domain are prevented from being null (but see the notes below, bestie, 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, fr fr.
  • CHECK (expression) β€” CHECK clauses 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 word VALUE to refer to the value being tested. expressions evaluating to TRUE or UNKNOWN succeed. if the expression produces a FALSE result, an error is reported and the value is not allowed to be converted to the domain type. no sneaking past the bouncer πŸ’€

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 yeah, alphabetical is the moment now ⭐


🌈 NOTES 🌈

okay this part is important, so pay attention bestie 🫢

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. lowkey cursed.

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, periodt πŸ’…

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.)

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.

it's also good practice to ensure that domain CHECK expressions will not throw errors. just saying πŸ™ˆ

πŸ“ 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, no crumbs πŸ”₯


🧚 COMPATIBILITY 🧚

the command CREATE DOMAIN conforms to the SQL standard ✨

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).


πŸ’œ SEE ALSO πŸ’œ


and that's the whole page, bestie!! πŸ¦‹ this lil beauty is from PostgreSQL 17.11, dated 2026 β€” go forth and constrain responsibly, iykyk πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from