我们可以改变在 postgresql 中存储为文本列的 epoc 时间类型吗

问题描述

select to_timestamp(created)::timestamp from users limit 1;
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

我正在尝试将在 postgresql 中存储为字符串的 epoc 时间列转换为时间戳。但出现上述错误。任何建议。

解决方法

to_timestamp 函数有两个版本,一个接受一个浮点参数(预计是一个 unix epoch 偏移量),另一个接受两个文本参数,其中第一个参数是一个文本字符串,第二个映射第一个文本参数的格式。

在您的情况下,您将纪元偏移量存储为文本,但您基本上想要该函数的第一个版本。您应该能够执行以下操作:

select to_timestamp(created::float) from users limit 1;