如何设置PGX从DB获取UTC值?
Postgres中创建的列是:
used_at timestamp with time zone NOT NULL,
Postgres中的值是在没有时区(在UTC中)的:
2022-06-30 22:49:03.970913+00
使用此查询:
show timezone
我获取:
Etc/UTC
但是从ENT(使用pgx
stdlib
)i获取值:
2022-07-01T00:49:03.970913+02:00
使用pgdriver
/pq
我从db获取UTC值。
如何设置pgx
获取UTC值?
我也尝试使用此代码使用此连接字符串:
import (
"database/sql"
_ "github.com/jackc/pgx/v4/stdlib"
)
conn, err := sql.Open("pgx", "postgres://postgres:postgres@localhost/project?sslmode=disable&timezone=UTC")
//handle err
问题仍在这里。
我需要一种从db的UTC值(存储在DB中的Laready)的方法。
The column created in Postgres is:
used_at timestamp with time zone NOT NULL,
The value in Postgres is saved without timezone (in UTC):
2022-06-30 22:49:03.970913+00
Using this query:
show timezone
I get:
Etc/UTC
But from Ent (using pgx
stdlib
) I get the value:
2022-07-01T00:49:03.970913+02:00
Using pgdriver
/pq
I get the UTC value from DB.
How can I setup pgx
to get UTC value?
I tried using this connection string with this code too:
import (
"database/sql"
_ "github.com/jackc/pgx/v4/stdlib"
)
conn, err := sql.Open("pgx", "postgres://postgres:postgres@localhost/project?sslmode=disable&timezone=UTC")
//handle err
The problem is still here.
I need a way to get back from DB the UTC values (that are laready stored in the DB).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
timezone
不是有效参数键字 。但是,您可以使用 关键字指定命令行选项,以在连接启动时发送到服务器。请记住,您需要%encode 其中。
如何设置代码> :
但是,这仅执行连接的时区,该时区似乎对
pgx
一旦从数据库中读取pgx
解析时间戳本身。实际上,似乎直接或间接地依赖于主机机器的本地时区。要确认您可以更新全局time.local
变量为UTC并观察差异。出于明显的原因,我避免进行上述。如果
PGX
没有提供一种配置用于解析时间戳的默认位置的方法类型。timezone
is not a valid parameter key word.You can however use the
options
key word to specify command-line options to send to the server at connection start. Just keep in mind that you need to percent encode the values therein.Example of how to set the
TimeZone
:However this only enforces the connection's timezone which does not seem to have an effect on how
pgx
parses the timestamps themselves once read from the database. In fact it seems it relies, directly or indirectly, on the host machine's local timezone. To confirm that you can update the globaltime.Local
variable to UTC and observe the difference.For obvious reasons I'd avoid doing the above. If
pgx
doesn't provide a way to configure the default location it uses to parse the timestamps then the next best option, that I can think of, would be to use a customtime.Time
type.您可以将应用程序配置为全球使用UTC:
You can configure your application to use UTC globally: