postgresql_16:3.5.5.2 Date/Time Output
PostgreSQL 16 > The SQL Language > Data Types > Date/Time Types > Date/Time Output
The output format of the date/time types can be set to one of the four styles ISO 8601, SQL (Ingres), traditional POSTGRES (Unix date format), or German. The default is the ISO format. (The SQL standard requires the use of the ISO 8601 format. The name of the “SQL” output format is a historical accident.) (see datatype-datetime-output-table) shows examples of each output style. The output of the date and time types is generally only the date or time part in accordance with the given examples. However, the POSTGRES style outputs date-only values in ISO format.
| Style Specification | Description | Example | | --- | --- | --- | | ISO | ISO 8601, SQL standard | 1997-12-17 07:37:16-08 | | SQL | traditional style | 12/17/1997 07:37:16.00 PST | | Postgres | original style | Wed Dec 17 07:37:16 1997 PST | | German | regional style | 17.12.1997 07:37:16.00 PST |
Note
ISO 8601 specifies the use of uppercase letter T to separate the date and time. PostgreSQL accepts that format on input, but on output it uses a space rather than T, as shown above. This is for readability and for consistency with RFC 3339 as well as some other database systems.
In the SQL and POSTGRES styles, day appears before month if DMY field ordering has been specified, otherwise month appears before day. (See (see postgresql_16:3.5.5.1) for how this setting also affects interpretation of input values.) (see datatype-datetime-output2-table) shows examples.
| datestyle Setting | Input Ordering | Example Output | | --- | --- | --- | | SQL, DMY | *day*/*month*/*year* | 17/12/1997 15:37:16.00 CET | | SQL, MDY | *month*/*day*/*year* | 12/17/1997 07:37:16.00 PST | | Postgres, DMY | *day*/*month*/*year* | Wed 17 Dec 07:37:16 1997 PST |
In the ISO style, the time zone is always shown as a signed numeric offset from UTC, with positive sign used for zones east of Greenwich. The offset will be shown as *hh* (hours only) if it is an integral number of hours, else as *hh*:*mm* if it is an integral number of minutes, else as *hh*:*mm*:*ss*. (The third case is not possible with any modern time zone standard, but it can appear when working with timestamps that predate the adoption of standardized time zones.) In the other date styles, the time zone is shown as an alphabetic abbreviation if one is in common use in the current zone. Otherwise it appears as a signed numeric offset in ISO 8601 basic format (*hh* or *hhmm*).
The date/time style can be selected by the user using the SET datestyle command, the (see guc-datestyle) parameter in the postgresql.conf configuration file, or the PGDATESTYLE environment variable on the server or client.
The formatting function to_char (see (see postgresql_16:3.6.8)) is also available as a more flexible way to format date/time output.