실제 DB 검증PostgreSQL 17.12에서 재현·해결 확인 · 2026-11-30 · 컨테이너 이미지 postgres:17
SQLSTATE 42804(datatype_mismatch)는 컬럼, CASE 결과, UNION 컬럼이 한 자료형이어야 하는데 넣은 값의 자료형이 다르고 자동 형변환이 없을 때 나요. 메시지에 두 자료형이 나와요.
문장 전체가 실패해서 바뀐 행은 없어요. PostgreSQL 17에서 확인한 5가지 원인이에요.
01text 컬럼을 USING 없이 integer로 ALTER
stock_qty는 text예요. ALTER TABLE items ALTER COLUMN stock_qty TYPE integer는 '"stock_qty" 칼럼의 자료형을 integer 형으로 형변환할 수 없음'으로 실패해요. HINT에 해결 문장 USING stock_qty::integer가 나와요.
사전 준비
CREATE TABLE items (id int PRIMARY KEY, stock_qty text);
INSERT INTO items VALUES (1, '10'), (2, '25');CREATE TABLE
INSERT 0 2ALTER TABLE items ALTER COLUMN stock_qty TYPE integer;ERROR: 42804: column "stock_qty" cannot be cast automatically to type integer
HINT: You might need to specify "USING stock_qty::integer".ALTER TABLE items ALTER COLUMN stock_qty TYPE integer USING stock_qty::integer;ALTER TABLEUSING 절을 붙이세요: ALTER TABLE items ALTER COLUMN stock_qty TYPE integer USING stock_qty::integer. 값 10, 25가 그대로 남고 pg_typeof가 integer로 나와요.
SELECT id, stock_qty, pg_typeof(stock_qty) FROM items ORDER BY id; id | stock_qty | pg_typeof
----+-----------+-----------
1 | 10 | integer
2 | 25 | integer
(2 rows)02정수 플래그(1/0)를 boolean 컬럼에 INSERT
예전 테이블은 active_flag를 int(1/0)로, 새 테이블은 is_active를 boolean으로 가져요. INSERT ... SELECT id, active_flag는 'boolean 자료형인데 표현식은 integer 자료형'이라며 실패해요.
사전 준비
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);CREATE TABLE
INSERT 0 2
CREATE TABLEINSERT INTO users (id, is_active) SELECT id, active_flag FROM legacy_users;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.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; table_name | column_name | data_type
--------------+-------------+-----------
legacy_users | active_flag | integer
users | is_active | boolean
(2 rows)information_schema.columns로 integer와 boolean을 확인하고, 숫자를 조건식으로 바꾸세요: SELECT id, active_flag = 1. t, f로 들어가요.
INSERT INTO users (id, is_active) SELECT id, active_flag = 1 FROM legacy_users;
SELECT * FROM users ORDER BY id;INSERT 0 2
id | is_active
----+-----------
1 | t
2 | f
(2 rows)03CASE 분기마다 결과 자료형이 다름
CASE WHEN qty > 0 THEN qty ELSE item END는 한쪽은 integer, 한쪽은 text를 돌려줘요. CASE 결과는 자료형이 하나여야 해서 'CASE 자료형 text 와 integer 는 서로 매치되지 않습니다'가 나요.
사전 준비
CREATE TABLE stock (item text, qty int);
INSERT INTO stock VALUES ('pen', 5), ('cup', 0);CREATE TABLE
INSERT 0 2SELECT item, CASE WHEN qty > 0 THEN qty ELSE item END AS status FROM stock;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...
^SELECT pg_typeof(qty) AS qty_type, pg_typeof(item) AS item_type FROM stock LIMIT 1; qty_type | item_type
----------+-----------
integer | text
(1 row)pg_typeof로 integer와 text를 확인하고, 모든 분기를 text로 맞추세요: THEN qty::text ELSE 'sold out'. 5와 sold out이 나와요.
SELECT item, CASE WHEN qty > 0 THEN qty::text ELSE 'sold out' END AS status FROM stock ORDER BY item; item | status
------+----------
cup | sold out
pen | 5
(2 rows)04UNION 같은 위치 컬럼의 자료형이 다름
첫 SELECT는 customers.id(integer), 둘째 SELECT는 같은 위치에 suppliers.code(text)를 내요. UNION ALL이 'UNION 자료형 integer 와 text 는 서로 매치되지 않습니다'로 실패해요.
사전 준비
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');CREATE TABLE
CREATE TABLE
INSERT 0 1
INSERT 0 1SELECT id, name FROM customers
UNION ALL
SELECT code, name FROM suppliers;ERROR: 42804: UNION types integer and text cannot be matched
LINE 3: SELECT code, name FROM suppliers;
^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; table_name | column_name | data_type
------------+-------------+-----------
customers | id | integer
suppliers | code | text
(2 rows)information_schema.columns로 integer와 text를 확인하고 한쪽을 바꾸세요: SELECT id::text AS id, .... 두 행(1, S-01)이 모두 나와요.
SELECT id::text AS id, name FROM customers
UNION ALL
SELECT code, name FROM suppliers; id | name
------+------
1 | Kim
S-01 | Acme
(2 rows)05WITH RECURSIVE에서 varchar(n) 컬럼을 이어 붙임
경로 컬럼을 비재귀 부분에서 name(varchar(20))으로 시작하고, 재귀 부분에서 tree.path || ' > ' || d.name으로 이어 붙였어요. '"tree" 재귀 쿼리의 2 번째 칼럼은 비재귀 조건에 character varying(20) 자료형을 포함하는데 전체적으로는 character varying 자료형임'으로 실패했고, HINT는 비재귀 부분을 형변환하라고 해요.
name::text AS path로 바꾸니 HQ, HQ > IT, HQ > IT > Infra가 나왔어요.
사전 준비
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');CREATE TABLE
INSERT 0 3WITH 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: 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.SELECT pg_typeof(name) AS name_type, pg_typeof(name || ' > ') AS concat_type FROM dept LIMIT 1; name_type | concat_type
-------------------+-------------
character varying | text
(1 row)pg_typeof로 보면 name || ' > '는 원래 컬럼 자료형이 아니라 text예요. 그래서 두 부분의 자료형이 달라요.
SELECT column_name, data_type, character_maximum_length FROM information_schema.columns WHERE table_name = 'dept' AND column_name = 'name'; column_name | data_type | character_maximum_length
-------------+-------------------+--------------------------
name | character varying | 20
(1 row)첫 번째(비재귀) SELECT의 컬럼을 재귀 부분이 만드는 자료형, 보통 ::text로 맞춰요.
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; id | path
----+-----------------
1 | HQ
2 | HQ > IT
3 | HQ > IT > Infra
(3 rows)자주 묻는 질문
cannot be cast automatically to type integer 해결 방법은?
ALTER COLUMN TYPE에 USING 컬럼::integer를 붙이세요. 에러의 HINT에 그대로 쓸 문장이 나와요.
PostgreSQL boolean 컬럼에 0/1을 넣으려면?
flag = 1처럼 조건식으로 바꿔 넣으세요. 실행해 보니 int 컬럼은 바로 안 들어가고 active_flag = 1은 성공했어요.
CASE types text and integer cannot be matched 뜻은?
THEN과 ELSE의 결과 자료형이 달라요. qty::text처럼 한 자료형으로 맞추세요.
UNION types integer and text cannot be matched 뜻은?
각 SELECT의 같은 위치 컬럼은 자료형이 맞아야 해요. id::text처럼 한쪽을 형변환하세요.
recursive query column has type ... in non-recursive term but type ... overall 해결은?
비재귀 부분의 컬럼을 name::text AS path처럼 형변환해요. 실습에서 경로 쿼리가 이렇게 해서 실행됐어요.
검증 방법
시나리오마다 새 DB 사용자를 만들어 시작해요. 스크립트가 사전 준비 → 에러 재현(에러 코드 일치 확인) → 해결 → 원래 문장 재실행 순서로 실행하고, 화면의 출력은 DB가 실제로 낸 결과를 스크립트가 그대로 저장한 거예요.
lab_a, lab_b는 테스트용 일반 역할(같은 이름의 스키마 소유), postgres는 슈퍼유저예요. PostgreSQL 17.12