Domains#
What is a Domain in PostgreSQL?#
A DOMAIN is a custom data type built on top of an existing type, with rules attached.
- Think of it as:
βA reusable column ruleβ
Instead of repeating the same constraints in every table, you define them once as a domain.
Simple mental model#
Without Domain |
With Domain |
|---|---|
Rules repeated in every column |
Rules defined once |
Hard to maintain |
Centralized |
Error-prone |
Safe & consistent |
Basic syntax
CREATE DOMAIN domain_name AS base_type [CONSTRAINT ...];
Very simple example
β Without domain
CREATE TABLE users (
username TEXT NOT NULL CHECK (length(username) >= 3)
);
You must repeat this everywhere.
β With domain
CREATE DOMAIN username_t AS TEXT
CHECK (length(VALUE) >= 3);
CREATE TABLE users (
username username_t
);
β Same rule
β Defined once
β Reusable
What is VALUE?#
- Inside a domain:
VALUE = the value being inserted
Similar to NEW.column in triggers
Domains in pgAdmin
In pgAdmin:
Schemas
βββ public
βββ Domains
These are schema-level reusable types.
Why Domains are powerful#
- Domains can enforce:
β NOT NULL
β CHECK constraints
β DEFAULT values
β Language rules
β Formatting rules
Pashto-specific examples#
1οΈβ£ Pashto word domain (Unicode + not empty)
CREATE DOMAIN pashto_word AS TEXT
COLLATE "ps_af" NOT NULL
CHECK (length(trim(VALUE)) > 0);
Use it:
CREATE TABLE pashto_words (
word pashto_word
);
2οΈβ£ Pashto-only characters (advanced)
Example rule (Arabic script range):
CREATE DOMAIN pashto_text AS TEXT
CHECK (VALUE ~ '^[\u0600-\u06FF\s]+$');
This is a regular expression check used inside a DOMAIN or CHECK constraint.
β Rejects Latin text
β Accepts Arabic/Pashto script
3οΈβ£ Language-safe text domain (multi-language)
CREATE DOMAIN human_text AS TEXT
COLLATE "und-x-icu"
NOT NULL;
Use across all tables:
CREATE TABLE articles (
title human_text,
content human_text
);
Domain vs Type#
Feature |
DOMAIN |
TYPE |
|---|---|---|
Based on existing type |
β |
β |
Adds constraints |
β |
β |
Simple |
β |
β |
Best for validation |
β |
β |
Best for structure |
β |
β |
Use DOMAIN for rules, TYPE for structure
Domain vs CHECK constraint#
Aspect |
DOMAIN |
CHECK |
|---|---|---|
Reusable |
β |
β |
Centralized |
β |
β |
Clean schema |
β |
β |
Column-specific |
β |
β |
- Important rules about Domains
Domains do not store data
They apply at insert/update
Changing a domain affects all tables using it
You cannot add indexes to domains
Domains respect collation
Real-world best practices
- β Use domains for:
Usernames
Emails
Language text
Currency values
IDs
Phone numbers
- β Donβt use domains for:
Complex logic
Relationships
Performance tricks
Example error (what users see)
INSERT INTO pashto_words VALUES ('');
β Error:
value for domain pashto_word violates check constraint
Thatβs good β rules are enforced at DB level.
- Summary
A DOMAIN is a reusable, rule-enforced data type that keeps your database clean, consistent, and safe.