PostgreSQL 18 reference · Date and Time Functions
PostgreSQL GENERATE_SERIES() Function
Generate a set of rows from a start to a stop value with a step: numbers, or timestamps with an interval step.
PostgreSQL 18
Syntax
generate_series(start, stop, step)
Parameters
| Parameter | Description |
|---|---|
start, stop |
Numbers or timestamps; both ends are included. |
step optional |
The increment (an interval for timestamps). Default: 1 |
Return value
A set of rows.
Examples
SELECT n FROM generate_series(1, 10, 3) AS n;
Output
n ---- 1 4 7 10 (4 rows)
Run this on your computer — see “Get Started” for setup.
SELECT d::date AS day, count(o.order_id) AS orders
FROM generate_series(DATE '2026-02-26', DATE '2026-03-04', interval '1 day') AS d
LEFT JOIN orders AS o ON o.order_date = d::date
GROUP BY d
ORDER BY d;
Output
day | orders ------------+-------- 2026-02-26 | 0 2026-02-27 | 0 2026-02-28 | 1 2026-03-01 | 0 2026-03-02 | 0 2026-03-03 | 1 2026-03-04 | 0 (7 rows)
Run this on your computer — see “Get Started” for setup.
Related
UNNEST()· Expand an array into a set of rows, one per element. With several arrays it zips them into columns.DATE_TRUNC()· Truncate a timestamp or interval to a unit such as 'month', 'week' or 'hour', for grouping by period.- SQL Date Functions · tutorial