Home › Databases

ERROR: invalid input syntax for type integer (PostgreSQL) — how to fix

ERROR:  invalid input syntax for type integer: ""
ERROR:  invalid input syntax for type integer: "abc"
LINE 1: SELECT * FROM users WHERE id = '';
                                       ^

Quick fix: Send NULL instead of empty strings (NULLIF(col, '')::int), fix the value in your app (it's often "undefined" or "NaN" from JavaScript), and for CSV imports use COPY ... WITH (FORMAT csv, FORCE_NULL (col)) or clean the column first.

What it means

The value in quotes after the colon is the exact text PostgreSQL tried to turn into an integer (SQLSTATE 22P02, invalid_text_representation). It happens when you insert into, compare against, or cast to an integer/bigint/smallint.

Only optional whitespace, an optional sign and digits are accepted. These all fail:

SELECT ''::int;          -- empty string
SELECT '1.5'::int;       -- decimal
SELECT 'undefined'::int; -- placeholder from app code
SELECT '1,000'::int;     -- thousands separator

The same message exists for other types: type uuid, type numeric, type boolean, type json, type date.

Cause 1: an empty string instead of NULL

Forms and CSVs produce '' for "no value". Empty string is not NULL in PostgreSQL. Convert it:

INSERT INTO orders (customer_id)
VALUES (NULLIF($1, '')::int);

Better: convert in the application before sending (None / null instead of "").

Cause 2: "undefined", "null" or "NaN" from the app

invalid input syntax for type integer: "undefined" means your code stringified a missing JavaScript value, e.g. a route /users/undefined. Validate before querying:

const id = Number.parseInt(req.params.id, 10);
if (!Number.isInteger(id)) return res.status(400).send('bad id');
await pool.query('SELECT * FROM users WHERE id = $1', [id]);

Always pass values as query parameters; don't build SQL with string concatenation.

Cause 3: decimals or formatted numbers

If the column really should hold fractions, change its type:

ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2);

To convert a decimal string to an integer, go through numeric:

SELECT round('1.5'::numeric)::int;   -- 2
SELECT replace('1,000', ',', '')::int;

Cause 4: casting a text column that has bad rows

ALTER COLUMN ... TYPE integer USING col::integer or a WHERE col::int > 5 fails on the first non-numeric row. Find them:

SELECT id, col FROM t WHERE col !~ '^\s*[+-]?\d+\s*$';

Then convert safely:

ALTER TABLE t
  ALTER COLUMN col TYPE integer
  USING CASE WHEN col ~ '^\s*[+-]?\d+\s*$' THEN col::integer END;

Rows that don't match become NULL — review them first.

Cause 5: CSV import with COPY

In FORMAT csv, an unquoted empty field is read as NULL, but a quoted empty field ("") is an empty string and fails. Tell COPY to treat it as NULL for specific columns:

COPY orders (id, customer_id, total)
FROM '/tmp/orders.csv'
WITH (FORMAT csv, HEADER true, FORCE_NULL (customer_id));

From psql without server file access use \copy with the same options. For messy files, load into a staging table with text columns, clean with SQL, then INSERT ... SELECT into the real table. The CSV to JSON tool is handy for eyeballing which rows are off.

Cause 6: comparing an integer column to text

WHERE id = '' or a mistyped column (WHERE id = name) triggers the cast. If the message says operator does not exist: integer = text instead, see operator does not exist.

Last updated 2026-12-05.