如何创建 cron 作业来运行 postgres SQL 函数? [英] How do I create a cron job to run an postgres SQL function?
问题描述
我认为我需要做的就是:
I assume that all I need to do is to:
创建一个 sql 文件,例如nameofsqlfile.sql 内容:
Create an sql file e.g. nameofsqlfile.sql contents:
执行proc_my_sql_funtion();
perform proc_my_sql_funtion();
将此作为 cron 作业执行.
Execute this as a cron job.
但是,我不知道我需要编写哪些命令才能将此 cron 作业作为指定主机、端口、数据库、用户和用户的 postgres 函数执行.他的密码……?
However, I don't know the commands that I'd need to write to get this cron job executed as a postgres function for a specified host,port,database, user & his password...?
推荐答案
您只需要将 cronjob 视为在指定的时间或日期运行 shell 命令即可.
You just need to think of cronjob as running a shell command at a specified time or day.
所以你的第一份工作是弄清楚如何运行你的 shell 命令.
So your first job is to work out how to run your shell command.
psql --host host.example.com --port 12345 --dbname nameofdatabase --username postgres < my.sql
然后您可以将其添加到您的 crontab 中(我建议您使用 crontab -e
以避免破坏事物)
You can then just add this to your crontab (I recommend you use crontab -e
to avoid breaking things)
# runs your command at 00:00 every day
#
# min hour wday month mday command-to-run
0 0 * * * psql --host host.example.com --port 12345 --dbname nameofdatabase < my.sql
这篇关于如何创建 cron 作业来运行 postgres SQL 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!