Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I alter the date format in Postgres?

Tags:

postgresql

I'm getting the following error message

ERROR: date/time field value out of range: "13/01/2010" HINT: Perhaps you need a different "datestyle" setting.

I want to get my date in the format DD/MM/YYYY

like image 385
deltanovember Avatar asked May 25 '11 11:05

deltanovember


People also ask

How do I change the date format in PostgreSQL?

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 date format from YYYY-MM-DD in PostgreSQL?

1) Get the current date To get the current date and time, you use the built-in NOW() function. However, to get the date part only (without the time part), you use the double colons (::) to cast a DATETIME value to a DATE value. The result is in the format: yyyy-mm-dd .

What is the datatype for date in PostgreSQL?

The date format for the date data type in PostgreSQL is yyyy-mm-dd . This is the format used for both storing data and for inserting data.

How do I convert datetime to date in PostgreSQL?

The TO_DATE function in PostgreSQL is used to converting strings into dates. Its syntax is TO_DATE(text, text) and the return type is date. The TO_TIMESTAMP function converts string data into timestamps with timezone. Its syntax is to_timestamp(text, text) .


2 Answers

SHOW datestyle;   DateStyle  -----------  ISO, MDY (1 row)  INSERT INTO container VALUES ('13/01/2010'); ERROR:  date/time field value out of range: "13/01/2010" HINT:  Perhaps you need a different "datestyle" setting.  SET datestyle = "ISO, DMY"; SET  INSERT INTO container VALUES ('13/01/2010'); INSERT 0 1  SET datestyle = default; SET 

http://www.postgresql.org/docs/current/static/runtime-config-client.html#GUC-DATESTYLE

DateStyle - Sets the display format for date and time values, as well as the rules for interpreting ambiguous date input values. For historical reasons, this variable contains two independent components: the output format specification (ISO, Postgres, SQL, or German) and the input/output specification for year/month/day ordering (DMY, MDY, or YMD).

Of course it's best to use unambiguous input format (ISO 8601), but there is no problem to adjust it as you need.

like image 149
Grzegorz Szpetkowski Avatar answered Sep 22 '22 19:09

Grzegorz Szpetkowski


You could set the date style to European dd/mm/yyyy:

SET DateStyle TO European; 

I'd advise against this though. I generally try to convert between formats, and keep ISO formatted dates in the data source. After all, it's only a matter of representation, not a matter of different data.

like image 32
Berry Langerak Avatar answered Sep 20 '22 19:09

Berry Langerak