Often in PostgreSQL you may want to add a specific number of minutes to a timestamp.
You can use the following basic syntax to do so:
SELECT *,
((start_time) + INTERVAL '10 MIN') AS ten_after
FROM emp;
This particular example adds 10 minutes to the timestamps in the start_time column of the table named emp and saves the results to a column named ten_after.
Note: To add a different number of minutes, simply change 10 MIN to a different number, e.g. 20 MIN.
The following example shows how to use this syntax in practice.
Example: How to Add Minutes to Timestamp in PostgreSQL
Suppose that we create the following table named emp that contains information about when various employees at some company started working on a specific task:
-- create table named 'emp'
CREATE TABLE emp (
empID INT PRIMARY KEY,
start_time TIMESTAMP
);
-- add rows to the table
INSERT INTO emp VALUES (0001, '2024-09-23 04:15:23');
INSERT INTO emp VALUES (0002, '2024-04-01 04:05:56');
INSERT INTO emp VALUES (0003, '2024-10-26 07:00:10');
INSERT INTO emp VALUES (0004, '2024-12-14 09:15:00');
INSERT INTO emp VALUES (0005, '2024-12-07 12:56:30');
-- view all rows in table
SELECT * FROM emp;
empid | start_time
-------+---------------------
1 | 2024-09-23 04:15:23
2 | 2024-04-01 04:05:56
3 | 2024-10-26 07:00:10
4 | 2024-12-14 09:15:00
5 | 2024-12-07 12:56:30
Suppose that we would like to create a new column that adds 10 minutes to each timestamp in the start_time column.
We can use the following syntax to do so:
SELECT *,
((start_time) + INTERVAL '10 MIN') AS ten_after
FROM emp;
empid | start_time | ten_after
-------+---------------------+---------------------
1 | 2024-09-23 04:15:23 | 2024-09-23 04:25:23
2 | 2024-04-01 04:05:56 | 2024-04-01 04:15:56
3 | 2024-10-26 07:00:10 | 2024-10-26 07:10:10
4 | 2024-12-14 09:15:00 | 2024-12-14 09:25:00
5 | 2024-12-07 12:56:30 | 2024-12-07 13:06:30
The resulting table contains a new column named ten_after that contains the timestamp from the start_time column with 10 minutes added to it.
You can use similar syntax to add a specific number of minutes to a timestamp column in your own table.
Additional Resources
The following tutorials explain how to perform other common tasks in PostgreSQL:
PostgreSQL: Calculate Timestamp Difference in Hours
PostgreSQL: Calculate Timestamp Difference in Minutes
PostgreSQL: Calculate Difference Between Dates in Days
