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.

Kindconstraint
Example LanguageSQL

📝 Syntax

`CREATE DOMAIN name AS basetype [DEFAULT expr] [NOT NULL] [CHECK (expr)]`
Drop: `DROP DOMAIN name`
Inspect: `\dD+ name`

⚙️ Parameters

nameThe domain name. It is a schema object and can be reused across many tables.
basetypeThe 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 violates check constraint "..."`.

▶ 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏