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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

generate_series(start, stop, step)

Parameters

ParameterDescription
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

Numbers
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.

Fill gaps in a daily report
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.

Was this page helpful?