Now() 没有时区 [英] Now() without timezone
问题描述
我有一列 added_at
类型为 timestamp without time zone
.我希望它的默认值是当前日期时间但没有时区.函数 now()
也返回一个时区.
I have a column added_at
of type timestamp without time zone
. I want it's default value to be the current date-time but without time zone. The function now()
returns a timezone as well.
我该如何解决这个问题?
How do I solve that problem?
推荐答案
SELECT now()::timestamp;
演员将 now()
返回的 timestamptz
转换为您所在时区中相应的 timestamp
- 由 timezone
会话设置.这也是 标准 SQL 函数 LOCALTIMESTAMP代码>
在 Postgres 中实现.
The cast converts the timestamptz
returned by now()
to the corresponding timestamp
in your time zone - defined by the timezone
setting of the session. That's also how the standard SQL function LOCALTIMESTAMP
is implemented in Postgres.
如果您不在多个时区运营,那效果很好.否则为 added_at
切换到 timestamptz
.区别?
If you don't operate in multiple time zones, that works just fine. Else switch to timestamptz
for added_at
. The difference?
顺便说一句,这完全相同,只是噪音更大,成本更高:
BTW, this does exactly the same, just more noisy and expensive:
SELECT now() AT TIME ZONE current_setting('timezone');
这篇关于Now() 没有时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!