Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

Part 03 / Topic 2 of 9

Choosing types well

Three choices come up constantly, and each has a professional default.

Money is numeric — never real or double precision. Those two are binary floating-point: fast, fine for measurements, but they cannot represent most decimals exactly:

SELECT (0.1::float8 + 0.2::float8)::text AS float_sum,
       0.1::float8 + 0.2::float8 = 0.3   AS float_equal,
       0.1::numeric + 0.2::numeric = 0.3 AS numeric_equal;
      float_sum      | float_equal | numeric_equal 
---------------------+-------------+---------------
 0.30000000000000004 | f           | t
(1 row)

That f is a customer who was charged wrong. shopdb's price numeric(10,2) means: up to 10 digits, 2 after the point — exact, always.

Moments in time are timestamptz. It stores the moment in UTC and converts to your session's zone on display, so a server in Mumbai and a laptop in Berlin agree about when a payment happened:

SELECT TIMESTAMPTZ '2025-01-10 10:15+05:30' AS in_kolkata_time;
      in_kolkata_time      
---------------------------
 2025-01-10 10:15:00+05:30
(1 row)

Plain timestamp keeps a wall-clock reading with no zone attached — fine for "the yoga class is at 07:00", wrong for "when did this order ship". shopdb's payments.paid_at is timestamptz, and so should be almost every "happened at" column you ever create.

Text is text. Unlike some databases, Postgres has no performance penalty for it — varchar(n) is just text plus a length check. Use varchar(n) only when a real rule exists, and even then a CHECK constraint (Stage 3) usually says it better. char(n) pads with spaces and is legacy — avoid.

🧪 Quick quiz — answer from memory, then check.

  1. Why is numeric, not real, the type for prices?
  2. An event time users in different countries will see — timestamp or timestamptz?
Show the answers
  1. Binary floats can't hold most decimals exactly (0.1 + 0.2 ≠ 0.3); numeric is exact.
  2. timestamptz — it stores an absolute moment (internally UTC) and displays it in each viewer's zone.

Done here? The practice set for Part 3 — 7 exercises with solutions — is on the part page.

Search the guide