FluentDBFluentDB
PostgreSQL errors

PostgreSQL: invalid input syntax for type integer

ERRORSQLSTATE 22P02invalid_text_representation
ERROR: invalid input syntax for type integer: "<value>"

Postgres tried to read a value as an integer and could not, because the text was not a plain whole number. The usual culprit is an empty string, a null-like string, or stray characters. Convert empty strings to NULL and validate input before it reaches the column.

What this error means

This is a data conversion error. Something sent a string like '' or '12abc' or '1,000' where an integer was expected, and Postgres refuses to guess. The message quotes the exact offending value.

It frequently comes from a form or API where an empty field arrives as an empty string rather than a null, and that empty string is bound to an integer column or parameter.

How to fix it

01

An empty string is being inserted

Turn '' into NULL before it hits the integer column. NULLIF does exactly that.

sql
INSERT INTO events (count)
VALUES (NULLIF($1, '')::int);
02

The value has stray characters

Clean the input in the application before sending it, so only digits (and an optional sign) reach Postgres.

03

The parameter type is wrong in your client

Make sure the driver binds the value as an integer, not as text. A text bind against an integer column forces a conversion that can fail.

How to avoid it next time

  • Normalize empty form fields to null at the edge of your app, before they reach the database.
  • Validate and coerce numeric input in code so the database only ever receives clean values.
Varies by version

Postgres 12 added the word type to these messages. Before 12 it read invalid input syntax for integer: "...".

Frequently asked questions

Why is an empty string not treated as zero or null?

Postgres does not silently coerce '' to 0 or NULL for an integer, because that would hide bugs. You must convert it explicitly, for example with NULLIF(value, '').

Does this apply to uuid and json too?

Yes. The same 22P02 error appears as 'invalid input syntax for type uuid' and 'for type json' when a malformed value is cast to those types.

Stop struggling with SQL

Use FluentDB, the AI database client for macOS.