PostgreSQL 17.12실제 DB 검증 · 2026-11-29

PostgreSQL 42846: integer 자료형을 date 자료형으로 형변환할 수 없습니다.

두 타입 사이에 바로 가는 형변환이 없어서 ::타입으로 바꿀 수 없어요.

PostgreSQL 17.12
42846: integer 자료형을 date 자료형으로 형변환할 수 없습니다.
42846: cannot cast type integer to date

원인 바로가기

  1. 0120240105 같은 yyyymmdd 정수 컬럼을 ::date로 변환
  2. 02ALTER COLUMN ... TYPE int USING 날짜컬럼::int
  3. 03SUM(플래그::numeric)으로 true 개수 세기

실제 DB 검증PostgreSQL 17.12에서 재현·해결 확인 · 2026-11-29 · 컨테이너 이미지 postgres:17

PostgreSQL에서 값::타입은 두 타입 사이에 형변환(cast)이 정의돼 있을 때만 돼요. 정수→날짜, 날짜→정수, boolean→numeric처럼 될 것 같은 조합도 형변환이 없어서 42846 cannot cast type X to Y가 나요. LINE 1 아래 ^ 표시가 형변환 위치를 가리켜요.

형변환이 있는 함수나 중간 타입을 거치면 돼요. 아래 3가지가 실무에서 가장 흔한 경우예요.

0120240105 같은 yyyymmdd 정수 컬럼을 ::date로 변환

sales.sale_ymd에 날짜가 20240105 같은 정수로 들어 있었어요. SELECT sale_ymd::date FROM sales가 cannot cast type integer to date로 실패했어요.

information_schema.columns로 컬럼이 integer인 걸 확인하고, 문자로 바꿔 읽는 to_date(sale_ymd::text, 'YYYYMMDD')를 쓰니 2024-01-05, 2024-02-20이 나왔어요.

사전 준비
사전 준비lab_a 계정으로 실행
CREATE TABLE sales (id int, sale_ymd int, amount int);
INSERT INTO sales VALUES (1, 20240105, 100), (2, 20240220, 70);
OUTPUT
CREATE TABLE
INSERT 0 2
재현lab_a 계정으로 실행
SELECT id, sale_ymd::date AS sale_date FROM sales;
✕ 에러
ERROR:  42846: cannot cast type integer to date
LINE 1: SELECT id, sale_ymd::date AS sale_date FROM sales;
                           ^
확인lab_a 계정으로 실행
SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'sales' AND column_name = 'sale_ymd';
OUTPUT
 column_name | data_type
-------------+-----------
 sale_ymd    | integer
(1 row)
해결 후 재실행lab_a 계정으로 실행
SELECT id, to_date(sale_ymd::text, 'YYYYMMDD') AS sale_date FROM sales ORDER BY id;
✓ 성공
 id | sale_date
----+------------
  1 | 2024-01-05
  2 | 2024-02-20
(2 rows)

02ALTER COLUMN ... TYPE int USING 날짜컬럼::int

orders.order_date를 date에서 int로 바꾸려고 USING order_date::int를 쓰니 cannot cast type date to integer로 실패했어요.

날짜를 먼저 문자로 만드는 USING to_char(order_date, 'YYYYMMDD')::int는 됐어요. 컬럼이 integer가 되고 값은 20240105, 20240220이었어요.

사전 준비
사전 준비lab_a 계정으로 실행
CREATE TABLE orders (id int, order_date date);
INSERT INTO orders VALUES (1, DATE '2024-01-05'), (2, DATE '2024-02-20');
OUTPUT
CREATE TABLE
INSERT 0 2
재현lab_a 계정으로 실행
ALTER TABLE orders ALTER COLUMN order_date TYPE int USING order_date::int;
✕ 에러
ERROR:  42846: cannot cast type date to integer
LINE 1: ...ders ALTER COLUMN order_date TYPE int USING order_date::int;
                                                                 ^
해결lab_a 계정으로 실행
ALTER TABLE orders ALTER COLUMN order_date TYPE int USING to_char(order_date, 'YYYYMMDD')::int;
OUTPUT
ALTER TABLE
해결 후 재실행lab_a 계정으로 실행
SELECT id, order_date, pg_typeof(order_date) FROM orders ORDER BY id;
✓ 성공
 id | order_date | pg_typeof
----+------------+-----------
  1 |   20240105 | integer
  2 |   20240220 | integer
(2 rows)

03SUM(플래그::numeric)으로 true 개수 세기

SELECT SUM(is_paid::numeric) FROM invoices가 cannot cast type boolean to numeric으로 실패했어요.

컬럼은 boolean이었어요. boolean은 int로는 형변환이 돼서 SUM(is_paid::int)가 2를 돌려줬고, COUNT(*) FILTER (WHERE is_paid)도 2였어요.

사전 준비
사전 준비lab_a 계정으로 실행
CREATE TABLE invoices (id int, is_paid boolean);
INSERT INTO invoices VALUES (1, true), (2, false), (3, true);
OUTPUT
CREATE TABLE
INSERT 0 3
재현lab_a 계정으로 실행
SELECT SUM(is_paid::numeric) AS paid_count FROM invoices;
✕ 에러
ERROR:  42846: cannot cast type boolean to numeric
LINE 1: SELECT SUM(is_paid::numeric) AS paid_count FROM invoices;
                          ^
확인lab_a 계정으로 실행
SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'invoices' AND column_name = 'is_paid';
OUTPUT
 column_name | data_type
-------------+-----------
 is_paid     | boolean
(1 row)
해결 후 재실행lab_a 계정으로 실행
SELECT SUM(is_paid::int) AS paid_count, COUNT(*) FILTER (WHERE is_paid) AS paid_count2 FROM invoices;
✓ 성공
 paid_count | paid_count2
------------+-------------
          2 |           2
(1 row)

자주 묻는 질문

PostgreSQL에서 20240105 같은 숫자를 날짜로 바꾸려면요?

문자를 거쳐요. to_date(col::text, 'YYYYMMDD'). 바로 col::date를 하면 42846이 나요.

date 컬럼을 integer로 바꾸려면요?

USING에서 날짜를 문자로 만든 뒤 정수로 바꿔요. ALTER TABLE t ALTER COLUMN d TYPE int USING to_char(d, 'YYYYMMDD')::int;

boolean 컬럼에서 true 개수 세는 법은요?

SUM(flag::int) 또는 COUNT(*) FILTER (WHERE flag). 위 실행에서 둘 다 2였어요. flag::numeric은 42846이 나요.

42846과 42804는 뭐가 달라요?

42846은 두 타입 사이에 형변환 자체가 없다는 뜻이에요. 42804(datatype mismatch)는 형변환을 쓰지 않은 채 다른 타입 자리에 값을 넣을 때 나요.

검증 방법

시나리오마다 새 DB 사용자를 만들어 시작해요. 스크립트가 사전 준비 → 에러 재현(에러 코드 일치 확인) → 해결 → 원래 문장 재실행 순서로 실행하고, 화면의 출력은 DB가 실제로 낸 결과를 스크립트가 그대로 저장한 거예요.

lab_a, lab_b는 테스트용 일반 역할(같은 이름의 스키마 소유), postgres는 슈퍼유저예요. PostgreSQL 17.12