designwithfocus

Back end & development

PostgreSQL Data Types for Numbers, Text, Time and JSON

By Design With Focus Team 7 min read
A table grid with column labels for numbers, text, time, and JSON

Short answer

PostgreSQL data types are the built-in formats a column uses for numbers, text, time, and JSON. The PostgreSQL 19 manual lists integer as a signed 4-byte value from -2147483648 to 2147483647, varchar as characters up to a declared n, and timestamptz as a zoned timestamp from 4713 BC to 294276 AD. jsonb stores decomposed JSON and is the type the manual prefers for most applications.

Last verified against the sources listed below.

Key takeaways

  • integer is 4 bytes, from -2147483648 to 2147483647. Values outside the range raise an error.
  • numeric is exact. real and double precision are inexact IEEE 754 types on supported platforms.
  • varchar(n) counts characters, not bytes. n cannot exceed 10485760. The longest string is about 1 GB.
  • timestamptz is an 8-byte timestamp with time zone conversion, at 1 microsecond resolution.
  • json keeps the input text. jsonb does not keep whitespace, key order, or duplicate keys.

PostgreSQL data types are the built-in column formats for numbers, text, dates, and JSON, plus many narrower types the manual lists beside them.

The pages cited here are the PostgreSQL 19 manual, served as the current documentation. The overview chapter says users can add more types with CREATE TYPE, and it lists the general-purpose built-ins in one table.

A relational table is the setting those columns live in, which is the subject of what a relational database is.

Choosing types is part of back-end development.

Numbers

Integer types store whole numbers in a fixed width. numeric stores exact values. Floating types store approximations.

The numeric types page says an out-of-range integer raises an error. integer is the usual choice, smallint is for when disk space is tight, and bigint is for when integer is not wide enough.

numeric is what the page recommends for money and other values that must stay exact. Arithmetic on numeric is exact where it can be, and it is slow next to integers and floats.

real and double precision follow IEEE 754 on supported platforms, single and double precision. The page says some values are stored as approximations, so equality checks can fail. It gives real a range around 1E-37 to 1E+37 with at least 6 decimal digits, and double precision a range around 1E-307 to 1E+308 with at least 15 decimal digits.

Type What it stores Limit stated in the manual
smallint Signed whole number 2 bytes, -32768 to 32767
integer Signed whole number 4 bytes, -2147483648 to 2147483647
bigint Signed whole number 8 bytes, -9223372036854775808 to 9223372036854775807
numeric Exact decimal Up to 131072 digits before the point and 16383 after
real Inexact float 4 bytes, at least 6 decimal digits
double precision Inexact float 8 bytes, at least 15 decimal digits
varchar(n) Variable text n characters, and n cannot exceed 10485760
text Variable text No declared n. Longest character string is about 1 GB
timestamptz Date and time with zone conversion 8 bytes, 4713 BC to 294276 AD, 1 microsecond
jsonb Decomposed JSON Rejects numbers outside numeric, and rejects NaN and infinity

Text

varchar, char, and text store characters from the database encoding. The manual measures declared length in characters, not bytes.

The character types page says varchar(n) and char(n) hold up to n characters. Storing a longer string is an error, unless the extra characters are spaces, in which case the value is truncated.

An explicit cast to varchar(n) or char(n) truncates without an error. char values are padded with spaces, and varchar stores the shorter string as it is. n must be greater than zero and cannot exceed 10,485,760. Without a length, varchar accepts any length.

text is PostgreSQL’s native string type. Most built-in string functions take or return text. The type table calls it variable unlimited length, and the same page says the longest character string that can be stored is about 1 GB.

The character with code zero cannot be stored. A word counter can count a draft you plan to insert, while the column limit is still characters and bytes, not words.

The page says there is no performance win for char(n) in PostgreSQL. The blank-padded type uses more space, and length checks cost a few CPU cycles. In most cases it recommends text or varchar.

Time

Date and time types cover a calendar day, a clock time, a timestamp, and an interval. timestamptz is the zoned timestamp.

The date and time page says dates use the Gregorian calendar even for years before that calendar existed. timestamp with no qualifier means timestamp without time zone, which is what the SQL standard requires.

timestamptz is the extension that means timestamp with time zone. Both are 8 bytes, from 4713 BC to 294276 AD, with 1 microsecond resolution. date is 4 bytes, from 4713 BC to 5874897 AD, with a resolution of 1 day.

time, timestamp, and interval take a precision p from 0 to 6, which is the number of fractional seconds kept. interval can also restrict the fields, such as DAY TO SECOND, and if you set both fields and precision, the fields must include SECOND.

The page says time with time zone is in the SQL standard and is of questionable usefulness. A mix of date, time, timestamp, and timestamptz covers what most applications need. Input belongs in single quotes, and 1999-01-08 is the ISO 8601 date the page recommends.

JSON

json stores the text you inserted. jsonb stores a decomposed binary value that is faster to process and can be indexed.

The JSON types page says both types reject input that is not valid JSON, which is the advantage over storing the same bytes in text. json keeps insignificant whitespace, key order, and duplicate keys, and processing functions treat the last duplicate as the one that counts.

jsonb drops whitespace, does not keep key order, and keeps only the last value for a duplicate key. Input is a bit slower because of the conversion, and later processing is faster because the text is not parsed again. The page says most applications should use jsonb unless they depend on key order.

json.org describes a JSON object as an unordered set of name and value pairs, and an array as an ordered list. That lines up with jsonb dropping key order.

ECMA-404, 2nd edition, December 2017, defines JSON as a lightweight text syntax and says it does not define how a language stores the text. PostgreSQL’s mapping is one such choice: jsonb numbers become numeric, and NaN and infinity are disallowed.

MDN’s JSON page also says NaN and Infinity are not JSON numbers. jsonb rejects numbers outside the numeric range. The json type does not.

How to choose

Match the type to the value you must not lose, then to the width you are willing to store.

  • Use integer until the documented range is too small, then bigint. Use smallint only when the page’s space note applies.
  • Use numeric for money and other exact quantities. Use real or double precision only when an approximation is acceptable.
  • Use text or varchar for strings. Add (n) when you want a character cap under 10,485,760. Prefer those two over char(n).
  • Use timestamptz when the value is a moment that should convert with a time zone. Use bare timestamp when you want no conversion, and know that is what the bare word means.
  • Use jsonb for JSON you will query. Use json when you must keep the original text, including key order.

Common mistakes

The mistakes are a float for money, a padded char, and json where you meant to index the value.

  • Storing currency in real or double precision. The numeric page says to use numeric when the value must be exact.
  • Assuming varchar without a length is capped at a small default. With no n, it accepts any length, up to the about 1 GB character-string ceiling.
  • Treating timestamp as zoned. The manual says the bare name is without time zone.
  • Expecting jsonb to echo your spacing and key order. It will not. json will.
  • Putting NaN in a JSON number. jsonb disallows it, and MDN says it is not a JSON number in the first place.
  • Assuming serial has no gaps. A rolled-back insert still consumes a sequence value.

Frequently asked questions

What range can numeric declare?

The numeric page says precision in a declaration must be positive, and the maximum you can write is 1000. Scale may be any value from -1000 to 1000, which is wider than the SQL standard range of 0 through precision, so values outside that standard range may not move to another database. An unconstrained numeric uses the table limits: up to 131072 digits before the decimal point and up to 16383 after it. Infinity fits only in an unconstrained column.

Why does serial skip numbers?

smallserial, serial, and bigserial are not true types. The numeric page says serial creates an integer column with a sequence default and a NOT NULL constraint. A sequence value is used up even when the insert rolls back, so gaps can appear even if no row was deleted. bigserial creates a bigint column, and the page says to use it if you may need more than 2^31 identifiers. A unique or primary key constraint is not added for you.

Does timestamp mean timestamptz?

No. The date and time page says the SQL standard requires the bare word timestamp to mean timestamp without time zone, and PostgreSQL follows that. timestamptz is a PostgreSQL extension and an abbreviation of timestamp with time zone. Both store 8 bytes, run from 4713 BC to 294276 AD, and resolve to 1 microsecond. An optional precision from 0 to 6 sets fractional digits in the seconds. The page calls time with time zone questionable for most applications.

Which JSON values does jsonb reject?

The JSON types page maps jsonb numbers onto numeric and disallows NaN and infinity. It also rejects the Unicode escape for a zero character, and it rejects escapes for characters the database encoding cannot store. Duplicate keys collapse to the last value. The json type stores the original text, keeps duplicate keys, and checks those escapes mainly for four hex digits. That looser input check is why the manual still points most applications at jsonb.

Is there a speed difference between text and varchar?

The character types page says there is no performance difference among text, varchar, and char, aside from extra space for blank-padded char and a few CPU cycles to check a length limit. It says char(n) is usually the slowest of the three because of that padding. Trailing spaces are significant in varchar and text, and insignificant when comparing char values. A short string, up to 126 bytes, stores with 1 byte of overhead. Longer strings use 4 bytes and may be compressed.

Sources