PostgreSQL Data Types: A Complete Guide with Examples

Updated

PostgreSQL data types examples

For a new table in PostgreSQL 17, five defaults are right most of the time: text for strings, numeric for money, timestamptz for time, jsonb for documents, and bigint GENERATED ALWAYS AS IDENTITY for surrogate keys. Everything below is the reason to deviate. Every number here came from a scratch PostgreSQL 17.10 cluster on this machine; /verify/postgresql-data-types.sh initialises its own throwaway cluster on a spare port, replays the queries behind these sizes, timings and errors, and removes the cluster on exit, so no cluster you already run is touched.

CREATE TABLE orders (
  id          bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  reference   text        NOT NULL,
  total       numeric(12,2) NOT NULL,
  placed_at   timestamptz NOT NULL DEFAULT now(),
  metadata    jsonb       NOT NULL DEFAULT '{}'
);

varchar(n) is not faster than text. It is not slower either.

The usual folklore about picking a length limit for performance is wrong, and the PostgreSQL manual’s character-types page says so directly: “There is no performance difference among these three types, apart from increased storage space when using the blank-padded type, and a few extra CPU cycles to check the length when storing into a length-constrained column.”

The catalog agrees before you run anything: text, varchar and bpchar all report typlen = -1, typstorage = x, typalign = i — the same varlena machinery. One million 32-character values into each, five runs apiece, in milliseconds:

text          1023   957   996  1037   959
varchar(100)   965   967   970   947   945
char(100)     1221  1208  1229  1208  1201

varchar(100)‘s entire range, 945–970 ms, sits inside text‘s 957–1037 ms. Run-to-run noise on a single type is larger than the gap between the two, so there is no signal to read; your own numbers will differ and the overlap is the only stable part. Heap size is the deterministic figure, and it is where char(n) loses — 65 MB, 65 MB, and 128 MB. char(n) pads to width on disk — octet_length('ab'::char(10)) is 10 while length() reports 2 — and the manual calls it “usually the slowest of the three.” Do not use it.

The real reason to prefer text is migration cost. Widening varchar(10) to varchar(50) keeps the same relfilenode, and so does varchar(20) → text: both are metadata-only. Narrowing 50 → 20 changed the relfilenode, meaning a full table rewrite under ACCESS EXCLUSIVE. Pick text and put real rules in a CHECK constraint, which you can validate without blocking. One trap if you keep varchar(n): inserting an over-long value raises value too long for type character varying(3), but an explicit cast silently truncates — 'abcd'::varchar(3) returns abc.

numeric for money, always

0.1::float8 + 0.2::float8 = 0.3::float8 is false; the sum prints as 0.30000000000000004. Scale that up — summing one million rows of 0.01 stored as float8 returned 10000.000000079015, while numeric(12,2) returned 10000.00. Both occupy 8 bytes for this value, so the float buys you nothing here. Use float8 for measurements and coordinates where the input was approximate anyway; use numeric whenever a human will reconcile the total. Also note PostgreSQL’s non-IEEE ordering: 'NaN'::float8 > 1e308 and 'NaN' = 'NaN' are both true, so NaN sorts last rather than poisoning comparisons.

timestamptz stores an instant; timestamp stores characters

Both are 8 bytes, and timestamptz does not store a timezone. It converts to UTC on input using the session zone and renders back in the session zone on output. Writing '2026-08-10 09:00:00' into both columns from an America/Chicago session and reading it back elsewhere:

-- session TimeZone = Asia/Tokyo
naive 2026-08-10 09:00:00 | aware 2026-08-10 23:00:00+09
-- session TimeZone = UTC
naive 2026-08-10 09:00:00 | aware 2026-08-10 14:00:00+00

The naive column reads the same everywhere and is therefore wrong everywhere but Chicago. Worse, timestamp discards an offset you explicitly supply: '2026-08-10 09:00:00+09'::timestamp yields 2026-08-10 09:00:00, no error. Use timestamp only for wall-clock values with no instant, like a recurring 09:00 store opening. One more edge: precision truncation rounds, so '2026-08-10 23:59:59.7'::timestamp(0) becomes 2026-08-11 00:00:00 — the next day. That same session-zone rule decides where a day boundary falls when you bucket a timestamptz column with DATE_TRUNC: two sessions in different zones group the same row into different days.

jsonb unless you need the bytes back verbatim

'{"b":1, "a":2, "a":3}' survives as typed under json. Under jsonb it becomes {"a": 3, "b": 1} — whitespace dropped, keys sorted, duplicate key resolved last-wins. jsonb also has an equality operator and GIN indexing; json has neither, and '{"a":1}'::json = '{"a":1}'::json fails with operator does not exist: json = json. Choose json only to archive a payload byte-for-byte.

Arrays and ranges

Arrays are 1-based and forgiving: index 0 or 99 on a 3-element array returns NULL, not an error, so a typo goes silent. Use them for small tag lists you never join on. Ranges are the better-kept secret — int4range and daterange canonicalize discrete bounds to [), so '[1,4]' and '[1,5)' compare equal, and '[2026-01-01,2026-02-01)'::daterange @> '2026-01-15'::date replaces a pair of hand-written comparisons. numrange is continuous and keeps [1.0,4.0] inclusive as written.

A free win: column order

Fixed-width columns are alignment-padded. Two tables with identical columns, 300,000 rows each, differing only in declaration order: interleaved int2, int8, int2, int8, int2, int8 occupied 22 MB; the same columns declared widest-first occupied 17 MB. Declare 8-byte columns before 2- and 4-byte ones.

What was and was not verified

Executed on PostgreSQL 17.10: every query, size, timing and error above, against a throwaway initdb cluster deleted afterwards. The quoted sentences come from the PostgreSQL 17 manual, section 8.3. Not covered: TOAST compression on large values, collation effects on text comparison, and index size differences — all real, none measured here.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.