PostgreSQL:根据客户端的时间戳不同,不带时区的输出 [英] PostgreSQL: different output for timestamp without time zone depending on client

查看:1519
本文介绍了PostgreSQL:根据客户端的时间戳不同,不带时区的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PostgreSQL数据库感到困惑,它为 timestamp(0)而不带时区字段返回不同的结果。假设我有一张表 t

I'm puzzled by my PostgreSQL database returning different results for a timestamp(0) without time zone field. Let's say I have a table t

create table t (
    x timestamp(0) without time zone
);
insert into t select now();

现在,当我运行 select * from t 来自不同的客户,我得到不同的输出。

Now when I run select * from t from different clients, I get different outputs.


  • PSQL: 2014 -04-06 10:22:57

JDBC: 2014 -04-06 10:22:57:000
(driver postgresql-9.3-1101.jdbc4.jar运行 SQLExplorer eclipse插件

node-pg: Sun Apr 06 2014 10:22:57 GMT + 0200(CEST)

在三个中,只有PSQL是我预期的(和要求)的输出。为什么会发生这种情况,如何解决这个问题,而不需要在查询中显式转换?

Of the three, only PSQL is the output I expected (and require). Why is this happening, and how can I fix it without having to explicitly cast in the query?

推荐答案

数据库返回的时间戳的字符串格式,通常应该在查询中请求。

If you want a particular string format for a timestamp to be returned by the database, you should generally request it in your queries.

to_char(the_timestamp_col, 'YYYY-MM-DD HH:MI:SS')

请参阅中/www.postgresql.org/docs/current/static/functions-formatting.htmlrel =nofollow> to_char

或者,您可以使用客户端应用程序语言的功能来按需要格式化时间戳。对于更多功能丰富的语言,这通常是更好的选择。

Alternately, you can use the client application language's features to format the timestamp as you desire. This is usually the better option for more feature rich languages.

虽然PostgreSQL通常会通过查询协议返回特定的日期格式,客户端可以自由选择使用而是在二进制协议中,在这种情况下,它们将在时代秒内获得宽整数值。而不是依靠PostgreSQL协议中的价值来格式化你想要的,更好的是要求你所需要的。

While PostgreSQL usually returns a specific text format for dates over the query protocol, clients are free to choose to use the binary protocol instead, in which case they'll get a wide integer value in epoch seconds. Rather than relying on the value in the PostgreSQL protocol being formatted how you want, it's much better to ask for exactly what you require.

这篇关于PostgreSQL:根据客户端的时间戳不同,不带时区的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆