【问题标题】:using pg_cron extension on Cloud SQL在 Cloud SQL 上使用 pg_cron 扩展
【发布时间】:2022-07-05 17:46:16
【问题描述】:

我正在尝试使用 pg_cron 在 Postgres Cloud SQL 实例中的多个 DB 上安排对存储过程的调用。

不幸的是,pg_cron 似乎只能在 postgres DB 上创建

当我尝试在不同于 postgres 的数据库上使用 pg_cron 时,我收到以下消息:

CREATE EXTENSION pg_cron;
 
ERROR: can only create extension in database postgres
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. Hint: Add cron.database_name = 
'mydb' in postgresql.conf to use the current database. 
Where: PL/pgSQL function inline_code_block line 4 at RAISE 

Query = CREATE EXTENSION pg_cron;

...我认为我无法访问 Cloud SQL 中的 postgresql.conf ...还有其他方法吗? 也许我可以使用 postgres_fdw 来实现我的目标?

谢谢,

【问题讨论】:

    标签: postgresql google-cloud-platform google-cloud-sql pg-cron


    【解决方案1】:

    无需编辑任何文件。您所要做的就是设置cloudsql.enable_pg_cron 标志(参见guide),然后在postgres 数据库中创建扩展。

    您需要登录 postgres 数据库,而不是您用于应用程序的数据库。对我来说,这只是用“postgres”替换我的应用程序数据库的名称,例如

    psql -U<username> -h<host ip> -p<port> postgres
    

    然后只需运行 create extension 命令,就会出现 cron.job 表。这是我几分钟前在我们的 cloudsql 数据库中所做的。我正在使用 cloudsql 代理访问远程数据库:

    127.0.0.1:2345 admin@postgres=> create extension pg_cron;
    CREATE EXTENSION
    Time: 268.376 ms
    127.0.0.1:2345 admin@postgres=> select * from cron.job;
     jobid | schedule | command | nodename | nodeport | database | username | active | jobname 
    
    -------+----------+---------+----------+----------+----------+----------+--------+---------
    (0 rows)
    
    Time: 157.447 ms
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 2018-06-12
      • 2022-11-10
      • 2021-10-02
      • 2021-10-28
      • 2014-05-28
      相关资源
      最近更新 更多