DEV Community

Beaver Bridge
Beaver Bridge

Posted on

Postgresql for loop

Postgres에서 날짜 목록을 생성한 후 테이블에 하나씩 넣기 위해 loop를 돌리려면 이렇게 하면 된다.

do $$
declare
  dates date;
begin
  for dates in SELECT generate_series('2023-07-01'::DATE, '2023-07-31'::DATE, '1 day'::interval)::DATE loop
    insert into my_table(user_id, date, type) values ('my_user_id',dates, 'OFF');
  end loop;
end;
$$;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay