One thing that I have been asked quite frequently is
“How do I add 1 day to now()
?”
in
PostgreSQL?
This is rather easy using
PostgreSQL’s interval
operator:
date + interval → timestamp
Example: How to add 1 day to current date/time:
postgres=# SELECT NOW() + INTERVAL '1 day';
?column?
-------------------------------
2023-04-28 08:48:17.944839+02
(1 row)
postgres=#
Example use: Add 7 day to current date:
postgres=# SELECT CURRENT_DATE + INTERVAL '7 days';
?column?
---------------------
2023-05-04 00:00:00
(1 row)
postgres=#
In addition to basic arithmetic operations, interval types can be used in combination with various date/time functions to manipulate timestamps and perform more complex calculations. The interval type provides a flexible and convenient way to work with durations of time in PostgreSQL.