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?
Join Stack Overflow to learn, share knowledge, and build your career.
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?
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');
now()
already returns timestamptz
, hence now()::timestamptz
doesn't do anything useful.
– Erwin Brandstetter
Jun 28 '18 at 11:26
now()::timestamp
might do some casting, but it does not convert from local time zone to UTC for me. But now() AT TIME ZONE 'UTC';
does the right thing.
– ThoPaz
Sep 25 '20 at 10:27
now() AT TIME ZONE 'UTC'
returns the local time for time zone UTC. LOCALTIMESTAMP
or now()::timestamp
return the local time for the current timezone
setting of the session. Two different things! Only happens to return the same while your current session runs with timezone = 'UTC'
.
– Erwin Brandstetter
Sep 25 '20 at 16:44
Well you can do something like:
SELECT now() AT TIME ZONE current_setting('TimeZone');
SELECT now() AT TIME ZONE 'Europe/Paris';
SELECT now() AT TIME ZONE 'UTC';
Not sure how that makes any sense for a column "added_at". You almost always want an absolute timestamp (timestamp with time zone) not a floating one.
Edit responding to points below:
Yes, should use timestamp with time zone (absolute time) unless you have a good reason not to.
The client timezone is given by SHOW TimeZone
or current_setting(...)
as shown above.
Do take some time to skim the manuals - they cover all this quite well.
timestamp with time zone
? 2) How do I know what's the current timezone of a machine where postgresql is running?
– アレックス
Dec 28 '13 at 7:41
CURRENT_TIME and CURRENT_TIMESTAMP deliver values with time zone; LOCALTIME and LOCALTIMESTAMP deliver values without time zone.
New, and native answer in 2020
In PostgreSQL, If you only want the current date-time by calling CURRENT_TIMESTAMP()
without time zone, and fractional digits in the seconds field which come after the decimal point of the seconds field?
(Tested on PostgreSQL v12.4)
Then use this:
SELECT CURRENT_TIMESTAMP(0)::TIMESTAMP WITHOUT TIME ZONE;
If you define your column's data type as timestamp
(not as timestamptz
), then you can store the timestamp without time zone, in that case you don't neet to add TIMESTAMP WITHOUT TIME ZONE
Like this:
CREATE TABLE foo (created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP(0))
In the above function, 0 is passed to get rid of the fractional digits in the seconds field.
with timestamp
, especially if your code is run anywhere where daylight savings is used. Or save the time in UTC, not a local timezone. You can always choose what timezone you want to display the data in. – some Dec 28 '13 at 10:27with timezone
? – アレックス Dec 28 '13 at 12:48timestamp with time zone
. It doesn't carry a time zone with it at all. It is just in absolute UTC time, with conversion to/from the local time in theTimeZone
session variable. Just usetimestamp with time zone
; it's almost always the right choice. – Craig Ringer Dec 29 '13 at 4:13