PostgreSQL中修改字段类型可使用ALTERTABLE语句,将doubleprecision转换为BIGINT时需指定USING子句进行类型转换,并同步更新默认值。转换默认按四舍五入处理,如需截断小数可使用trunc函数。生产环境操作前建议备份数据,注意检查是否存在小数。
在 PostgreSQL 的实际运维中,调整字段类型算是一个高频操作。业务一变,数据类型就得跟着改——比如某个价格字段,原本设计成 double precision(浮点数),后来发现业务里根本不需要小数,干脆换成 BIGINT(大整数)。这件事听着简单,但真要动手,还是有几个坑需要注意。

长期稳定更新的攒劲资源: >>>点此立即查看<<<
今天就用一张实际业务表来演示,怎么把 double precision 改成 BIGINT,顺便把默认值也一块儿处理利索。
假设我们有一张表叫 public.example_price_change_log,里面有个字段 new_value,当前类型是 double precision。想看看表长什么样?用 \d+ 命令就行:
\d+ public.example_price_change_log
输出长这样:
Table "public.example_price_change_log" Column | Type --------------------+------------------ id | integer operator_id | integer category_code | text new_value | double precision effective_time | bigint created_time | bigint expire_time | bigint status | integer remark | text
这次要动刀的就是 new_value 这个字段。
PostgreSQL 里改字段类型,标准语法是 ALTER TABLE ... ALTER COLUMN ... TYPE。把 new_value 从 double precision 改成 BIGINT,SQL 如下:
ALTER TABLE public.example_price_change_log ALTER COLUMN new_value TYPE BIGINT USING new_value::BIGINT;
这里的关键是 USING new_value::BIGINT——它告诉数据库“用这种方式把旧值转换成新类型”。如果不加 USING,某些类型转换可能报错,所以最好明确写上。
如果原字段设了默认值,比如 '-1'::integer,改完类型后最好也把默认值统一成 BIGINT:
ALTER TABLE public.example_price_change_log ALTER COLUMN new_value SET DEFAULT -1::BIGINT;
当然,也可以把两步合成一条 SQL:
ALTER TABLE public.example_price_change_log ALTER COLUMN new_value TYPE BIGINT USING new_value::BIGINT, ALTER COLUMN new_value SET DEFAULT -1::BIGINT;
再跑一遍 \d+,看看成果:
\d+ public.example_price_change_log
输出变成:
Column | Type | Default ----------+--------+-------------- new_value | bigint | - 1::bigint
仔细看,默认值显示成了 - 1::bigint,中间多了个空格。别慌,这是 PostgreSQL 对表达式显示格式的“小癖好”,实际含义和 -1::bigint 完全一样,不影响使用。
如果想更严谨地确认,可以查 information_schema.columns:
SELECT column_default FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'example_price_change_log' AND column_name = 'new_value';
结果可能还是 - 1::bigint,正常现象,不用额外处理。
既然原字段是 double precision,很可能存着小数。转成 BIGINT 之前,先扫一眼到底有没有小数:
SELECT * FROM public.example_price_change_log WHERE new_value IS NOT NULL AND new_value <> trunc(new_value);
如果查询结果为空,说明没有小数,可以放心转。
直接 USING new_value::BIGINT 时,PostgreSQL 会按四舍五入处理。比如:
12.3 -> 12 12.7 -> 13
如果业务不允许自动四舍五入,务必提前确认规则。
想截断小数而不是四舍五入?可以用 trunc():
ALTER TABLE public.example_price_change_log ALTER COLUMN new_value TYPE BIGINT USING trunc(new_value)::BIGINT, ALTER COLUMN new_value SET DEFAULT -1::BIGINT;
效果:
12.9 -> 12 12.1 -> 12
改字段类型属于表结构变更,生产环境执行前最好备份数据。简单点可以建一张备份表:
CREATE TABLE public.example_price_change_log_bak AS SELECT * FROM public.example_price_change_log;
或者用 pg_dump 工具,怎么放心怎么来。
ALTER TABLE public.example_price_change_log ALTER COLUMN new_value TYPE BIGINT USING new_value::BIGINT, ALTER COLUMN new_value SET DEFAULT -1::BIGINT;
ALTER TABLE public.example_price_change_log ALTER COLUMN new_value TYPE BIGINT USING trunc(new_value)::BIGINT, ALTER COLUMN new_value SET DEFAULT -1::BIGINT;
改完之后,字段类型从 double precision 变成 bigint,默认值是 -1::bigint。就算 \d+ 里显示成 - 1::bigint,也完全正常——只是 PostgreSQL 的显示格式问题,里子没变。
PostgreSQL 修改字段类型的核心语法就一句话:
ALTER TABLE 表名 ALTER COLUMN 字段名 TYPE 新类型 USING 字段名::新类型;
回到这个案例,把 double precision 改成 BIGINT 的完整示例:
ALTER TABLE public.example_price_change_log ALTER COLUMN new_value TYPE BIGINT USING new_value::BIGINT;
如果字段有默认值,最好同步改掉:
ALTER TABLE public.example_price_change_log ALTER COLUMN new_value SET DEFAULT -1::BIGINT;
最终推荐写法是两者合并,这样字段类型和默认值类型保持一致,后续维护少踩坑。
侠游戏发布此文仅为了传递信息,不代表侠游戏网站认同其观点或证实其描述