Sub topic 1
How PostgreSQL types and conversions fit together
Prerequisites
This chapter assumes you can do the following:
- Write basic SQL:
SELECT,WHERE,INSERTandCREATE TABLE. - Recognize common PostgreSQL types such as
integer,numeric,text,boolean,dateandtimestamp. - Run statements in
psqlor another PostgreSQL client.
Examples were written for current PostgreSQL releases. Where a feature needs a newer version, the later chapter that covers it says so.
Why types matter for conversion
PostgreSQL is strongly typed. Every expression has exactly one type, and operators and functions are defined for specific types. The expression 1 + 2 works because an operator integer + integer exists. An expression like 1 + 'a'::text fails because no operator accepts an integer and a text value.
To combine values of different types, PostgreSQL must convert at least one of them. It does this in two ways:
- You ask for it with a cast, such as
CAST(x AS integer)orx::integer. - The server does it for you when the context allows. It does so for operator arguments, function arguments and values stored into columns.
The rest of this tutorial covers the rules for both. This chapter gives you the vocabulary and a map.
Looking at types
Use pg_typeof to ask an expression for its type:
SELECT pg_typeof(1) AS int_literal,
pg_typeof(1.5) AS decimal_literal,
pg_typeof('a'::text) AS text_value,
pg_typeof(DATE '2026-11-27') AS date_value,
pg_typeof(now()) AS now_type,
pg_typeof(ARRAY[1, 2]) AS array_value;The result is integer, numeric, text, date, timestamp with time zone and integer[]. A literal such as 1.5 is numeric, not a floating-point type. Chapter 3 explains how literals get their types.
PostgreSQL stores type definitions in the pg_type catalog. A few columns are useful when you reason about conversions:
SELECT typname, typcategory, typlen
FROM pg_type
WHERE typname IN ('int4', 'numeric', 'text', 'bool', 'date', 'timestamptz')
ORDER BY typname;typnameis the internal name. For example,int4is the internal name ofinteger, andtimestamptzistimestamp with time zone.typcategoryis a one-letter group:Nfor numeric,Sfor string,Dfor date/time andBfor boolean. Type resolution uses categories to choose between candidates.typlenis the storage size in bytes.-1means variable length.
Kinds of types
Conversions behave differently depending on the kind of type involved.
| Kind | Example | Notes for conversion |
|---|---|---|
| Base type | `integer`, `text`, `date` | Built in or defined by an extension. Each has text input and output functions. |
| Array | `integer[]` | Exists automatically for each base type. Converts element by element. |
| Composite | A row type from `CREATE TABLE` | Casts between composites are limited. |
| Enum | `CREATE TYPE mood AS ENUM (...)` | Converts to and from text through its labels. |
| Domain | `CREATE DOMAIN` | A base type plus constraints. Conversion to the base type is free. |
| Range | `int4range`, `daterange` | Built from a subtype, with its own text format. |
| Pseudo-type | `anyelement`, `unknown` | Used in function signatures and resolution. You cannot store a column of these. |
The unknown pseudo-type matters most for this tutorial. An untyped string literal such as '42' starts out as unknown, and PostgreSQL decides its real type from context.
Three ways a value changes type
Every conversion falls into one of three groups. Later chapters expand each one.
- Explicit casts. You write
CAST(... AS ...),::typeor a typed literal likeDATE '2026-11-27'. Almost any registered conversion is allowed. - Implicit and assignment casts. The server converts without being asked. In expressions, only "safe" conversions happen automatically, mainly widening ones such as
integertobigintornumeric. When storing a value into a table column, a wider set of conversions is allowed. These are the assignment casts. - Conversion through text. Every type can be turned into a string by its output function and parsed from a string by its input function. A cast from text to a type, such as
'42'::integer, runs the target type's input function. This is why'3.7'::integerfails, because the integer parser does not accept a decimal point.
The first two groups are recorded in the pg_cast catalog. The third covers conversions that have no pg_cast entry. The rules for each context are in chapter 6, and pg_cast is covered in chapter 11.
A first look at implicit and assignment conversion
SELECT 1 + 1.5 AS sum_value, pg_typeof(1 + 1.5) AS sum_type;This returns 2.5 with type numeric. No operator adds an integer and a numeric directly. PostgreSQL finds that integer converts implicitly to numeric and uses numeric + numeric.
Now see assignment conversion:
CREATE TABLE measurements (
id bigint,
value integer,
label text
);
INSERT INTO measurements VALUES (1, 2.6, 42);
SELECT * FROM measurements;The row stored is 1, 3, 42. Three conversions happened without a cast:
- The integer
1went into abigintcolumn. - The
numericvalue2.6went into anintegercolumn and was rounded to3. - The integer
42went into atextcolumn and became the string'42'.
Converting numeric to integer loses information. That is allowed only in assignment context, so SELECT 1 + 2.6 never narrows anything. It also explains why a value silently changes when it is stored.
Common failures at this stage
Two error messages cover most early problems, and they point to different causes:
operator does not exist: integer + textorcannot cast type X to Ymeans no conversion is registered for the context you are in. Add an explicit cast, or fix the types.invalid input syntax for type integer: "abc"means a conversion exists, but this particular value cannot be parsed. The problem is the data. Chapter 10 covers handling it.
Separating these two causes saves time. A missing conversion is a problem in your query. A bad value is a problem in your data.
How the rest of the tutorial is organized
The chapters follow the three groups above:
- Chapters 2 to 5 cover cast syntax, literals, numeric conversions and text conversions.
- Chapters 6 and 7 explain the cast contexts and how PostgreSQL picks operator and function argument types.
- Chapters 8 to 10 cover formatting functions, dates and time zones, and invalid input.
- Chapters 11 to 17 cover
pg_cast,UNIONandCASEresult types, JSONB and array conversions, index usage, enums and domains, andCREATE CAST.
Exercise
A table has a column qty integer. Without writing any cast, INSERT INTO t (qty) VALUES (2.4) succeeds, but SELECT 2.4 + 1 returns a numeric, not an integer. Explain why the first statement narrows the value while the second does not.
Show answer
Storing a value into a column uses assignment casts, and numeric to integer is an assignment cast, so 2.4 is rounded to 2 and stored. In an expression, only implicit casts are used, and PostgreSQL allows integer to numeric implicitly but not numeric to integer. So 2.4 + 1 is resolved as numeric + numeric and the result stays numeric (3.4).
Exercise
What is the difference between the errors operator does not exist: integer + text and invalid input syntax for type integer: "abc"? Which one means your data is bad?
Show answer
The first means no operator or conversion is available for those types in that context, so the query itself needs changing, for example with an explicit cast. The second means a conversion to integer was attempted and the input function rejected the value "abc", so the data is bad.