How to change datestyle in PostgreSQL?

后端 未结 4 399
春和景丽
春和景丽 2020-12-14 17:00

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?

When I do:

相关标签:
4条回答
  • 2020-12-14 17:32

    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!

    0 讨论(0)
  • 2020-12-14 17:33

    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.

    0 讨论(0)
  • 2020-12-14 17:34

    you also can use the command

    set datestyle to [your new datestyle];
    

    in the console of postgreSQL.

    0 讨论(0)
  • 2020-12-14 17:39

    Use the to_char function with your date as follows:

    select to_char(date_column1, 'Mon/DD/YYYY');
    
    0 讨论(0)
提交回复
热议问题