How to generate time series data in PostgreSQL

Generate_series is an example of set returning function

Generate_series used to generate time series data and extremely useful

generate_series ( start, stop[, step] ) → set of values

start and stop are mandatory

step is optional

When step is positive, zero rows are returned if start is greater than stop. Conversely, when step is negative, zero rows are returned if start is less than stop. Zero rows are also returned if any input is NULL. It is an error for step to be zero

generate_series ( start integer, stop integer [, step integer ] ) → set of integer
generate_series (start bigint, stop bigint[, step bigint]) → set of bigint
generate_series ( start numeric, stop numeric [, step numeric ] ) → set of numeric
Generates a series of values from start to stop, with a step size of step. step defaults to 1
generate_series ( start timestamp, stop timestamp, step interval ) → set of timestamp
generate_series ( start timestamp with time zone, stop timestamp with time zone, step interval ) → set of timestamp with time zone
Generates a series of values from start to stop, with a step size of step
Generate_Series

Examples:-

postgres=# select * from generate_series(1,5);
 generate_series 
-----------------
               1
               2
               3
               4
               5
(5 rows)

--with alias
postgres=# select a from generate_series(1,5) as a;
 generate_series 
-----------------
               1
               2
               3
               4
               5
(5 rows)

postgres=# select * from generate_series(1,5,1);
 generate_series 
-----------------
               1
               2
               3
               4
               5
(5 rows)

postgres=# select * from generate_series(1,5,2);
 generate_series 
-----------------
               1
               3
               5
(3 rows)

postgres=# select * from generate_series(5,1,-1);
 generate_series 
-----------------
               5
               4
               3
               2
               1
(5 rows)

postgres=# select * from generate_series(4,3);
 generate_series 
-----------------
(0 rows)

postgres=# select * from generate_series(1.0,5.0,0.5);
 generate_series 
-----------------
             1.0
             1.5
             2.0
             2.5
             3.0
             3.5
             4.0
             4.5
             5.0
(9 rows)

-- printing 7 days starting from tomorrow
postgres=# select (current_date + a) as dates from generate_series(1,7,1) as a;
   dates    
------------
 2022-08-25
 2022-08-26
 2022-08-27
 2022-08-28
 2022-08-29
 2022-08-30
 2022-08-31
(7 rows)

--print all timestamps with 2 hour difference 
postgres=# select * from generate_series('2022-07-24 00:00'::timestamp,'2022-07-25 23:59'::timestamp,'2 hours');
   generate_series   
---------------------
 2022-07-24 00:00:00
 2022-07-24 02:00:00
 2022-07-24 04:00:00
 2022-07-24 06:00:00
 2022-07-24 08:00:00
 2022-07-24 10:00:00
 2022-07-24 12:00:00
 2022-07-24 14:00:00
 2022-07-24 16:00:00
 2022-07-24 18:00:00
 2022-07-24 20:00:00
 2022-07-24 22:00:00
 2022-07-25 00:00:00
 2022-07-25 02:00:00
 2022-07-25 04:00:00
 2022-07-25 06:00:00
 2022-07-25 08:00:00
 2022-07-25 10:00:00
 2022-07-25 12:00:00
 2022-07-25 14:00:00
 2022-07-25 16:00:00
 2022-07-25 18:00:00
 2022-07-25 20:00:00
 2022-07-25 22:00:00
(24 rows)

Below query will print all dates in 2022 chronologically

select to_char(a,’yyyy-mm-dd’) from generate_series(‘2022-01-01′::date,’2022-12-31′::date,’1 day’) as a;

Using generate_series we can create lot of time series synthetic data