How to Set the datestyle Variable for the Current Session in PostgreSQL

PostgreSQL has a datestyle variable that specifies the display format for date and time values, as well as the rules for interpreting ambiguous date input values. 

We can set the date/time style with the SET datestyle command, the DateStyle parameter in the postgresql.conf configuration file, or the PGDATESTYLE environment variable on the server or client.

Below is an example of using the SET datestyle command to change the datestyle for the current session.

Set the Variable

Here’s an example of code used to set the datestyle variable for the current session:

SET datestyle TO Euro;

Output:

SET

That’s all. The datestyle variable has been changed for the current session.

Check the Result

Let’s now check the datestyle variable:

SHOW datestyle;

Output:

 DateStyle 
-----------
ISO, DMY

Euro and European are synonyms for DMY, while US, NonEuro, and NonEuropean are synonyms for MDY. That’s why SHOW datestyle returns DMY when I used Euro to set it.

The value of the datestyle variable contains two separate components: the output format specification (ISOPostgres, SQL, or German) and the input/output specification for year/month/day ordering (DMY, MDY, or YMD). These can be set separately or together.

The default value is ISO, MDY, but initdb will initialise the configuration file with a setting that corresponds to the behaviour of the chosen lc_time locale.