Working with pgAgent
PgAgent is scheduling agent that can be managed with PgAdmin. This tutorial will show you how to configure PgAgent in aidboxdb.
Introduction
Currently available only in aidboxdb:13.2 or later.
PgAgent provides an ability to run user-defined jobs on the Postgres database. It runs as a separate process on a system with Postgres databases.
Aidboxdb comes with PgAgent preinstalled.
By default, aidboxdb container doesn't start the PgAgent daemon. To start the daemon you need to specify PGAGENT_ENABLED
variable. If the variable is specified then the daemon will be started on container startup.
PgAgent wouldn't run if Postgres instance is run as a replica (PG_ROLE
is not equal to 'replica'
) because it may lead to unexpected behavior.
PgAgent will run on every instance of Postgres master, which may lead to unexpected behavior.
By default PgAgent process in aidboxdb will use a database specified in POSTGRES_DB
environmental variable and will run as a user which is specified in POSTGRES_USER
variable (postgres
by default).
If you want to specify a dedicated user for PgAgent (for example to limit PgAgent privileges), you need to specify PGAGENT_USER
and PGAGENT_PASSWORD
variables.
If you want to use a dedicated database to store PgAgent service data, you can specify it in PGAGENT_DB
variable. But you have to create the database and extension manually before starting PgAgent.
Note: if you want to use a dedicated user or database you need to create them manually before enabling PgAgent in your aidboxdb configuration.
Don't forget to create the PgAgent extension if your choose to use a dedicated database.
To create the extension run CREATE EXTENSION pgagent;
There are some more options available to configure PgAgent:
You can specify log level with PGAGENT_LOG_LEVEL
and you can specify a file where PgAagent logs are written in PGAGENT_LOG_FILE_PATH
.
You can see more info on configuring aidboxdb.
Step-by-step example with docker
In this example we will setup simple pgagent
job with dedicated pgagent database and user.
Configuring PgAgent
First, we will need to create an initial docker-compose
configuration:
Start container:
Now let's create a database, extension, test table, and user.
Connect to postgres with:
And run following commands:
After you've run commands updatedocker-compose.yaml
file:
And re-create container:
Let's exec into the container and see what is going on.
First let's run pgrep command
:
You can use ps aux
command if you want to see more details on pgagent
arguments:
Let's check pgagent
logs:
Don't worry if you see WARNING: Couldn't create the primary connection [Attempt #2]
– postgres takes some time to start and pgagent will reconnect as soon as postgres is ready to accept connections.
Defining a job via PgAdmin
Refer to https://www.pgadmin.org/ for pgAdmin and pgAgent documentation.
Now when pgagent is up and running we can define some jobs and see if they're actually scheduled.
Test if jobs are running
Let's connect into postgres to see if jobs are actually running.
We can check scheduled jobs, last and next time their run in pgagent.pga_job
table.
Let's test if job was run in our test
table:
If everything was successfull you will see new records in the table.
Summary
pgagent
is a tool to run various jobs on your postgres database.pgagent
runs only ifPGAGENT_ENABLED
variable is present.You can configure
pgagent
using several variables described here.
If you have any questions on how to configure pgagent
feel free to contact us.
Last updated