Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change datestyle in PostgreSQL?

Tags:

postgresql

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:

   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?

like image 577
Kliver Max Avatar asked Nov 06 '12 04:11

Kliver Max


People also ask

How do I change the Datestyle setting in SQL?

You can change the format in the postgresql. conf file. 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.

How do I change the default date format in PostgreSQL?

PostgreSQL uses the yyyy-mm-dd format for storing and inserting date values. If you create a table that has a DATE column and you want to use the current date as the default value for the column, you can use the CURRENT_DATE after the DEFAULT keyword.

How do you change character type in PostgreSQL?

First, specify the name of the table to which the column you want to change after the ALTER TABLE keywords. Second, specify the name of the column that you want to change the data type after the ALTER COLUMN clause. Third, supply the new data type for the column after the TYPE keyword.


2 Answers

you also can use the command

set datestyle to [your new datestyle]; 

in the console of postgreSQL.

like image 158
T Ismael Verdugo Avatar answered Oct 07 '22 21:10

T Ismael Verdugo


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!

like image 34
Ha Sh Avatar answered Oct 07 '22 22:10

Ha Sh