Incident Response

PostgreSQL ERROR: invalid input syntax for type integer

PostgreSQL could not turn a piece of text into a number. Usually it is an empty string from a CSV or a form, sometimes a decimal or a stray word. Here is what the integer parser accepts, how to find the bad rows before they break a load, and what COPY can do about it in PostgreSQL 17, 18 and 19.

· 12 min read · AG Data Team

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

How to fix it

  1. 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 with COALESCE. 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
  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.
  3. 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.
    -- 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;
    On older versions, a regular expression does the job for plain integers:
    -- 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:

-- 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:

Key takeaways

PostgreSQL Errors COPY Data quality
Work with us

A big import keeps failing on bad values?

We build robust PostgreSQL load pipelines: staging tables, validation, COPY error handling and safe type changes on large tables.

Get in touch More from the blog