将列数据类型从数组更改为整数

change column datatype from array to integer

我需要将列的数据类型从 _float8 更改为 int4

ALTER TABLE "table" ALTER COLUMN "col" SET DATA TYPE int4;

结果 column "col" cannot be cast automatically to type integer

ALTER TABLE "table" ALTER COLUMN "col" SET DATA TYPE int4 USING (col::integer);

结果 cannot cast type double precision[] to integer

有什么想法吗?

你必须指出在转换中应该使用数组的哪个元素,例如

alter table x alter column y set data type int4 using (y[1]::int)

db<>fiddle.

问题是您有一个 数组。要解决此问题,您需要数组运算符:

ALTER TABLE "table"
    ALTER COLUMN "col" SET DATA TYPE int4 USING (col[1]::integer);

Here 是一个 db<>fiddle.