简体   繁体   中英

How to change datestyle in PostgreSQL?

In postgres I have a table with date column. Now postgres allows me to write date in Ymd format. But I need date in d/m/Y format. How to change it?

When I do:

   show datestyle;

I get:

 "ISO, DMY"

And input date in table in this format 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?

yyyy-mm-dd is the recommended format for date field, its the ISO 8601 format.

You can change the format in the postgresql.conf file.

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. The formatting function to_char is also available as a more flexible way to format date/time output.

Hope this helps!

you also can use the command

set datestyle to [your new datestyle];

in the console of postgreSQL.

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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