pg_cron icon indicating copy to clipboard operation
pg_cron copied to clipboard

Multiple database

Open sm2017 opened this issue 4 years ago • 11 comments

# add to postgresql.conf:
shared_preload_libraries = 'pg_cron'
cron.database_name = 'postgres'

How can I use pg_cron for multiple database , Assume I have foo and bar databases

sm2017 avatar Oct 13 '19 06:10 sm2017

@marcocitus Can you help me?

sm2017 avatar Oct 13 '19 15:10 sm2017

@sm2017 , hi ! From source code i can see that cron.database_name is for single database only:

https://github.com/citusdata/pg_cron/blob/master/src/pg_cron.c#L149

anikin-aa avatar Oct 15 '19 05:10 anikin-aa

@sm2017 you can do Create extension pg_cron per logical database and it will work.

cron.database_name is used as databases for metadata tables: https://github.com/citusdata/pg_cron#setting-up-pg_cron

By default, the pg_cron background worker expects its metadata tables to be created in the "postgres" database.

anikin-aa avatar Oct 15 '19 05:10 anikin-aa

@anikin-aa

[2019-10-15 09:29:26] [P0001] ERROR: can only create extension in database postgres
[2019-10-15 09:29:26] Detail: Jobs must be scheduled from the database configured in cron.database_name, since the pg_cron background worker reads job descriptions from this database.
[2019-10-15 09:29:26] Hint: Add cron.database_name = 'postgres' in postgresql.conf to use the current database.
[2019-10-15 09:29:26] Where: PL/pgSQL function inline_code_block line 4 at RAISE

sm2017 avatar Oct 15 '19 06:10 sm2017

hm, that is strange, i thought that is possible to create pg_cron in multiple databases, but it's actually not.

anikin-aa avatar Oct 15 '19 06:10 anikin-aa

https://github.com/citusdata/pg_cron/issues/36

anikin-aa avatar Oct 15 '19 06:10 anikin-aa

Is there any update?

dereisele avatar Sep 24 '20 16:09 dereisele

I've discovered, that you can do queries on other databases with version 1.2., like in https://github.com/citusdata/pg_cron/tree/beta-2.0#advanced-usage But you can still only use one scheduling table / one db with the cron.schedule() function

dereisele avatar Sep 28 '20 11:09 dereisele

Actually if you want to schedule jobs in multiple databases here is simple setup:

CREATE DATABASE pg_cron

postgresql.conf:

shared_preload_libraries = 'pg_cron'
cron.database_name = 'pg_cron' # Use separate database for pg_cron

Suppose you have 2 separate databases db1 and db2, in database pg_cron in table cron.jobs there is a field database in which you can specify on which db is job executed:

image

hazardland avatar Sep 30 '20 18:09 hazardland

But how do I submit a job as a user in db1 (e.g. public) using this setup?

just-doit avatar Nov 11 '20 09:11 just-doit

While pg_cron is still managed though a single database, with 1.4 jobs can be scheduled to run in other databases via cron.schedule_in_database. Additionally, existing jobs can be changed to run in a different db with cron.alter_job.

I guess this issue can be closed now.

carobme avatar Mar 17 '22 09:03 carobme