在装有 PostgreSQL 14 的 Red Hat8上安装 `pg_cron`

要在装有 PostgreSQL 14 的 Red Hat、CentOS、Fedora 或 Amazon Linux 上安装 pg_cron,请遵循以下步骤。这些步骤假定您已经安装了 PostgreSQL Global Development Group (PGDG) 的 PostgreSQL 版本。

安装 pg_cron 扩展

  1. 使用 yum 安装 pg_cron 扩展:
    sudo yum install -y pg_cron_14
    

设置 pg_cron

  1. 要在 PostgreSQL 启动时启动 pg_cron 后台进程,需要在 postgresql.conf 文件中添加 pg_cronshared_preload_libraries 配置:

    # required to load pg_cron background worker on start-up
    shared_preload_libraries = 'pg_cron'
    
  2. 默认情况下,pg_cron 后台进程的元数据表将在 “postgres” 数据库中创建。您可以通过在 postgresql.conf 中设置 cron.database_name 配置参数来更改此设置:

    # optionally, specify the database in which the pg_cron background worker should run (defaults to postgres)
    cron.database_name = 'postgres'
    
  3. 之前 pg_cron 只能使用 GMT 时间,但现在您可以通过在 postgresql.conf 中设置 cron.timezone 来适应您的时区。例如,使用中国标准时间 (PRC):

    # optionally, specify the timezone in which the pg_cron background worker should run (defaults to GMT). E.g:
    cron.timezone = 'PRC'
    
  4. 重启 PostgreSQL 以使更改生效。

创建 pg_cron 扩展

  1. 重启 PostgreSQL 后,您可以使用 CREATE EXTENSION pg_cron 创建 pg_cron 函数和元数据表:
    -- run as superuser:
    CREATE EXTENSION pg_cron;
    
    -- optionally, grant usage to regular users:
    GRANT USAGE ON SCHEMA cron TO marco;
    

确保 pg_cron 可以启动作业

  1. 默认情况下,pg_cron 使用 libpq 打开到本地数据库的新连接,这需要在 pg_hba.conf 中允许。您可能需要为从 localhost 来的连接启用信任认证,或者可以将密码添加到 .pgpass 文件中,libpq 将在打开连接时使用此文件。

使用后台工作进程调度作业

  1. pg_cron 还可以配置为使用后台工作进程。在这种情况下,同时进行作业的数量受 max_worker_processes 设置的限制,因此您可能需要提高该设置:
    # Schedule jobs via background workers instead of localhost connections
    cron.use_background_workers = on
    # Increase the number of available background workers from the default of 8
    max_worker_processes = 20
    

查看作业运行详情

  1. 您可以在 cron.job_run_details 中查看正在运行和最近完成的作业运行状态:
    select * from cron.job_run_details order by start_time desc limit 5;
    

确保在进行这些更改时,您已经备份了相应的配置文件,并在必要时咨询了经验丰富的数据库管理员。

相关推荐

  1. 装有 PostgreSQL 14 Red Hat8安装 `pg_cron`

    2023-12-11 19:52:01       27 阅读
  2. redhat7/8平台部署ELK7.17.18技术方案

    2023-12-11 19:52:01       50 阅读
  3. PostgreSQL】- 1.1 Debian 12 安装 PostgreSQL 15

    2023-12-11 19:52:01       23 阅读
  4. k8s安装hostPath方式存储PostgreSQL15

    2023-12-11 19:52:01       42 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-11 19:52:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-11 19:52:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-11 19:52:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-11 19:52:01       20 阅读

热门阅读

  1. 基于粒子群算法求解充电桩布局

    2023-12-11 19:52:01       42 阅读
  2. 如何编写编写干净的 PHP 代码

    2023-12-11 19:52:01       36 阅读
  3. 抖音视频评论区采集软件使用教程

    2023-12-11 19:52:01       74 阅读
  4. 智能家居IC

    2023-12-11 19:52:01       43 阅读
  5. 实验七 子网的划分

    2023-12-11 19:52:01       32 阅读
  6. CCSDS标准中使用的9/整数小波变换(Matlab实现)

    2023-12-11 19:52:01       36 阅读
  7. pixmap must be grayscale or rgb to write as png

    2023-12-11 19:52:01       42 阅读
  8. 【东枫科技 招聘】实习:无线通信工程

    2023-12-11 19:52:01       33 阅读
  9. mysql 批量修改优化方案

    2023-12-11 19:52:01       32 阅读
  10. Boost.SafeNumerics模块测试:constexpr转换编程

    2023-12-11 19:52:01       32 阅读