FluentDBFluentDB
PostgreSQL errors

PostgreSQL: integer out of range

ERRORSQLSTATE 22003numeric_value_out_of_range
ERROR: integer out of range

A value exceeded the range of its integer type. The classic case is an int4 primary key running past 2,147,483,647. Migrate the column to bigint, or cast intermediate results before they overflow.

What this error means

Postgres integers have fixed limits: int4 tops out at about 2.1 billion, and smallint much lower. When a value or an arithmetic result exceeds the type's range, you get this error.

The most infamous version is a serial (int4) primary key exhausting its range on a large, long-lived table.

How to fix it

01

A serial primary key ran out of range

Migrate the column to bigint.

sql
ALTER TABLE events ALTER COLUMN id TYPE bigint;
02

Arithmetic overflows mid-expression

Cast to bigint or numeric before the operation overflows.

sql
SELECT sum(count::bigint) FROM t;
03

A value is simply too large for the column

Use a wider type: bigint for large integers, or numeric for exact large values.

How to avoid it next time

  • Choose bigint for identifiers and counters that can realistically grow large.
  • Cast intermediate results to a wider type before sums and products can overflow.

Frequently asked questions

What is the maximum value of a Postgres integer?

int4 (integer) tops out at 2,147,483,647. bigint goes to roughly 9.2 quintillion.

How do I migrate a serial id to bigint?

ALTER TABLE t ALTER COLUMN id TYPE bigint; The associated sequence also needs to be bigint, which bigserial handles for you.

Stop struggling with SQL

Use FluentDB, the AI database client for macOS.