PostgreSQL Data Types
Text, bytes, integers, floats, money, timestamps and auto-numbered ids: which PostgreSQL column type to pick, and why it matters.

In the basic queries post every column was VARCHAR(128)
or SERIAL. Those two go a long way, but every column in a table has a type,
and picking the right one decides what the column can hold, how much space it
takes and how it sorts.
This post covers the types you'll reach for most: text, raw bytes, whole numbers, decimals, money, timestamps and auto-numbered ids.
All the types
CREATE TABLE examples ( id SERIAL PRIMARY KEY, -- numbered for you: 1, 2, 3… country CHAR(2), -- always exactly 2 characters email VARCHAR(128), -- up to 128 characters body TEXT, -- any length thumbnail BYTEA, -- raw bytes, no character set likes INTEGER, -- whole numbers up to about 2 billion views BIGINT, -- much bigger whole numbers temperature REAL, -- approximate decimals orbit DOUBLE PRECISION, -- more precise decimals price NUMERIC(14, 2), -- exact decimals, for money created_at TIMESTAMP -- a date and a time);Text: CHAR, VARCHAR and TEXT
country CHAR(2),email VARCHAR(128),body TEXTVARCHAR(128): text of up to 128 characters. PostgreSQL stores a small length followed by just the characters you gave it, so short values take little space. This is the one to use most of the time.CHAR(2): text of exactly 2 characters. Shorter values are padded with spaces to fill the space. Use it when every value really is the same length: two-letter country codes, or a SHA-256 hash written as 64 hex characters.TEXT: text with no length limit. Use it for long writing where you don't want the database to enforce a maximum: blog posts, comments, whole web pages saved by a crawler.
Many databases have a long list of text types (tiny, medium, long…).
PostgreSQL keeps it simple: if you know the maximum length, use VARCHAR(n);
if you don't, use TEXT.
Long TEXT columns usually aren't what you sort or filter by. A search like
WHERE body LIKE '%postgres%' works, but PostgreSQL has to read every row to
answer it, so expect it to be slow on a big table.
Character sets
CHAR, VARCHAR and TEXT all know their character set, which is the
point of using them for anything a person types.
- Plain English letters, digits and punctuation (ASCII) fit in one byte (8 bits) each.
- Characters like
ç,ñ,éand Chinese, Japanese or Korean characters need more: two, three or four bytes each in UTF-8. So 100 characters can take up to 400 bytes. - Different languages also sort differently, and the database handles that for you.
You can't build a web app that only accepts English letters: people will type names, comments and posts in their own language. Text columns make sure those characters are stored, compared and sorted correctly.
Raw bytes: BYTEA
thumbnail BYTEABYTEA: a "byte array", often called a blob (binary large object). It holds raw bytes and has no character set, so PostgreSQL doesn't treat it as text at all.- Use it for data that isn't human writing: a small image, a file, an encrypted value.
Whole numbers: SMALLINT, INTEGER and BIGINT
likes INTEGER,views BIGINTINTEGER: 4 bytes, from about −2.1 billion to 2.1 billion. The right choice for almost every number column.SMALLINT: 2 bytes, from −32,768 to 32,767. Saves a little space, but it's easy to outgrow.BIGINT: 8 bytes, up to about 9.2 quintillion. For counts that might pass 2 billion, like page views or ids in a very large table.
Decimals: REAL and DOUBLE PRECISION
temperature REAL,orbit DOUBLE PRECISIONREAL: 4 bytes, about 6 significant digits. The decimal point can sit anywhere in those digits, so it can hold123456or0.123456but not123456.789exactly. Fine for approximate values, like an average temperature over a year.DOUBLE PRECISION: 8 bytes, about 15 significant digits. It's called "double" because it's twice the size. Use it for scientific work and simulations, where a calculation runs over and over and small errors inREALwould pile up.
Money: NUMERIC
price NUMERIC(14, 2)NUMERIC(14, 2): an exact decimal with 14 digits in total, 2 of them after the decimal point. That holds up to999999999999.99.
REAL and DOUBLE PRECISION store fractions in binary (halves, quarters,
eighths…). A cent is one hundredth of a dollar, and one hundredth can't be
written exactly in binary, the same way one third can't be written exactly in
decimal. Add up enough of those tiny errors and the books stop balancing.
SELECT 0.1::DOUBLE PRECISION + 0.2::DOUBLE PRECISION; -- 0.30000000000000004SELECT 0.1::NUMERIC + 0.2::NUMERIC; -- 0.3Dates and times: TIMESTAMP
created_at TIMESTAMPTIMESTAMP: a date and a time of day, like2026-11-15 09:30:00. PostgreSQL also hasDATE(a day only) andTIME(a time only), butTIMESTAMPis the one you'll use most.- It's stored as a 64-bit number and covers 4713 BC to 294276 AD, down to the microsecond.
Add DEFAULT NOW() to fill it in automatically when a row is inserted:
created_at TIMESTAMP DEFAULT NOW()Why 64 bits matters: the year 2038 problem
Unix systems have long kept time as the number of seconds since January 1, 1970, stored in a signed 32-bit number. That number runs out on January 19, 2038, and anything still counting that way wraps around to 1901.
A 64-bit timestamp, like PostgreSQL's, doesn't run out for hundreds of thousands of years, so it's not something you need to worry about.
Auto-numbered ids: SERIAL
id SERIAL PRIMARY KEYSERIAL: anINTEGERthat PostgreSQL fills in for you, counting up 1, 2, 3… with each new row. You leave it out of yourINSERTand the database picks the next number.BIGSERIAL: the same, but aBIGINT, for tables that might pass 2 billion rows.PRIMARY KEY: makes the id unique, so it's how each row is identified.
Which type should I use?
- Short text a person types (name, email, title):
VARCHAR(n) - Text that's always the same length (country code, hash):
CHAR(n) - Long writing (posts, comments, pages):
TEXT - Files, images, raw bytes:
BYTEA - Counts and whole numbers:
INTEGER, orBIGINTif it could pass 2 billion - Measurements and science:
DOUBLE PRECISION - Money:
NUMERIC(14, 2) - When something happened:
TIMESTAMP - Row ids:
SERIAL PRIMARY KEY