Auto-fill that you can trust
Defaults are the database's auto-fill. When an INSERT skips a column, the default kicks in. This keeps inserts terse, keeps data consistent, and removes a category of "we forgot to set X" bugs.
Static, expression, and generated
Three flavours, in increasing power:
- Static defaults: literal values like
0,'draft',FALSE. - Expression defaults: computed at INSERT time —
now(),CURRENT_DATE,gen_random_uuid(),encode(gen_random_bytes(32),'hex'). - Generated columns: computed from other columns in the same row — STORED (written to disk) or VIRTUAL (computed on read; PG 18+).
Generated columns earn their keep
A generated column derives its value from a formula. It can never be inserted or updated directly — the database keeps it correct. Use them for derived values that you'd otherwise compute in the application a thousand times: full names, totals with tax, slugified titles, normalised search terms.