【问题标题】:Cron job not able to establish connection with postgresql databaseCron 作业无法与 postgresql 数据库建立连接
【发布时间】:2020-02-05 09:07:09
【问题描述】:

我尝试使用以下 shell 脚本运行 cron 作业。

#!/bin/bash

PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

gsutil cp gs://api-bucket/order_status.csv order_status.csv

psql -d  apidb -U apidb  << EOF

DELETE FROM apidbgermany.order_status;
\copy apidb.order_status from '/u01/app/postgres/data/silver01/order_status.csv'  delimiter ',' CSV ;
EOF

我的 cronjob 看起来像这样。

* * * * * /u01/app/postgres/data/silver01/order_status.sh &>/tmp/mycommand.log

但是,如果我在命令行中手动执行 shell 文件,效果会很好。

【问题讨论】:

  • 是本地 cron 还是远程?你能分享一下作业错误吗?
  • @RajVerma 它在远程 Linux 服务器中。我已经分享了图片中的错误

标签: shell unix cron psql


【解决方案1】:

要访问远程服务器,您应该以这种方式执行命令psql

psql -h postgress_host -d  apidb -U apidb  << EOF
....

否则psql 尝试连接本地服务器。当创建 cron 脚本时,总是使用可执行文件的填充路径

/usr/bin/psql ....

(例如)

并添加为脚本行的第二行,例如:

source ~/.bash_profile

source ~/.bashrc

取决于您的环境设置在哪里

【讨论】:

  • 实际上是我在安装了 postgres 的远程服务器上的 cron 工作。不在我的本地机器上
  • @PraveenkumarBeedanal,好的,但是错误说脚本无法连接到实例。您是否以 PostgreSQL 用户身份执行了 cron?
  • 我已经在 postgresql 用户中创建了 shell 脚本文件。就像我使用了'sudo su'然后su - postgres,我在那里创建了文件
  • @PraveenkumarBeedanal,运行 cron 的是 postgres 吗?并将编辑我的答案以添加更多内容
  • 成功了,非常感谢。我也添加了端口号,因为我的端口是 60901
【解决方案2】:

另外,请直接使用 pg_cron 扩展,它将为您解决此问题。您可以从 postgres 内部设置 crons。 pg_cron extension

【讨论】:

  • 好吧,你的意思是我必须使用 pg_cron insde '[postgres@dapih1pgs62 silver01]' postgres 对吗?
  • 文档包含所有内容,只需按照那里的说明进行操作,但如果这不是您的障碍,则必须安装该扩展程序。
  • 实际上它是要求 postgres 密码来安装它,但在此之前我使用过 'su -postgres'
  • 关注github.com/citusdata/pg_cron/issues/21 或仅关注“安装pg_cron”部分:github.com/citusdata/pg_cron
【解决方案3】:

该错误消息询问 PostgreSQL 服务器是否正在运行,因为它不是,它正在生成该错误消息。我必须用来消除该错误的方法是运行以下命令:“sudo pg_ctlcluster (PG version number) main start”或 sudo pg_cltcluster 11 main start”,因为我使用的是 PG 版本 11。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2015-02-15
    • 2016-03-11
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多