Postgresql – How to generate a time series in PostgreSQL

postgresqltime

If you're looking to generate a date series, see this question

Let's say I want to generate a series for every 5 minutes for 24 hours. How do I do that in PostgreSQL?

PostgreSQL can generate_series() from a timestamp, but not from time.

Is it better to pick an arbitrary timestamp, or is there another way to generate the series?

Best Answer

To optimize:

SELECT x::time
FROM   generate_series(timestamp '2000-01-01 00:00'
                     , timestamp '2000-01-02 00:00'
                     , interval  '5 min') t(x);

The date is irrelevant, so use arbitrary timestamp constants. The cast to time is very cheap.
This includes lower and upper bound, so we get '00:00' twice. Use '2000-01-01 23:59' as upper bound to get it once only.

Related: