简体   繁体   English

如何在PostgreSQL中更改datestyle?

[英]How to change datestyle in PostgreSQL?

In postgres I have a table with date column. 在postgres中,我有一个带有日期列的表。 Now postgres allows me to write date in Ymd format. 现在postgres允许我以Ymd格式写日期。 But I need date in d/m/Y format. 但我需要d / m / Y格式的日期。 How to change it? 怎么改呢?

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 . 但当我再次关闭并打开桌子时,我看到了2009-02-13 JDBC gives me date in this format too. JDBC也以这种格式给出了日期。 What am I doing wrong? 我究竟做错了什么?

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 document states 文件指出

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. 用户可以使用SET datestyle命令,postgresql.conf配置文件中的DateStyle参数或服务器或客户端上的PGDATESTYLE环境变量来选择日期/时间样式。 The formatting function to_char is also available as a more flexible way to format date/time output. 格式化函数to_char也可用作格式化日期/时间输出的更灵活方式。

Hope this helps! 希望这可以帮助!

you also can use the command 你也可以使用该命令

set datestyle to [your new datestyle];

in the console of postgreSQL. 在postgreSQL的控制台中。

使用带有日期的to_char函数,如下所示:

select to_char(date_column1, 'Mon/DD/YYYY');

If at all possible, don't use DATESTYLE . 如果可能的话,不要使用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

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 . 如果可以,只要需要使用任何可能不明确的日期格式(如D/M/Y ,就可以使用to_char作为日期输出,并使用to_timestamp作为日期输入。 Use ISO dates the rest of the time. 其余时间使用ISO日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM