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.

Kindconstraint
Example LanguageSQL

📝 Syntax

Column level: `column_name type DEFAULT expression`
Values: `DEFAULT` applies the default, while `NULL` explicitly stores NULL (different from the default)

⚙️ Parameters

expressionThe 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

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.
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%

🙏 帮我们做得更好

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

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