DEFAULT Constraint
Last updated: 2026-11-04
DEFAULT Constraint
DEFAULT supplies a fallback value for a column, applied automatically whenever an INSERT omits that column, which removes duplicated logic from application code. Typical uses are initial status values, zeroed counters and timestamp columns. Note that a default only kicks in when the column is omitted; an explicit NULL still stores NULL.
| Kind | constraint |
|---|---|
| Example Language | SQL |
📝 Syntax
Column level: `column_name type DEFAULT expression`
Values: `DEFAULT` applies the default, while `NULL` explicitly stores NULL (different from the default)
⚙️ Parameters
| expression | The expression used when the column is omitted. It must be an immutable constant or immutable function (CURRENT_TIMESTAMP is a special case). |
|---|
Returns:DEFAULT has no return value. It applies when an INSERT omits the column; an explicit NULL stores NULL rather than the default.
▶ 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 article_id, title, status, views, score, summary,
(created_at IS NOT NULL) AS created_at_set
FROM articles ORDER BY article_id
article_id | title | status | views | score | summary | created_at_set
------------+-------------+-----------+-------+-------+---------+----------------
1 | Hello PG | draft | 0 | 0.00 | n/a | true
2 | Second Post | published | 42 | 0.00 | n/a | true
3 | Third Post | draft | 0 | 0.00 | NULL | true
(3 rows)
📌 Key Points
- Omitting a column and passing NULL differ: omission uses the default, an explicit NULL stores NULL.
- To use the default explicitly, write the keyword: `INSERT INTO t (c) VALUES (DEFAULT)`.
- A default may call a sequence: `DEFAULT nextval('seq_name')`, which is the mechanism behind SERIAL and IDENTITY.
- CURRENT_TIMESTAMP records the moment the statement began, so every statement in one transaction sees the same value.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhat does DEFAULT together with NOT NULL mean?
AThe column may never be NULL and is auto-filled when omitted — two complementary rules. The most common pairing is `created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP`.
QHow do I change an existing default?
AUse `ALTER TABLE t ALTER COLUMN c SET DEFAULT new_expr`, or `DROP DEFAULT` to remove it. Changing a default affects future rows only and leaves existing data untouched.