IT lexicon › Database › DOMAIN (Postgres)

DOMAIN (Postgres)

Database På svenska → Updated: 2026-05-27

PostgreSQL feature: create your own type with an embedded constraint. CREATE DOMAIN email AS TEXT CHECK (VALUE ~ '^[^@]+@[^@]+$'). Use as a column type → the constraint follows automatically.

Usage: avoid repeating the same CHECK in 20 tables. CREATE DOMAIN positive_int AS INTEGER CHECK (VALUE >= 0). Differs from CREATE TYPE which is for composite types. Migration: ALTER DOMAIN changes all tables using it. Trade-off: hides logic in the type system (harder to see in the table schema). Not a popular feature — many PostgreSQL users do the same thing with inline CHECK. ENUM types occupy the same "type constraints" ground but for fixed value lists.

← Back to the lexicon