Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-12-01 · container image postgres:17
PostgreSQL converts a quoted value to the type it needs: the column type on INSERT, the column type in a comparison, or the type in a :: cast. 22P02 means that text is not a valid value of that type. The message shows both the type and the exact value, including an empty one ("").
Unlike MySQL, PostgreSQL never turns a bad value into 0 or NULL silently. Fix the value, or convert it explicitly before the cast.
01An empty string sent for an integer column
A form or CSV field left blank arrives as '', and '' is not an integer. The caret under LINE 1 points at the empty value. This is common when an application passes every input as a string.
Setup
CREATE TABLE stock (item text PRIMARY KEY, qty integer);CREATE TABLEINSERT INTO stock (item, qty) VALUES ('pen', '');ERROR: 22P02: invalid input syntax for type integer: ""
LINE 1: INSERT INTO stock (item, qty) VALUES ('pen', '');
^SELECT '' IS NULL AS empty_is_null, length('') AS len,
pg_input_is_valid('', 'integer') AS valid_integer; empty_is_null | len | valid_integer
---------------+-----+---------------
f | 0 | f
(1 row)'' IS NULL is f and its length is 0: in PostgreSQL an empty string is not NULL. pg_input_is_valid('', 'integer') is also f.
Turn the empty string into NULL before the cast: NULLIF(value, '')::integer. In application code, pass NULL for an empty field instead of ''. The row was stored with qty NULL.
INSERT INTO stock (item, qty) VALUES ('pen', NULLIF('', '')::integer);
SELECT * FROM stock;INSERT 0 1
item | qty
------+-----
pen |
(1 row)02A non-UUID value compared with a uuid column
The column is uuid, so the literal in WHERE id = 'undefined' must be a UUID. Values like undefined, null or an empty string usually come from a front end that sent a missing id.
Setup
CREATE TABLE sessions (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), user_name text);
INSERT INTO sessions (id, user_name) VALUES ('6f1c2a9e-3b4d-4e5f-8a7b-1c2d3e4f5a6b', 'kim');CREATE TABLE
INSERT 0 1SELECT * FROM sessions WHERE id = 'undefined';ERROR: 22P02: invalid input syntax for type uuid: "undefined"
LINE 1: SELECT * FROM sessions WHERE id = 'undefined';
^SELECT pg_input_is_valid('undefined', 'uuid') AS valid_uuid,
pg_input_is_valid('6f1c2a9e-3b4d-4e5f-8a7b-1c2d3e4f5a6b', 'uuid') AS real_id; valid_uuid | real_id
------------+---------
f | t
(1 row)pg_input_is_valid (PostgreSQL 16+) tests a value without raising the error: undefined gives f, the real id gives t.
Validate the id before running the query, and skip the query when it is missing. With a real UUID the same SELECT returns the row.
SELECT * FROM sessions WHERE id = '6f1c2a9e-3b4d-4e5f-8a7b-1c2d3e4f5a6b'; id | user_name
--------------------------------------+-----------
6f1c2a9e-3b4d-4e5f-8a7b-1c2d3e4f5a6b | kim
(1 row)03Text data with commas cast to numeric
Imported data is stored as text and cast with amount::numeric. One row holds 1,200, which is not a valid number, so the whole query fails. The message shows the bad value but not the row.
Setup
CREATE TABLE sales_import (region text, amount text);
INSERT INTO sales_import VALUES ('seoul', '900'), ('busan', '1,200'), ('incheon', '300');CREATE TABLE
INSERT 0 3SELECT sum(amount::numeric) FROM sales_import;ERROR: 22P02: invalid input syntax for type numeric: "1,200"SELECT region, amount, pg_input_is_valid(amount, 'numeric') AS valid_numeric
FROM sales_import ORDER BY region; region | amount | valid_numeric
---------+--------+---------------
busan | 1,200 | f
incheon | 300 | t
seoul | 900 | t
(3 rows)Checking every row with pg_input_is_valid(amount, 'numeric') shows only busan (1,200) as f.
Clean the value inside the cast: replace(amount, ',', '')::numeric gives 2400. If the data keeps coming in this form, store it in a numeric column after cleaning it once.
SELECT sum(replace(amount, ',', '')::numeric) FROM sales_import; sum
------
2400
(1 row)04A decimal string like '3.0' cast to integer (CSV import)
Casting text to int accepts only whole-number text. One imported row has 3.0, so the whole INSERT ... SELECT fails with invalid input syntax for type integer: "3.0".
Setup
CREATE TABLE stock (item text PRIMARY KEY, qty int NOT NULL);
CREATE TABLE stock_import (item text, qty text);
INSERT INTO stock_import VALUES ('pen', '10'), ('note', '3.0'), ('clip', '25');CREATE TABLE
CREATE TABLE
INSERT 0 3INSERT INTO stock SELECT item, qty::int FROM stock_import;ERROR: 22P02: invalid input syntax for type integer: "3.0"SELECT item, qty, pg_input_is_valid(qty, 'integer') AS valid_integer,
pg_input_is_valid(qty, 'numeric') AS valid_numeric
FROM stock_import ORDER BY item; item | qty | valid_integer | valid_numeric
------+-----+---------------+---------------
clip | 25 | t | t
note | 3.0 | f | t
pen | 10 | t | t
(3 rows)Only note (3.0) is f for integer, but it is t for numeric. That is why casting through numeric works.
Casting through numeric first (qty::numeric::int) converts 3.0 to 3, and all 3 rows are inserted (INSERT 0 3).
INSERT INTO stock SELECT item, qty::numeric::int FROM stock_import;
SELECT item, qty FROM stock ORDER BY item;INSERT 0 3
item | qty
------+-----
clip | 25
note | 3
pen | 10
(3 rows)05JSON with unquoted keys (JavaScript object style)
{name: "Kim", age: 30} is valid JavaScript but not JSON: keys must be in double quotes. The INSERT into a jsonb column fails with invalid input syntax for type json, and DETAIL says Token "name" is invalid.
With quoted keys the INSERT works and payload->>'name' returns Kim. In an application, build the value with a JSON serializer instead of string concatenation.
Setup
CREATE TABLE events (id int PRIMARY KEY, payload jsonb);CREATE TABLEINSERT INTO events VALUES (1, '{name: "Kim", age: 30}');ERROR: 22P02: invalid input syntax for type json
LINE 1: INSERT INTO events VALUES (1, '{name: "Kim", age: 30}');
^
DETAIL: Token "name" is invalid.
CONTEXT: JSON data, line 1: {name...SELECT pg_input_is_valid('{name: "Kim", age: 30}', 'jsonb') AS unquoted_keys,
pg_input_is_valid('{"name": "Kim", "age": 30}', 'jsonb') AS quoted_keys; unquoted_keys | quoted_keys
---------------+-------------
f | t
(1 row)pg_input_is_valid(..., 'jsonb') is f for the unquoted keys and t for the quoted keys.
INSERT INTO events VALUES (1, '{"name": "Kim", "age": 30}');
SELECT id, payload->>'name' AS name, payload FROM events;INSERT 0 1
id | name | payload
----+------+----------------------------
1 | Kim | {"age": 30, "name": "Kim"}
(1 row)06Passing '1,2,3' to = ANY() (array literal without braces)
ANY() needs an array, so '1,2,3' is read as an array literal and fails with malformed array literal: "1,2,3"; DETAIL says Array value must start with "{". This often happens when an app sends a comma-separated ID list.
string_to_array('1,2,3', ',')::int[] makes {1,2,3}, and the query returns products 1, 2, 3.
Setup
CREATE TABLE products (id int PRIMARY KEY, name text);
INSERT INTO products VALUES (1, 'pen'), (2, 'note'), (3, 'clip'), (4, 'tape');CREATE TABLE
INSERT 0 4SELECT id, name FROM products WHERE id = ANY('1,2,3');ERROR: 22P02: malformed array literal: "1,2,3"
LINE 1: SELECT id, name FROM products WHERE id = ANY('1,2,3');
^
DETAIL: Array value must start with "{" or dimension information.SELECT pg_input_is_valid('1,2,3', 'int[]') AS without_braces,
pg_input_is_valid('{1,2,3}', 'int[]') AS with_braces; without_braces | with_braces
----------------+-------------
f | t
(1 row)As an int[], '1,2,3' is f and '{1,2,3}' is t: an array literal needs the braces.
SELECT id, name FROM products WHERE id = ANY(string_to_array('1,2,3', ',')::int[]) ORDER BY id; id | name
----+------
1 | pen
2 | note
3 | clip
(3 rows)FAQ
How do I fix invalid input syntax for type integer: "" in PostgreSQL?
An empty string is being cast to integer. Use NULLIF(value, '')::integer, or have the application send NULL for empty fields.
What does invalid input syntax for type uuid mean?
The value compared with or inserted into a uuid column is not a UUID, often undefined or an empty string from the front end. Check the id before running the query.
How do I find which row breaks a cast in PostgreSQL?
On PostgreSQL 16 or later, check each row with pg_input_is_valid(column, 'numeric'): the rows that return f are the ones that break the cast.
What is SQLSTATE 22P02?
22P02 is invalid_text_representation: a text value could not be read as the requested data type.
What does invalid input syntax for type json, Token is invalid mean?
The text is not valid JSON, for example keys without double quotes. Check it with pg_input_is_valid('...', 'jsonb') and build JSON with a serializer.
How do I fix malformed array literal in PostgreSQL?
An array literal needs braces like '{1,2,3}'. For a comma-separated string, use string_to_array(value, ',')::int[].
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12