Data types that matter
Choosing the right type protects your data. A column typed date can never contain "next Tuesday-ish", and a numeric price never loses a cent. PostgreSQL has dozens of types; these are the ones you'll use daily.
Numbers
| Type | Range / precision | Use for |
|---|---|---|
smallint |
±32,767 | Small counters |
integer |
±2.1 billion | Most ids and counts |
bigint |
±9.2 quintillion | Ids on big tables, money in cents |
numeric(p, s) |
Exact, up to 131,072 digits | Money, anything that must be exact |
real, double precision |
Approximate (floating point) | Science, measurements |
Floating point is fast but approximate. Never use it for money:
:: is Postgres's cast operator: '42'::integer converts text to a number. The standard CAST('42' AS integer) works too.
Watch out: dividing two integers gives an integer (9 / 2 = 4).
Text
| Type | Notes |
|---|---|
text |
Any length. The default choice. |
varchar(n) |
Text with a maximum length; errors if longer |
char(n) |
Fixed length, padded with spaces. Rarely useful. |
In Postgres, text and varchar perform identically. Use varchar(n) only when a real limit exists (or better, a CHECK constraint, covered in the next lesson).
Booleans
boolean holds true, false or NULL. psql displays them as t and f:
Dates and times
| Type | Example | Use for |
|---|---|---|
date |
2025-03-14 |
Birthdays, due dates |
timestamptz |
2025-03-14 09:30:00+00 |
Events: when something happened |
timestamp |
2025-03-14 09:30:00 |
Rarely: a wall-clock time without a zone |
interval |
3 days 04:00:00 |
Durations |
time |
09:30:00 |
Opening hours |
Use timestamptz for real events. Despite the name, it doesn't store a time zone: it stores an exact moment (in UTC) and converts to the session's time zone for display. Plain timestamp ignores time zones, which causes bugs when servers or users are in different zones.
Same moment, displayed in two zones.
UUIDs
A UUID is a 128-bit random identifier, such as 0b6f…. They're handy when ids must be generated in many places, or when you don't want ids to be guessable (/orders/7 reveals how many orders you have).
PostgreSQL 18 added uuidv7(). Version 7 UUIDs start with a timestamp, so new ones sort after old ones. That keeps indexes compact and fast, making them a better primary key than random v4 UUIDs.
Enums
An enum is a type with a fixed list of values:
Enums sort in the order you declared them, not alphabetically. You can add values later (ALTER TYPE … ADD VALUE), but you can't easily remove one, so many teams prefer a text column with a CHECK constraint instead.
Generated columns
A generated column is computed from other columns, so it can never get out of sync:
From PostgreSQL 18, generated columns are virtual by default (computed when read). Add STORED to compute once on write and save it to disk.
Domains: reusable rules
A domain is a type plus a rule, reusable across tables:
(~ is Postgres's regular-expression match operator.)
Practice
Create an events table with:
- a
uuidv7()primary key title text NOT NULLstarts_at timestamptz NOT NULLduration interval DEFAULT '1 hour'ticket_price numeric(8,2)- an enum
kindwith valuestalk,workshop,party
Insert two events, then select each event's end time (starts_at + duration).
1.Which type should store a product's price?
2.Which type should store "when the order was placed"?
3.Why is uuidv7() a better primary key than gen_random_uuid()?