postgresqltimestamppostgresql-9.2

Now() without timezone


I have a column added_at of type timestamp without time zone. I want it's default value to be the current date-time but without time zone. The function now() returns a timezone as well.

How do I solve that problem?


Solution

  • SELECT now()::timestamp;
    

    The cast converts the timestamptz returned by now() to the corresponding timestamp in your time zone - defined by the timezone setting of the session. That's also how the standard SQL function LOCALTIMESTAMP is implemented in Postgres.

    If you don't operate in multiple time zones, that works just fine. Else switch to timestamptz for added_at. The difference?

    BTW, this does exactly the same, just more noisy and expensive:

    SELECT now() AT TIME ZONE current_setting('timezone');