PostgreSQL Data Types
Every column has a data type. The type decides which values are allowed, how much space they take, how they sort and which operators and functions work on them. PostgreSQL has a particularly rich set of built-in types; choosing the right one makes your data safer and your queries simpler.
Numbers
| Type | Range / precision | Use for |
|---|---|---|
smallint |
−32,768 to 32,767 | small counters |
integer (int) |
about ±2.1 billion | most ids and counts |
bigint |
about ±9.2 × 10¹⁸ | very large ids, big counters |
numeric(p, s) |
exact, up to p digits, s after the point |
money, measurements that must be exact |
real, double precision |
approximate floating point (6 / 15 digits) | scientific values where tiny errors are fine |
The difference between exact and approximate types is easy to see:
SELECT 0.1::numeric + 0.2::numeric AS exact,
0.1::double precision + 0.2::double precision AS floating,
(0.1::double precision + 0.2::double precision) = 0.3 AS float_equals,
12.345::numeric(5,2) AS rounded_to_2_places; exact | floating | float_equals | rounded_to_2_places
-------+---------------------+--------------+---------------------
0.3 | 0.30000000000000004 | f | 12.35
(1 row)
Values that don't fit are rejected rather than silently cut off:
SELECT 40000::smallint;
SELECT 12345.678::numeric(5,2);ERROR: smallint out of range
ERROR: numeric field overflow
DETAIL: A field with precision 5, scale 2 must round to an absolute value less than 10^3.
Text
| Type | Meaning |
|---|---|
text |
any length; the usual choice in PostgreSQL |
varchar(n) |
at most n characters |
char(n) |
exactly n characters, padded with spaces (rarely useful) |
In PostgreSQL, text and varchar perform the same; use varchar(n) only when the limit is a real business rule.
CREATE TABLE isbn_test (code varchar(13));
INSERT INTO isbn_test VALUES ('9780000000017');
INSERT INTO isbn_test VALUES ('97800000000170');
SELECT 'abc'::char(5) || '|' AS padded, length('Chloé') AS chars, octet_length('Chloé') AS bytes;CREATE TABLE
INSERT 0 1
ERROR: value too long for type character varying(13)
padded | chars | bytes
--------+-------+-------
abc| | 5 | 6
(1 row)
length counts characters while octet_length counts bytes: é takes two bytes in UTF-8.
Booleans
boolean holds true, false or NULL. PostgreSQL accepts several spellings on input ('yes', 'on', '1', 't') and psql displays t / f:
SELECT 'yes'::boolean AS a, 'off'::boolean AS b, (stock > 0) AS in_stock
FROM books WHERE id IN (1, 2); a | b | in_stock
---+---+----------
t | f | t
t | f | f
(2 rows)
Dates and times
| Type | Example | Notes |
|---|---|---|
date |
2024-05-15 |
a calendar day |
time |
14:30:00 |
a time of day |
timestamp |
2024-05-15 14:30:00 |
date + time, no time zone |
timestamptz |
2024-05-15 14:30:00+00 |
an absolute moment; stored in UTC, shown in the session's time zone |
interval |
3 days 04:00:00 |
a duration |
SELECT DATE '2024-02-28' + 1 AS next_day,
TIMESTAMP '2024-05-15 14:30' + INTERVAL '90 minutes' AS later,
DATE '2024-12-25' - DATE '2024-01-01' AS days,
INTERVAL '1 day 3 hours' * 2 AS doubled; next_day | later | days | doubled
------------+---------------------+------+-----------------
2024-02-29 | 2024-05-15 16:00:00 | 359 | 2 days 06:00:00
(1 row)
Tip: For "when did it happen" columns use
timestamptz. It records a real point in time, so it stays correct when users and servers are in different time zones.
PostgreSQL specials
| Type | Example |
|---|---|
uuid |
'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11' |
arrays (text[], integer[]) |
'{sql,beginner}' |
jsonb |
'{"pages": 312}' |
ranges (int4range, daterange) |
'[2024-01-01,2024-02-01)' |
inet, cidr |
'192.168.1.10/24' |
enums (CREATE TYPE … AS ENUM) |
'shipped' |
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
SELECT 'happy'::mood > 'sad'::mood AS enum_order,
daterange('2024-01-01', '2024-02-01') @> DATE '2024-01-15' AS in_range,
'192.168.1.10'::inet << '192.168.1.0/24'::inet AS in_subnet,
pg_typeof(tags) AS tags_type, pg_typeof(details) AS details_type
FROM books WHERE id = 1;CREATE TYPE
enum_order | in_range | in_subnet | tags_type | details_type
------------+----------+-----------+-----------+--------------
t | t | t | text[] | jsonb
(1 row)
Arrays and JSONB get their own lessons later in the course. pg_typeof(x) is handy whenever you are unsure what type an expression has.
Test yourself
Which type should you use to store prices?
- real
- double precision
- numeric(10,2)
- text
What is the practical difference between text and varchar(n) in PostgreSQL?
- text is slower
- varchar(n) enforces a maximum length; otherwise they behave the same
- text can't be indexed
- varchar can store emoji, text can't
Which type represents an absolute moment in time, independent of time zones?
- timestamp
- timestamptz
- date
- time