Skip to content

Instantly share code, notes, and snippets.

@peterneave
Last active November 10, 2023 06:51
Show Gist options
  • Save peterneave/83cefce2a081add244ad7dc1c53bc0c3 to your computer and use it in GitHub Desktop.
Save peterneave/83cefce2a081add244ad7dc1c53bc0c3 to your computer and use it in GitHub Desktop.
Install pgAgent on Postgres 10 (Debian Linux)

Install pgAgent on Postgres 10 (Debian Linux)

This assumes you will have pgAgent running on the same machine as your database.

Terminal

  1. Install pgAgent via package manager
sudo apt update
sudo apt install pgagent

We are going to create a user to use pgAgent called pgagent due to security concerns we will run as the postgres user on the server and as the pgagent on the database.

  1. Create .pgpass file.

As we are running as postgres then put it in $HOME$ for postgres (/var/lib/postgresql)

sudo su - postgres
echo localhost:5432:*:pgagent:securepassword >> ~/.pgpass
chmod 600 ~/.pgpass
chown postgres:postgres /var/lib/postgresql/.pgpass
  1. Setup Logging directory

Setup directory for logging

mkdir /var/log/pgagent
chown -R postgres:postgres /var/log/pgagent
chmod g+w /var/log/pgagent

Postgres

  1. Setup pgAgent on Postgres (maintenance) Database
//use Postgres database
//This creates the pgagent schema. Dropping this extension will remove this schema and any jobs you have created.
CREATE EXTENSION pgagent;

CREATE USER "pgagent" WITH
  LOGIN
  NOSUPERUSER
  INHERIT
  NOCREATEDB
  NOCREATEROLE
  NOREPLICATION
  encrypted password 'securepassword';

GRANT USAGE ON SCHEMA pgagent TO pgagent;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA pgagent TO pgagent;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA pgagent TO pgagent;

Testing

  1. Test pgAgent connections

In a separate terminal, tail -f /var/log/postgresql/postgresql-10-main.log to watch Postgres logs

Postgres connection

Test connection to database in terminal with

psql -h localhost -d yourdatabase -U pgagent

pgAgent

sudo su - postgres
/usr/bin/pgagent -f -l 2 host=localhost port=5432 user=pgagent dbname=postgres

Review logs for errors.

pgAgent load on boot in Debian

  1. Setup pgAgent to load on reboot

Create a config file and save to /etc/pgagent.conf

#/etc/pgagent.conf
DBNAME=postgres
DBUSER=pgagent
DBHOST=localhost
DBPORT=5432
# ERROR=0, WARNING=1, DEBUG=2
LOGLEVEL=1
LOGFILE="/var/log/pgagent/pgagent.log"

Loading with Systemd

Based on the Centos pgagent.service file found at https://centos.pkgs.org/7/postgresql-12-x86_64/pgagent_12-4.0.0-4.rhel7.x86_64.rpm.html

/usr/lib/systemd/system/pgagent.service

[Unit]
Description=PgAgent for PostgreSQL
After=syslog.target
After=network.target

[Service]
Type=forking

User=postgres
Group=postgres

# Location of the configuration file
EnvironmentFile=/etc/pgagent.conf

# Where to send early-startup messages from the server (before the logging
# options of pgagent.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog

# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000

ExecStart=/usr/bin/pgagent -s ${LOGFILE}  -l ${LOGLEVEL} host=${DBHOST} dbname=${DBNAME} user=${DBUSER} port=${DBPORT}
KillMode=mixed
KillSignal=SIGINT

Restart=on-failure

# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300

[Install]
WantedBy=multi-user.target

Start Service

sudo -i
systemctl daemon-reload
systemctl disable pgagent
systemctl enable pgagent
systemctl start pgagent

Auto Rotate Logs

  1. Enable auto rotation of logs
#/etc/logrotate.d/pgagent
/var/log/pgagent/*.log {
       weekly
       rotate 10
       copytruncate
       delaycompress
       compress
       notifempty
       missingok
       su root root
}

Test a log rotation with

logrotate -f /etc/logrotate.d/pgagent

Source

@gbryant200
Copy link

gbryant200 commented Apr 12, 2019

I had to use these commands to get it to auto-start properly on Ubuntu 18.04
sudo update-rc.d pgagent.sh defaults
sudo update-rc.d pgagent.sh enable

@jeffrobdun
Copy link

Thank you for creating this! Huge help in getting pgAgent setup

@adburne
Copy link

adburne commented Feb 13, 2023

Thanks, great help, works 100%

@DmitryBaranov77
Copy link

Thanks a lot man <3

@YeeY23
Copy link

YeeY23 commented Nov 10, 2023

Hello everyone, I am getting error: "psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: could not initiate GSSAPI security context: Unspecified GSS failure. Minor code may provide more information: Ticket expired
connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: password authentication failed for user "pgAgent"
" , when I run psql -h localhost -d yourdatabase -U pgagent. Do I need to set up GSSAPI authentication to run pgAgent on postgresql Redhat linux? Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment