Postgres 拒绝将时间戳值从一个 table 插入到另一个 table

Postgres refuses to insert timestamp value from one table to another table

Table 富:

create table table_foo
(
    foo_id            integer,
    foo_some_timestamp timestamp without timezone
)

Table栏:

create table table_bar
(
    bar_id            integer,
    foo_id            integer,
    bar_some_timestamp timestamp without timezone
)

当我像这样插入时,它失败了:

insert into table_bar (foo_id, bar_some_timestamp) (
  select foo_id, foo_some_timestamp as bar_some_timestamp
  from foo
  left join table_bar on table_foo.foo_id = table_bar.foo_id
);

但是我得到一个错误:

column "foo_sime_timestamp" is of type timestamp without time zone but expression is of type text Hint: You will need to rewrite or cast the expression.

我试过 to_timestamp 奇怪地抛出以下错误:

to_timestamp(foo_some_timestamp, 'YYYY-MM-DD HH24-MI-SS.US')::timestamp without time zone as bar_some_timestamp

 ERROR: function to_timestamp(timestamp without time zone, unknown) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts.

由于是左连接,结果包含很多空值。但是使用 COALESCE 会产生相同的错误。

我错过了什么? (Postgres v11)

您的目标 table 有三列,但您的 select 只提供了两列。您应该始终限定 INSERT 语句中的目标列,但如果您提供的少于目标 table 的列,则这是强制性的。列按位置匹配,而不是按名称匹配。

insert into table_bar (foo_id, bar_some_timestamp)
select tf.foo_id, tf.foo_some_timestamp
from table_foo tf
  left join table_bar tb on tf.foo_id = tb.foo_id
;

Online example