Using current time in UTC as default value in PostgreSQL

前端 未结 6 479
北海茫月
北海茫月 2020-12-22 17:28

I have a column of the TIMESTAMP WITHOUT TIME ZONE type and would like to have that default to the current time in UTC. Getting the current time in UTC is easy:

6条回答
  •  甜味超标
    2020-12-22 18:08

    What about

    now()::timestamp
    

    If your other timestamp are without time zone then this cast will yield the matching type "timestamp without time zone" for the current time.

    I would like to read what others think about that option, though. I still don't trust in my understanding of this "with/without" time zone stuff.

    EDIT: Adding Michael Ekoka's comment here because it clarifies an important point:

    Caveat. The question is about generating default timestamp in UTC for a timestamp column that happens to not store the time zone (perhaps because there's no need to store the time zone if you know that all your timestamps share the same). What your solution does is to generate a local timestamp (which for most people will not necessarily be set to UTC) and store it as a naive timestamp (one that does not specify its time zone).

提交回复
热议问题