Skip to content

return native PostgreSQL ISO8601 format #2924

Answered by wolfgangwalther
seven1240 asked this question in Q&A
Discussion options

You must be logged in to vote

This format is used when returning a timestamp as json:

postgres=> SELECT to_json(now());
              to_json
------------------------------------
 "2023-08-30T15:04:48.724897+00:00"
(1 row)

Since we use the built-in transformation to json now, all columns exposed as type timestamp will have this format. However, if you cast your columns to text first, you will receive what you want:

postgres=> SELECT to_json(now()::TEXT);
             to_json
---------------------------------
 "2023-08-30 15:04:44.512372+00"
(1 row)

Datestyle settings will only affect the cast to TEXT (2nd example), not the cast to json directly.

You can achieve the cast either via VIEWs, via computed columns (as menti…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@wolfgangwalther
Comment options

Answer selected by seven1240
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #2920 on August 29, 2023 19:42.