Fix “ERROR:  step size cannot equal zero” When Creating a PostgreSQL Series

If you’re getting an error that reads “step size cannot equal zero” when creating a series with the generate_series() function in PostgreSQL, it’s because you’re using a step size of 0 (i.e. zero).

The generate_series() function doesn’t accept a zero step size.

To fix this issue, either use a non-zero step size, or remove the step size altogether (so that the default step is used).

Example of Error

Here’s an example of code that produces the error:

SELECT * FROM generate_series( 1, 5, 0 );

Result:

ERROR:  step size cannot equal zero

Here, the step size (i.e. the third argument) is zero and so we get the error.

Solution

To fix the problem either change the step size to a non-zero amount, or remove it altogether.

Here’s an example of changing the step size:

SELECT * FROM generate_series( 1, 5, 2 );

Result:

 generate_series 
-----------------
               1
               3
               5
(3 rows)

This time I specified a step size of 2 and it worked without error.

Here’s an example of removing the step size altogether:

SELECT * FROM generate_series( 1, 5 );

Result:

 generate_series 
-----------------
               1
               2
               3
               4
               5
(5 rows)

By removing the step size, it used the default step size of 1 and returned the result without error.