Sub topic 1
How PostgreSQL Types and Conversions Fit Together
PostgreSQL assigns a data type to every value and expression. A type describes what a value means, which operations are valid, and how the value is stored. For example, integer stores whole numbers, date stores calendar dates, and text stores character data.
This chapter assumes you know basic SELECT, WHERE, INSERT, and CREATE TABLE statements. You should also recognize common PostgreSQL types such as integer, numeric, text, boolean, date, and timestamp, and have access to a PostgreSQL client.
Prerequisites
The examples use PostgreSQL syntax. Run them in a test database with psql or another PostgreSQL client. The exact result of some expressions depends on the PostgreSQL version and the session's time zone.
Values, Types, and Expressions
A value can have a type explicitly assigned by a table definition or by an expression. When you insert 42 into an integer column, the column gives the stored value its expected type. When you write an expression such as price * quantity, PostgreSQL must determine the types of both operands and select an appropriate multiplication operator.
A conversion, also called a cast, changes a value from one type to another. Some conversions are safe and automatic. Others require you to request them explicitly because they can lose information or fail.
| Conversion | Typical reason | Possible concern |
|---|---|---|
| `integer` to `numeric` | Preserve or calculate with a general numeric type | Usually safe, but may change operator selection |
| `numeric` to `integer` | Store a whole-number result | Fractional values are rounded |
| `text` to `date` | Interpret input as a date | Invalid text raises an error |
| `timestamp` to `timestamptz` | Interpret a local date and time as an instant | The session time zone affects the result |
SELECT
42 AS untyped_number,
42::numeric AS decimal_number,
'2026-11-27'::date AS calendar_date,
CAST(true AS text) AS text_boolean;The first expression is initially an untyped numeric literal. PostgreSQL can choose a concrete type from context. The other expressions request a particular type explicitly. CAST(value AS type) is standard SQL syntax. PostgreSQL also supports the shorter value::type syntax.
A cast does not merely change display formatting. It invokes PostgreSQL's conversion rules for the source and target types. For instance, converting text to an integer parses the text as an integer. Converting a numeric value to an integer applies numeric conversion rules, which can produce a different result than parsing an integer-shaped string.
In later chapters, you will distinguish automatic conversions from assignment and explicit casts. That distinction matters when PostgreSQL resolves overloaded operators and functions, validates inserted data, or chooses whether a predicate can use an index.
Exercise
What is the main difference between 42::numeric and simply writing 42 in an expression?
Show answer
42::numeric explicitly requests the numeric type. The uncast literal starts without a fixed concrete type and PostgreSQL resolves it from its context.