Generating A Series Of Numbers, Dates and Times In PostgreSQL
[PostgreSQL, Database]
One of the more powerful features of LINQ is the ability to generate sequences of values.
To get a list of numbers from 1 to 10, we do it like so:
var numbers = Enumerable.Range(1, 10).ToList();
numbers.ForEach(Console.WriteLine);
To get a list of all even numbers from 1 to 10, we do it like this:
var evenNumbers = Enumerable.Range(1, 10).Where(x => x % 2 == 0).ToList();
evenNumbers.ForEach(Console.WriteLine);
To get a list of all odd numbers from 1 to 10, we do it like this:
var oddNumbers = Enumerable.Range(1, 10).Where(x => x % 2 != 0).ToList();
oddNumbers.ForEach(Console.WriteLine);
You might wonder if it was possible to generate smaller increments, like 0.0 to 1.0 in steps of .05.
It is.
var smallNumbers = Enumerable.Range(0, 20).Select(x => x / 20.0).ToList();
smallNumbers.ForEach(Console.WriteLine);
Do you wish there were something similar in PostgreSQL?
There is!
You can use the generate_series function for such purposes.
There are two sets of these
- One that operates on numbers, specifically
integers,biginteger,andnumeric - One that operates on timestamps, with and without time zones
Numeric
The numeric one takes three parameters:
- Start
- Stop
- An optional step, which defaults to
1
The scenarios above would this be as follows:
-- 1 to 10
select *
from generate_series(1, 10);
-- odd numbers
select *
from generate_series(0, 10, 2);
-- even numbers
select *
from generate_series(1, 10, 2);
-- 0.0 to 1.0 in steps of .05
select *
from generate_series(0.0, 1.0, 0.05);
The results are as we’d expect.




This, we can see, is almost identical to how to tackle this problem in SQL Server, covered in the post “Generating A Series Of Numbers In SQL Server”.
Timestamp
The generate_series functions that operate with timestamps are in two flavours:
- Timezone aware
- Non-timezone aware
The timezone-aware version takes:
- Start timestamp with timezone
- End timestamp with timezone
- Interval
- Timezone
In this example, we are taking midnight, Nairobi time, and adding a day, from 1 January 2026 to 10 January 2026. We are further indicating we are using the Nairobi time zone (this matters when doing timezone-aware date computations!)
SELECT *
FROM generate_series('2026-01-01 00:00 +00:00'::timestamptz,
'2026-01-10 00:00 +00:00'::timestamptz,
'1 day'::interval, 'Africa/Nairobi');
This returns the following:

The non-timezone-aware version is very similar:
- Start timestamp
- End timestamp
- Interval
It looks like this:
SELECT *
FROM generate_series('2026-01-01 00:00 +00:00'::timestamp,
'2026-01-10 00:00 +00:00'::timestamp,
'1 day');
The results are as follows:

Note the difference here: the results do not include any time zone information.
TLDR
You can generate any series of numbers, dates, and times in PostgreSQL using the generate_series function.
The code is in my GitHub.
Happy hacking!