How to INSERT now() + INTERVAL in Postgres 9.x

ajxs picture ajxs · Sep 16, 2017 · Viewed 15.3k times · Source

I'm trying to INSERT a future date into a timestamp with time zone column in a table in Postgres 9.6.2. I'd like this date to be the current transaction time plus an arbitrary interval, such as 1 hour. I understand how to do date/time arithmetic in a SELECT statement, but the same syntax of NOW() + INTERVAL '1 hour' does not seem to be valid in an INSERT statement.

Aside from accomplishing this at the application level, is it possible to accomplish this using a single INSERT statement using date/time arithmetic? If not, what would best practice be in this situation?

Answer

user2182349 picture user2182349 · Sep 16, 2017

I think you have it right - this worked for me:

insert into barcode (id,barcode,active,created,updated) 
    values (111,'23432', true, NOW() + INTERVAL '1 hour',NOW());

I am running Postgres 9.4.13.