Coder Social home page Coder Social logo

pg_cron_poc's Introduction

Introduction

This is a PoC regarding how to run pg_cron based cron jobs within a postgres container.

Get started

# Build a new docker image, based on the offical postgres image, with 
docker build -t atwayne/pgcron_poc .

# start a postgres instance
docker run --name local-pgcron-poc -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d atwayne/pgcron_poc

Example

  1. Create a new table events with create-table.sql
  2. Create cron jobs with add-cron-jobs.sql

With the example scripts above, there will be a table events with two columns:

Column Type
event_time timestamp with time zone
event_id uuid

Two cron jobs have been created, the first one will insert a random row with the current timestamp, meanwhile the second one will delete all rows that were created two minutes ago.

Give it a few minutes and check the table with

SELECT * FROM events

It's expected that there will be 2-3 rows in the table created in the last 3 minutes.

To stop the example scheduling jobs

SELECT cron.unschedule('cron_insert_every_minute');
SELECT cron.unschedule('cron_cleanup_every_minute');

Links

  1. pg_cron https://github.com/citusdata/pg_cron

  2. crontab.guru https://crontab.guru/

  3. Credit to a StackOverflow answer https://stackoverflow.com/a/69495449/553073

pg_cron_poc's People

Contributors

atwayne avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.