天天看点

postgresql 类型转化时遇到的问题

postgres=# alter table test alter config type jsonb;
ERROR:  column "config" cannot be cast automatically to type jsonb 
HINT:  Specify a USING jsonb to perform the conversion.
           

之前config 字段类型为 hstore,怎么才能转化呢,如下三种方式都可以:

第一种

第二种

第三种

alter table test alter config type jsonb using cast(config as jsonb);

#test case
select cast('12' as int);
select '12'::int;
           

如果是int 类型变更为 varchar 可以不用显示的using 方式去变更,这个与类型的强弱有关系, 如 5/2.0 其实计算机会把5先转成与2.0一样的浮点类型,然后再做除法, 如果想要5/2.0得到整数,则这样才能获得:(int)5/2.0 。

注:5/2.0的类型转化是C语言例子

继续阅读