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:
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!
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.
you also can use the command
set datestyle to [your new datestyle];
in the console of postgreSQL.
Use the to_char function with your date as follows:
select to_char(date_column1, 'Mon/DD/YYYY');