PostgreSQL Best Practices

Chapter 2 · Practice 1 of 4

Make columns NOT NULL unless null has a real meaning

From Constraints that protect your data

Why it matters

Every nullable column adds a third state your code has to handle, and NULL does not behave like a normal value: NULL = NULL is not true. Most columns should never be empty, so the safe default is to say so.

  • Declare columns NOT NULL and give them a default where it makes sense.
  • Leave a column nullable only when "unknown" or "not yet" is a real state, such as deleted_at or shipped_at.