PostgreSQL Date Addition: How to Use Intervals and Operators
In this article
PostgreSQL does not have a single function named date_add() like MySQL does; instead, it uses the + operator combined with the INTERVAL data type to perform date and time arithmetic. To add time to a date, you simply use the syntax date_column + INTERVAL '1 day'. This approach is more flexible than a dedicated function because it allows you to mix and match different units of time—such as hours, months, and years—within a single expression.
What You'll Need
- A running PostgreSQL database instance.
- A client tool to execute queries, such as pgAdmin, psql, or DBeaver.
- A table containing a column with a
DATE,TIMESTAMP, orTIMESTAMPTZdata type. - Basic knowledge of SQL
SELECTandUPDATEstatements.
Step-by-Step
-
Identify your target date value. Determine whether you are working with a hard-coded date string, a column from a table, or a system function like
CURRENT_DATEorNOW(). For example, if you want to calculate a deadline based on today's date, you will useCURRENT_DATE. -
Define the Interval. The
INTERVALkeyword tells PostgreSQL that the following string represents a duration of time. You must enclose the duration in single quotes. Common formats include'1 day','2 weeks','3 months', or'5 hours'. You can also be more specific, such as'1 year 2 months 3 days'. -
Apply the Addition Operator. Use the plus sign (
+) to combine the date and the interval.
Example 1: Adding days to a date
SELECT CURRENT_DATE + INTERVAL '7 days';
This query takes today's date and returns the date exactly one week from now. -
Handle Data Type Casting. When you add an
INTERVALto aDATE, PostgreSQL automatically converts the result to aTIMESTAMP. If you need the result to remain aDATEtype (removing the time component), you must cast it back using the::DATEsyntax.
Example 2: Casting to Date
SELECT (CURRENT_DATE + INTERVAL '1 month')::DATE; -
Perform Complex Arithmetic. You can add multiple intervals together or use a variable to define the number of units. To use a column value as the number of days to add, you can multiply an interval of one unit by that column.
Example 3: Using a column for dynamic addition
Assuming you have a tableorderswith a columnshipping_days:
SELECT order_date + (shipping_days * INTERVAL '1 day') FROM orders; -
Update existing records. To permanently change dates in your table, use the
UPDATEstatement combined with the interval logic.
Example 4: Extending a subscription
UPDATE subscriptions SET expiry_date = expiry_date + INTERVAL '1 year' WHERE user_id = 123;
Common Mistakes
One of the most frequent errors is attempting to use date_add(). Because this function exists in other SQL dialects but not in PostgreSQL, users often receive a "function does not exist" error. Always remember that PostgreSQL relies on operators and the INTERVAL type.
Another common mistake is forgetting the single quotes around the interval value. Writing INTERVAL 1 day without quotes will result in a syntax error. The duration must be a string literal, such as '1 day'.
Users also frequently struggle with the result type. Adding an interval to a date changes the data type from DATE to TIMESTAMP. If your application expects a date format (YYYY-MM-DD) and receives a timestamp (YYYY-MM-DD HH:MM:SS), it may cause errors in the frontend or during data export. Always use ::DATE if the time of day is irrelevant.
Lastly, be careful with month and year additions. Because months have different lengths (28 to 31 days), adding INTERVAL '1 month' to January 31st will result in February 28th (or 29th). This is the logically correct behavior for calendar dates, but it can be surprising if you are expecting a strict 30-day increment.
Tips
To subtract time, simply use the minus operator (-) instead of the plus operator. For example, CURRENT_DATE - INTERVAL '1 day' will give you yesterday's date. This makes the syntax symmetrical and easy to remember.
For high-precision time arithmetic, you can include milliseconds or microseconds in your interval string, such as INTERVAL '1 second 500 milliseconds'. This is particularly useful for logging systems or high-frequency trading data where every fraction of a second matters.
If you find yourself writing INTERVAL '1 day' repeatedly, you can use the shorthand integer addition for dates. In PostgreSQL, adding an integer to a DATE type automatically adds that number of days. For example, CURRENT_DATE + 7 is equivalent to CURRENT_DATE + INTERVAL '7 days'. Note that this shorthand only works for the DATE type, not for TIMESTAMP.
When working with time zones, always use TIMESTAMPTZ (Timestamp with time zone). Adding an interval to a TIMESTAMPTZ ensures that Daylight Savings Time (DST) transitions are handled correctly based on the database's configured time zone, preventing "off-by-one-hour" bugs in your application logic.
Troubleshooting
If you encounter an error stating operator does not exist: date + integer, check your data types. While DATE + integer is supported, TIMESTAMP + integer is not. If your column is a timestamp, you must use the INTERVAL keyword: timestamp_column + INTERVAL '1 day'.
If your query is returning a timestamp when you wanted a date, look at your casting. Ensure the entire addition expression is wrapped in parentheses before the cast: (date_col + INTERVAL '1 day')::DATE. If you write date_col + INTERVAL '1 day'::DATE, the cast is applied to the interval, not the result, which will lead to a type mismatch error.
When you see an "invalid input syntax for type interval" error, double-check your string format. PostgreSQL is flexible, but it expects a recognizable pattern. Valid patterns include '1 day', '1 week', '1 month', '1 year', or '1 hour'. Ensure there are no typos inside the single quotes.
If the result of your date addition seems incorrect (e.g., the day is wrong), verify the time zone of your session. Use SHOW timezone; to see your current setting. If your server is in UTC but you are calculating dates for a user in EST, the date might flip over at 7:00 PM or 8:00 PM local time. Use AT TIME ZONE to normalize your dates before adding intervals.
Frequently Asked Questions
How do I add 3 hours to a timestamp?
Use the + operator with an interval of 3 hours: SELECT timestamp_column + INTERVAL '3 hours' FROM table_name;
Can I add multiple units of time at once?
Yes. You can combine units within a single interval string, such as INTERVAL '1 year 2 months 15 days', or add multiple interval expressions together: date + INTERVAL '1 day' + INTERVAL '2 hours'.
What is the difference between adding an integer and an interval?
Adding an integer (e.g., date + 5) only works on DATE types and always adds days. Adding an INTERVAL works on both DATE and TIMESTAMP types and allows for any time unit (hours, months, etc.).
How do I add a variable number of days from a column?
Multiply the column value by a one-day interval: date_column + (days_column * INTERVAL '1 day'). This converts the integer in the column into a duration that PostgreSQL can add to a date.
Does adding an interval handle leap years?
Yes, PostgreSQL's interval arithmetic is calendar-aware. Adding INTERVAL '1 year' to 2024-02-29 (a leap year) will result in 2025-02-28.
How do I subtract 15 minutes from a time?
Use the minus operator with the appropriate interval: SELECT now() - INTERVAL '15 minutes';