如何更改 PostgreSQL 中的日期样式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13244460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change datestyle in PostgreSQL?
提问by Kliver Max
In postgres I have a table with date column. Now postgres allows me to write date in Y-m-d format. But I need date in d/m/Y format. How to change it?
在 postgres 中,我有一个带有日期列的表。现在 postgres 允许我以 Ymd 格式写日期。但我需要 d/m/Y 格式的日期。如何改变它?
When I do:
当我做:
show datestyle;
I get:
我得到:
"ISO, DMY"
And input date in table in this format 13/02/2009
并以这种格式在表中输入日期 13/02/2009
But when I close and open table again I see this 2009-02-13
. JDBC gives me date in this format too. What am I doing wrong?
但是当我再次关闭并打开桌子时,我看到了这一点2009-02-13
。JDBC 也以这种格式为我提供日期。我究竟做错了什么?
采纳答案by Ha Sh
yyyy-mm-dd is the recommended format for date field, its the ISO 8601 format.
yyyy-mm-dd 是日期字段的推荐格式,它是 ISO 8601 格式。
You can change the format in the postgresql.conf file.
您可以更改 postgresql.conf 文件中的格式。
The documentstates
该文件指出
The date/time styles can be selected by the user using the SET datestyle command, the DateStyle parameter in the postgresql.conf configuration file, or the PGDATESTYLE environment variable on the server or client. The formatting function to_char is also available as a more flexible way to format date/time output.
用户可以使用 SET datestyle 命令、postgresql.conf 配置文件中的 DateStyle 参数或服务器或客户端上的 PGDATESTYLE 环境变量来选择日期/时间样式。格式化函数 to_char 也可以作为一种更灵活的方式来格式化日期/时间输出。
Hope this helps!
希望这可以帮助!
回答by T Ismael Verdugo
you also can use the command
你也可以使用命令
set datestyle to [your new datestyle];
in the console of postgreSQL.
在 postgreSQL 的控制台中。
回答by hd1
回答by Craig Ringer
If at all possible, don't use DATESTYLE
. It'll affect all code in your session, including things like stored procedures that might not be expecting it and might not have set an explicit overriding DATESTYLE
in their definitions.
如果可能,请不要使用DATESTYLE
. 它会影响会话中的所有代码,包括可能不期望它并且可能没有DATESTYLE
在其定义中设置显式覆盖的存储过程之类的东西。
If you can, use to_char
for date output, and to_timestamp
for date input whenever you need to use any date formats that might be ambiguous, like D/M/Y
. Use ISO dates the rest of the time.
如果可以,请to_char
用于日期输出和to_timestamp
日期输入,只要您需要使用任何可能不明确的日期格式,例如D/M/Y
. 其余时间使用 ISO 日期。