如果您使用yum
or安装了 pgagent apt-get
,它应该已经为您创建了 systemd 文件。例如,在 RHEL 7(本质上是 CentOS 7)上,您可以安装 PostgreSQL 12,然后安装 pgagent
sudo yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install postgresql12
sudo yum install postgresql12-server
sudo yum install pgagent_12.x86_64
这会将 PostgreSQL/var/lib/pgsql/12
和 pgagent_12 安装到/usr/bin/pgagent_12
此外,它还会在以下位置创建一个 systemd 文件/usr/lib/systemd/system/pgagent_12.service
查看服务的状态systemctl status pgagent_12
将其配置为自动启动,然后启动它,使用:
sudo systemctl enable pgagent_12
sudo systemctl start pgagent_12
很可能身份验证会失败,因为默认的 .service 文件有
ExecStart=/usr/bin/pgagent_12 -s ${LOGFILE} hostaddr=${DBHOST} dbname=${DBNAME} user=${DBUSER} port=${DBPORT}
确认sudo tail /var/log/pgagent_12.log
将显示
Sat Oct 12 19:35:47 2019 WARNING: Couldn't create the primary connection [Attempt #1]
Sat Oct 12 19:35:52 2019 WARNING: Couldn't create the primary connection [Attempt #2]
Sat Oct 12 19:35:57 2019 WARNING: Couldn't create the primary connection [Attempt #3]
Sat Oct 12 19:36:02 2019 WARNING: Couldn't create the primary connection [Attempt #4]
为了解决问题,我们需要创建一个在服务启动时可以访问的 .pgpass 文件。一、停止服务
sudo systemctl stop pgagent_12
检查服务文件并less /usr/lib/systemd/system/pgagent_12.service
显示它有
User=pgagent
Group=pgagent
此外,/etc/pgagent/pgagent_12.conf
有
DBNAME=postgres
DBUSER=postgres
DBHOST=127.0.0.1
DBPORT=5432
LOGFILE=/var/log/pgagent_12.log
检查/etc/passwd
文件以查找 pgagent 用户及其主目录:grep "pgagent" /etc/passwd
pgagent:x:980:977:pgAgent Job Schedule:/home/pgagent:/bin/false
因此,我们需要创建一个 .pgpass 文件/home/pgagent/.pgpass
来定义 postgres 用户的密码
sudo su -
mkdir /home/pgagent
chown pgagent:pgagent /home/pgagent
chmod 0700 /home/pgagent
echo "127.0.0.1:5432:postgres:postgres:PasswordGoesHere" > /home/pgagent/.pgpass
chown pgagent:pgagent /home/pgagent/.pgpass
chmod 0600 /home/pgagent/.pgpass
目录和文件权限很重要。如果您遇到问题,您可以通过编辑服务文件/usr/lib/systemd/system/pgagent_12.service
来启用调试日志记录,以通过更新ExecStart
命令来 启用调试日志记录-l 2
ExecStart=/usr/bin/pgagent_12 -l 2-s ${LOGFILE} hostaddr=${DBHOST} dbname=${DBNAME} user=${DBUSER} port=${DBPORT}
更改 .service 文件后,必须重新加载内容sudo systemctl daemon-reload
(如果您忘记了这个要求,systemd 会通知您)。
继续启动/停止服务并检查/var/log/pgagent_12.log
最终,它将正常启动并sudo systemctl status pgagent_12
显示
● pgagent_12.service - PgAgent for PostgreSQL 12
Loaded: loaded (/usr/lib/systemd/system/pgagent_12.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2019-10-12 20:18:18 PDT; 13s ago
Process: 6159 ExecStart=/usr/bin/pgagent_12 -s ${LOGFILE} hostaddr=${DBHOST} dbname=${DBNAME} user=${DBUSER} port=${DBPORT} (code=exited, status=0/SUCCESS)
Main PID: 6160 (pgagent_12)
Tasks: 1
Memory: 1.1M
CGroup: /system.slice/pgagent_12.service
└─6160 /usr/bin/pgagent_12 -s /var/log/pgagent_12.log hostaddr=127.0.0.1 dbname=postgres user=postgres port=5432
Oct 12 20:18:18 prismweb3 systemd[1]: Starting PgAgent for PostgreSQL 12...
Oct 12 20:18:18 prismweb3 systemd[1]: Started PgAgent for PostgreSQL 12.