PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL 42804: datatype mismatch

Two values that must share one type have types PostgreSQL will not convert automatically.

PostgreSQL 17.12
42804: column "stock_qty" cannot be cast automatically to type integer

Jump to a cause

  1. 01ALTER COLUMN TYPE from text to integer without USING
  2. 02Inserting an integer flag into a boolean column
  3. 03CASE branches return different types
  4. 04UNION columns have different types
  5. 05WITH RECURSIVE: varchar(n) column grows by concatenation

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17

SQLSTATE 42804 (datatype_mismatch) appears when a column, a CASE result or a UNION column needs one type, but the values you give have different types and there is no automatic conversion. The message names both types.

The statement fails before any row is changed. Below are five causes verified on PostgreSQL 17.

01ALTER COLUMN TYPE from text to integer without USING

stock_qty is text. ALTER TABLE items ALTER COLUMN stock_qty TYPE integer fails with cannot be cast automatically to type integer. The HINT already shows the fix: USING stock_qty::integer.

Setup
Setuprun as lab_a
CREATE TABLE items (id int PRIMARY KEY, stock_qty text);
INSERT INTO items VALUES (1, '10'), (2, '25');
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
ALTER TABLE items ALTER COLUMN stock_qty TYPE integer;
✕ ERROR
ERROR:  42804: column "stock_qty" cannot be cast automatically to type integer
HINT:  You might need to specify "USING stock_qty::integer".
Fixrun as lab_a
ALTER TABLE items ALTER COLUMN stock_qty TYPE integer USING stock_qty::integer;
OUTPUT
ALTER TABLE

Add the USING clause: ALTER TABLE items ALTER COLUMN stock_qty TYPE integer USING stock_qty::integer. The values 10 and 25 are kept and pg_typeof shows integer.

After the fixrun as lab_a
SELECT id, stock_qty, pg_typeof(stock_qty) FROM items ORDER BY id;
✓ SUCCESS
 id | stock_qty | pg_typeof
----+-----------+-----------
  1 |        10 | integer
  2 |        25 | integer
(2 rows)

02Inserting an integer flag into a boolean column

The old table stores active_flag as int (1/0) and the new is_active is boolean. INSERT ... SELECT id, active_flag fails: column "is_active" is of type boolean but expression is of type integer.

Setup
Setuprun as lab_a
CREATE TABLE legacy_users (id int, active_flag int);
INSERT INTO legacy_users VALUES (1, 1), (2, 0);
CREATE TABLE users (id int PRIMARY KEY, is_active boolean);
OUTPUT
CREATE TABLE
INSERT 0 2
CREATE TABLE
Reproducerun as lab_a
INSERT INTO users (id, is_active) SELECT id, active_flag FROM legacy_users;
✕ ERROR
ERROR:  42804: column "is_active" is of type boolean but expression is of type integer
LINE 1: INSERT INTO users (id, is_active) SELECT id, active_flag FRO...
                                                     ^
HINT:  You will need to rewrite or cast the expression.
Checkrun as lab_a
SELECT table_name, column_name, data_type FROM information_schema.columns WHERE table_schema = 'lab_a' AND column_name IN ('active_flag', 'is_active') ORDER BY table_name;
OUTPUT
  table_name  | column_name | data_type
--------------+-------------+-----------
 legacy_users | active_flag | integer
 users        | is_active   | boolean
(2 rows)

information_schema.columns shows integer vs boolean. Turn the number into a condition: SELECT id, active_flag = 1. The rows are inserted as t and f.

After the fixrun as lab_a
INSERT INTO users (id, is_active) SELECT id, active_flag = 1 FROM legacy_users;
SELECT * FROM users ORDER BY id;
✓ SUCCESS
INSERT 0 2
 id | is_active
----+-----------
  1 | t
  2 | f
(2 rows)

03CASE branches return different types

CASE WHEN qty > 0 THEN qty ELSE item END returns integer in one branch and text in the other. A CASE must have one result type, so it fails with CASE types text and integer cannot be matched.

Setup
Setuprun as lab_a
CREATE TABLE stock (item text, qty int);
INSERT INTO stock VALUES ('pen', 5), ('cup', 0);
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
SELECT item, CASE WHEN qty > 0 THEN qty ELSE item END AS status FROM stock;
✕ ERROR
ERROR:  42804: CASE types text and integer cannot be matched
LINE 1: SELECT item, CASE WHEN qty > 0 THEN qty ELSE item END AS sta...
                                            ^
Checkrun as lab_a
SELECT pg_typeof(qty) AS qty_type, pg_typeof(item) AS item_type FROM stock LIMIT 1;
OUTPUT
 qty_type | item_type
----------+-----------
 integer  | text
(1 row)

pg_typeof shows integer and text. Make every branch text: THEN qty::text ELSE 'sold out'. The query returns 5 and sold out.

After the fixrun as lab_a
SELECT item, CASE WHEN qty > 0 THEN qty::text ELSE 'sold out' END AS status FROM stock ORDER BY item;
✓ SUCCESS
 item |  status
------+----------
 cup  | sold out
 pen  | 5
(2 rows)

04UNION columns have different types

The first SELECT returns customers.id (integer) and the second returns suppliers.code (text) in the same position. UNION ALL fails with UNION types integer and text cannot be matched.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int, name text);
CREATE TABLE suppliers (code text, name text);
INSERT INTO customers VALUES (1, 'Kim');
INSERT INTO suppliers VALUES ('S-01', 'Acme');
OUTPUT
CREATE TABLE
CREATE TABLE
INSERT 0 1
INSERT 0 1
Reproducerun as lab_a
SELECT id, name FROM customers
UNION ALL
SELECT code, name FROM suppliers;
✕ ERROR
ERROR:  42804: UNION types integer and text cannot be matched
LINE 3: SELECT code, name FROM suppliers;
               ^
Checkrun as lab_a
SELECT table_name, column_name, data_type FROM information_schema.columns WHERE table_schema = 'lab_a' AND column_name IN ('id', 'code') ORDER BY table_name;
OUTPUT
 table_name | column_name | data_type
------------+-------------+-----------
 customers  | id          | integer
 suppliers  | code        | text
(2 rows)

information_schema.columns shows integer and text. Cast the first one: SELECT id::text AS id, .... Both rows (1 and S-01) come back.

After the fixrun as lab_a
SELECT id::text AS id, name FROM customers
UNION ALL
SELECT code, name FROM suppliers;
✓ SUCCESS
  id  | name
------+------
 1    | Kim
 S-01 | Acme
(2 rows)

05WITH RECURSIVE: varchar(n) column grows by concatenation

A path column started as name (varchar(20)) in the non-recursive term, and the recursive term built tree.path || ' > ' || d.name. The error said column 2 has type character varying(20) in the non-recursive term but character varying overall. The HINT says to cast the non-recursive term.

With name::text AS path the query returned HQ, HQ > IT, HQ > IT > Infra.

Setup
Setuprun as lab_a
CREATE TABLE dept (id int PRIMARY KEY, parent_id int, name varchar(20));
INSERT INTO dept VALUES (1, NULL, 'HQ'), (2, 1, 'IT'), (3, 2, 'Infra');
OUTPUT
CREATE TABLE
INSERT 0 3
Reproducerun as lab_a
WITH RECURSIVE tree AS (
  SELECT id, name AS path FROM dept WHERE parent_id IS NULL
  UNION ALL
  SELECT d.id, tree.path || ' > ' || d.name
  FROM dept d JOIN tree ON d.parent_id = tree.id
)
SELECT * FROM tree;
✕ ERROR
ERROR:  42804: recursive query "tree" column 2 has type character varying(20) in non-recursive term but type character varying overall
LINE 2:   SELECT id, name AS path FROM dept WHERE parent_id IS NULL
                     ^
HINT:  Cast the output of the non-recursive term to the correct type.
Check the causerun as lab_a
SELECT pg_typeof(name) AS name_type, pg_typeof(name || ' > ') AS concat_type FROM dept LIMIT 1;
OUTPUT
     name_type     | concat_type
-------------------+-------------
 character varying | text
(1 row)

pg_typeof shows that name || ' > ' is text, not the original column type, so the two terms disagree.

Checkrun as lab_a
SELECT column_name, data_type, character_maximum_length FROM information_schema.columns WHERE table_name = 'dept' AND column_name = 'name';
OUTPUT
 column_name |     data_type     | character_maximum_length
-------------+-------------------+--------------------------
 name        | character varying |                       20
(1 row)

Cast the column in the first (non-recursive) SELECT to the type the recursive part produces, usually ::text.

After the fixrun as lab_a
WITH RECURSIVE tree AS (
  SELECT id, name::text AS path FROM dept WHERE parent_id IS NULL
  UNION ALL
  SELECT d.id, tree.path || ' > ' || d.name
  FROM dept d JOIN tree ON d.parent_id = tree.id
)
SELECT * FROM tree ORDER BY id;
✓ SUCCESS
 id |      path
----+-----------------
  1 | HQ
  2 | HQ > IT
  3 | HQ > IT > Infra
(3 rows)

FAQ

How do I fix column cannot be cast automatically to type integer?

Add a USING clause to ALTER COLUMN TYPE, for example USING col::integer. The HINT in the error shows the exact text.

How do I insert 0/1 into a boolean column in PostgreSQL?

Turn it into a condition such as flag = 1. In our test an int column could not go into boolean directly, but active_flag = 1 worked.

What does CASE types text and integer cannot be matched mean?

The THEN and ELSE parts return different types. Cast them to one type, for example qty::text.

What does UNION types integer and text cannot be matched mean?

Columns in the same position of each SELECT must have a compatible type. Cast one side, for example id::text.

How do I fix recursive query column has type character varying(n) in non-recursive term but type character varying overall?

Cast the column in the non-recursive term, e.g. name::text AS path. In the lab that made the path query run.

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