DOMAIN Type
Last updated: 2026-11-04
DOMAIN Type
A domain is a reusable custom type that bundles a value range, a default and NOT NULL into one named object shared by many columns across many tables. Where CHECK constraints would be repeated column by column, a domain defines the rule once, lets you change it centrally, and makes it easy to look up in the catalog which business rules a table actually uses.
| Kind | constraint |
|---|---|
| Example Language | SQL |
📝 Syntax
`CREATE DOMAIN name AS basetype [DEFAULT expr] [NOT NULL] [CHECK (expr)]`
Drop: `DROP DOMAIN name`
Inspect: `\dD+ name`
⚙️ Parameters
| name | The domain name. It is a schema object and can be reused across many tables. |
|---|---|
| basetype | The underlying type; columns using the domain must be compatible with it. |
| CHECK (expr) | A constraint attached to the domain that applies to every column using it. VALUE stands for the value being checked. |
Returns:CREATE DOMAIN returns success on completion. When a domain is used as a column type, violating it raises `value for domain
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
SELECT 'insert amount = 250.00' AS step, 'OK' AS result
UNION ALL SELECT 'insert amount = -5.00', (SELECT pg_try('INSERT INTO invoices VALUES (2, -5.00)'))
UNION ALL SELECT 'rows stored', (SELECT count(*)::TEXT FROM invoices)
step | result
------------------------+------------------------------------------------------------------------
insert amount = 250.00 | OK
insert amount = -5.00 | value for domain pos_money violates check constraint "pos_money_check"
rows stored | 1
(3 rows)
📌 Key Points
- A domain is a schema object rather than a table constraint: it lives in pg_type, not in pg_constraint.
- Columns using a domain inherit every constraint on it, including CHECK and NOT NULL.
- Drop the table before the domain: while a column still references it, DROP DOMAIN fails with `cannot drop type ... because other objects depend on it`.
- Keep the base type consistent when reusing a domain across tables, since it constrains what each column can hold.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QShould I use a domain or a CHECK constraint?
AA one-off rule for a single column is fine as a CHECK. Use a domain when the same rule must be reused across tables, or when the rule also carries a default and NOT NULL semantics. A domain additionally makes the business meaning of a column visible at the type level.
QCan a foreign key be defined on a domain?
AA foreign key must be declared on a concrete column, not on a domain. Once a column uses the domain, though, a foreign key can reference that column as usual.