ERROR: invalid input syntax for type integer: "..." means PostgreSQL was given a text value to store or compare as an integer, and the text is not a valid integer. The value in quotes at the end of the message is the culprit. Very often it is "": an empty string from a CSV file, a web form or an ORM that sends '' instead of NULL.
=> INSERT INTO orders (customer_id, qty) VALUES ('42', '');
ERROR: invalid input syntax for type integer: ""
LINE 1: INSERT INTO orders (customer_id, qty) VALUES ('42', '');
^
The SQLSTATE is 22P02, invalid_text_representation in the error codes table. The same message appears for other types: bigint, smallint, numeric, uuid, boolean, json and so on. Older versions, up to PostgreSQL 11, worded it slightly differently, as invalid input syntax for integer, so search your logs for both.
What the integer parser accepts
The integer input function in numutils.c is strict. It skips leading and trailing whitespace, accepts an optional sign and requires at least one digit. Anything else left over is a syntax error. Since PostgreSQL 16 it also accepts underscores between digits and 0x, 0o and 0b prefixes, matching the PostgreSQL 16 change that allowed them in numeric constants.
SELECT ' 42 '::integer; -- 42: leading and trailing spaces are allowed
SELECT ''::integer; -- ERROR: invalid input syntax ... ""
SELECT '1.5'::integer; -- ERROR: no decimal point in integer input
SELECT '1e3'::integer; -- ERROR: no exponent either
SELECT 'NULL'::integer; -- ERROR: the word NULL is just text
SELECT '1_000'::integer; -- 1000 on PostgreSQL 16+, error before
SELECT '0x1F'::integer; -- 31 on PostgreSQL 16+, error before
SELECT '3000000000'::integer; -- different error: out of range (22003)
Note the last line. A value that is a valid number but too big for the type raises a different error with a different SQLSTATE: value "..." is out of range for type integer, 22003. That problem has its own fix, which we cover in our article on "integer out of range".
The usual causes
- Empty strings. An empty input field or an empty CSV column, sent as
''. PostgreSQL does not treat an empty string as NULL for numbers. - Decimals and formatting.
'1.5','1,000','10 pcs'or'1e3'exported from a spreadsheet. - The word NULL. An export tool wrote the four letters
NULLinto the file. To the integer parser that is just text. - Comparing a text column with a number.
WHERE text_col = 42does not work at all (that is an "operator does not exist" error), so people writetext_col::integer = 42, and the cast then fails on the first row that does not hold a number.
How to fix it
- Decide what an empty value means. If it means "no value", convert it to NULL on the way in with
NULLIF(value, ''). If it means zero, say so explicitly withCOALESCE. Do not leave it to chance.-- empty string means "no value": turn it into NULL INSERT INTO orders (customer_id, qty) VALUES ($1::integer, NULLIF($2, '')::integer); -- a decimal string that should be an integer: go through numeric SELECT round('1.5'::numeric)::integer; -- 2 - Fix it in the application. Bind integers as integers, and send NULL rather than an empty string for missing values. That removes the problem at the source.
- Find the bad rows instead of guessing. PostgreSQL 16 added pg_input_is_valid() and pg_input_error_info(). They test a string against a type without raising an error, and the second one returns the message and SQLSTATE the cast would have raised.
On older versions, a regular expression does the job for plain integers:-- PostgreSQL 16+: find the bad rows without failing SELECT id, raw_qty FROM staging_orders WHERE NOT pg_input_is_valid(raw_qty, 'integer') LIMIT 20; -- and see exactly what error each value would raise SELECT raw_qty, e.message, e.sql_error_code FROM staging_orders, LATERAL pg_input_error_info(raw_qty, 'integer') AS e WHERE e.sql_error_code IS NOT NULL LIMIT 20;-- before PostgreSQL 16: a regular expression check SELECT id, raw_qty FROM staging_orders WHERE raw_qty !~ '^\s*[+-]?[0-9]+\s*$';
Bulk loads with COPY
Most of these errors appear during imports, and by default one bad value aborts the whole COPY. The COPY documentation gives you several tools:
- NULL and FORCE_NULL. In CSV format, an unquoted empty string is already NULL by default. A quoted empty string
""is not;FORCE_NULLon that column turns it into NULL as well. In text format the default NULL marker is\N. - ON_ERROR ignore (PostgreSQL 17). Rows whose values fail to convert are skipped instead of aborting the load. It works only for
COPY FROMin text or CSV format. A NOTICE at the end reports how many rows were skipped, andLOG_VERBOSITY verboseadds one NOTICE per skipped row with the line number and column. - REJECT_LIMIT (PostgreSQL 18). Puts a ceiling on how many rows may be skipped. Past that,
COPYfails anyway. Without it,ON_ERROR ignoresilently skips any number of bad rows. - ON_ERROR set_null (PostgreSQL 19). Instead of dropping the whole row, replaces only the field that failed to convert with NULL and keeps the rest of the row.
-- CSV: quoted empty strings become NULL in these columns
COPY orders (customer_id, qty, note)
FROM '/data/orders.csv'
WITH (FORMAT csv, HEADER true, FORCE_NULL (qty));
-- PostgreSQL 17+: skip rows that do not convert, and report them
-- (REJECT_LIMIT needs PostgreSQL 18)
COPY orders FROM '/data/orders.csv'
WITH (FORMAT csv, HEADER true,
ON_ERROR ignore, REJECT_LIMIT 100, LOG_VERBOSITY verbose);
Skipping is not cleaning. ON_ERROR ignore keeps the load running, but every skipped row is data you no longer have. Always set REJECT_LIMIT on PostgreSQL 18+, log the skipped lines, and compare row counts with the source file.
On large production tables
With a hundred test rows, every value is clean and the error never appears. With a hundred million rows from a real export, one bad value is almost certain. Plan for it:
- Load into a staging table first. Load the raw columns as
text, validate withpg_input_is_valid(), then insert into the real table with explicit casts. In our experience, this is the only approach that tells you exactly which rows are wrong without restarting a long load. - Validate before ALTER COLUMN TYPE. Changing a
textcolumn tointegeron a big table rewrites it under anACCESS EXCLUSIVElock, and one bad value at the end throws all that work away. Run apg_input_is_valid()scan first, and on large tables prefer a new column filled in batches. - A failed COPY still costs disk. A
COPYthat aborts after loading most of a file leaves the rows it already wrote as dead tuples, which take space until vacuum cleans them up. In our experience a few failed retries of a large import can noticeably bloat a table. - Do not cast in WHERE on mixed data.
WHERE text_col::integer = 42fails on the first bad row, can fail or not depending on the plan, and cannot use a plain index on the column. Clean the data or store it in the right type. - Monitor 22P02. With
%einlog_line_prefixyou can count these errors in the logs. A sudden spike usually means an upstream export or a form changed its format.
Key takeaways
- 22P02 means the text is not a valid integer; the quoted value in the message shows which one.
- Empty strings are the most common cause: use
NULLIF(x, '')or fix the application to send NULL. - PostgreSQL 16+ has
pg_input_is_valid()andpg_input_error_info()to find bad values without errors. - COPY has
FORCE_NULL,ON_ERROR ignore(17),REJECT_LIMIT(18) andON_ERROR set_null(19). - For big loads, use a text staging table and validate before converting.