You can use the generate_series
function to do that:
psql# select generate_series('2014-01-01 16:00'::timestamp, '2014-01-01 20:00'::timestamp, '1 hour'); generate_series --------------------- 2014-01-01 16:00:00 2014-01-01 17:00:00 2014-01-01 18:00:00 2014-01-01 19:00:00 2014-01-01 20:00:00 (5 rows)
And then you can use:
SELECT
t AS starttime,
t + INTERVAL '1 hour' as endtime
FROM
GENERATE_SERIES(
'2014-01-01 16:00'::TIMESTAMP,
'2014-01-01 20:00'::TIMESTAMP,
'1 hour'
) AS t
To get the start and end times.
Alternatively, to just get the times, you can use:
SELECT
'08:00'::time + (t || ' hours')::interval as starttime,
'08:00'::time + ((t + 1)::text || ' hours')::interval as endtime
FROM
GENERATE_SERIES(0, 12) AS t