Skip to content

generate_series ​

Generates an inclusive series of values. Use it in FROM to produce a one-column table named generate_series.

Syntax ​

text
FROM generate_series(start, stop [, step [, timezone]])

Arguments ​

  • start — The first numeric or calendar value.
  • stop — The inclusive bound; it is returned when the chosen step reaches it exactly.
  • step — The increment. Numeric series default to 1. Timestamp bounds require an explicit native interval or quantity-and-unit step. Zero raises an error.
  • timezone — An optional timezone for supported calendar series.

A NULL input or a step pointing away from the stop produces no rows. generate_series is a table function and cannot be called as an ordinary scalar expression.

Returned columns ​

One column, generate_series, containing one generated value per row in series order.

Examples ​

Inclusive ascending series ​

sql
SELECT generate_series AS number
FROM generate_series(1, 3)
FORMAT csv;
text
1
2
3

Descending with an explicit step ​

sql
SELECT generate_series AS number
FROM generate_series(5, 1, -2)
FORMAT csv;
text
5
3
1

A direction that cannot reach the bound ​

sql
SELECT count(*)
FROM generate_series(1, 5, -1)
FORMAT csv;
text
0

See the date and time functions for calendar values and utility functions for the full series input contract.