【发布时间】:2021-10-20 22:09:48
【问题描述】:
我正在使用带有 timescaledb 扩展的 Postgresql,pg13 - tsdb 2.2.1
表 1
create table user
(
...
join_dt timestamptz NULL,
...
);
表 2
create table a_user
(
...
join_dt timestamptz NULL,
...
);
我使用 SQL 插入到 a_user 表中,因此表中有数据,并希望通过查询将数据移动到用户表:
insert into user
select * from a_user;
我收到此错误:
[42804] 错误:列“join_dt”是带时区的时间戳类型,但表达式的类型是字符变化
原始数据来自这样的表
create table ori_user
(
...
join_dt timestamp(6) with time zone,
...
);
我从 ori_user 以 SQL 插入形式导出数据,然后插入到 a_user,现在我想从 a_user 移动到用户。
这是我尝试过的:
insert into user select
...
join_dt::timestamptz,
...
from a_user;
不起作用。
with aa as (select pg_typeof(join_dt)::varchar as type,* from a_user)
select * from aa where aa.type not like 'timestamp with time zone';
不显示任何行。
还有其他解决方案吗?请帮忙。 提前致谢。
【问题讨论】:
-
列顺序很可能不匹配。始终指定目标表是一种很好的编码习惯:
insert into "user" (col1, col2, join_dt) select col1, col2, join_dt from a_user -
天哪,我怎么没想到……它们应该是完全相同的,所以我猜它们是一样的,但现在我发现列的顺序不同。我觉得很愚蠢..谢谢你指出我的错误。!
标签: sql postgresql timestamp timescaledb