📚 PGagent enabled¶
🔍 Prepare:¶
## OS and Software environment:
Environment name Version OS Rocky Linux 9.x DB PostgreSQL 16.X
### 1. Install pgagent_16 package
http_proxy="http://10.55.13.139:8080"
export http_proxy
https_proxy="http://10.55.13.139:8080"
export https_proxy
cat << EOF > /etc/yum.repos.d/pgdg_rocky-16.repo
[pgdg16]
name=PostgreSQL 16 for RHEL/CentOS
baseurl=https://download.postgresql.org/pub/repos/yum/16/redhat/rhel-\$releasever-\$basearch
enabled=1
gpgcheck=0
EOF
yum clean all
yum makecache
yum list |grep pgagent
yum install -y pgagent_16.x86_64
chown -R postgres. /usr/pgsql-16
### 2. Create pgagent extension on postgres DB
### 3. to make sure pgAgent jobs can run the setup jobs. You need to start pgagent service on server. Grant privilege to user
grant all on schema pgagent to user;
grant all on all tables in schema pgagent to user;
grant all on all sequences in schema pgagent to user;
grant all on all functions in schema pgagent to user;
### 4. Check IPv4 connections
### 5. Setting pgagent
- 5.1 Set pgagent conf
vi /etc/pgagent/pgagent_16.conf
# pgagent_16_conf
DBNAME=postgres
DBUSER=postgres
DBHOST=10.37.39.223
DBPORT=5435
# if can not connection ,use log debug
LOGFILE=/var/log/pgagent_16.log
If you have multi Cluster , you shou create copy pgagent_16.conf and service (example pgagent_16_SID.conf)
- 5.2 start pgagent_16.service
vi /usr/lib/systemd/system/pgagent_16.service
#check and modify the row host => hostaddr
EnvironmentFile=/etc/pgagent/pgagent_16.conf
ExecStart=/usr/bin/pgagent_16 -s ${LOGFILE} hostaddr=${DBHOST} dbname=${DBNAME} user=${DBUSER} port=${DBPORT}
If you change pgagent_16.conf name , you shou modify in pgagent_16.service
systemctl enable pgagent_16.service
systemctl start pgagent_16.service
systemctl status pgagent_16.service
- 5.3 Check running
Pgagent is running on server successfully
### 6. How to use Pgagent (by postgres account, like oracle sys or system)
- 6.1 use postgres to login db
Ex. setup a inserting job to update HQRPAD1(DB)/dennis(schema)/aaa(table) every 2 minutes.
- 6.2 create job on pgadmin.
- 6.3 Right click, pgAgent Jobs->Create->pgAgent Job…
- 6.4 type the name
When you see question mark, Click ?, you will get the detailed tutorials from web help.
After you click ?, it will pop up new window as below.
- 6.5 you will see below. There are Schedules and Steps under job test123
- 6.6 Right click Steps ->Create->Job Step…
- 6.7 type name for Step and choose database that your job step will run
- 6.8 switch to code tag. Type the sql you wan to run and click Save
- 6.9 switch to SQL tag. It will show the step detail. We don’t need to do any things about this tag. Just click Save
- 6.10 Right click Schedules ->Create->Schedule…
- 6.11 type name for Schedule and choose the start time and end time
- 6.12 switch to repeat tag. In our case, we want every 2 mins to run job.
A.Click 00; then click 02,04,…
B.After you finished, it will like this.
- 6.13 switch to exception. In this tag, you can set up the time that you don’t want the job run
- 6.14 switch to SQL tag. It will show the schedule detail. We don’t need to do any things about this tag. Just click Save
- 6.15 After we finish setup, we can run the jobs manually. ,Right click test123->Run now
- 6.16 check the job result
Click test123 switch to top Statistics tag
Job status r=running,s=successful,f=failed,i=no step needs to run,d=abort
- 6.17 check the HQRPAD1(DB)/dennis(schema)/aaa(table) result
Data inserted successfully.
### 7. How to use Pgagent (by non postgres account)
we plan to grant user: gary with pgagent authority.
- 7.1 check the HQRPAD1(DB)/dennis(schema)/aaa(table) result
- 7.2 Create Extension and Refresh
- 7.3 Grant privilege to Gary
grant all on schema pgagent to gary;
grant all on all tables in schema pgagent to gary;
grant all on all functions in schema pgagent to gary;
grant connect on database "IHLABD1" to gary;
grant all on all sequences in schema pgagent to gary;
grant all on all functions in schema pgagent to gary;
- 7.4 to make sure pgAgent jobs can run the setup jobs. You need to start pgagent service on server.
su - postgres
pgagent_16 host=IP port=5435 dbname=IHLABD1 user=gary password=gary -s ./pgagent_gary.log &
If DB restart, you must execute this command again !!
- 7.5 Leave postgres session and use gary to login DB
- 7.6 Create table
- 7.7 follow 6.3 to 6.16 create job
And 6.8 change to
Note After PGagent extension installed successfully, no mater pgagent service is running or not. All the related pgagent tables and schema existed. And you can create job as well. The only thing is creating job cannot run.
pgagen’s related tables on postgres/Catalogs/pgAgent job scheduler pgagent.pga_exception: record exception job message pgagent.pga_job: job information. Like start time and end time. pgagent.pga_jobagent:pgAgent configuration. Server’s pgagent and starting time. pgagent.pga_jobclass: pgAgent configuration. Define job type pgagent.pga_joblog: job running job. Include start time and execution time pgagent.pga_jobstep: each job setp pgagent.pga_jobsteplog: eacho job step log. Include the starting time and execution time of each job pgagent.pga_schedule:job schedule information
### 8. if you want to revoke the user: dennis with pgagent authority,You need to run below sql to revoke pgagent schema from dennis;
revoke all on schema pgagent from dennis;
revoke all on all tables in schema pgagent from dennis;
revoke all on all functions in schema pgagent from dennis;
revoke connect on database postgres from dennis;
revoke all on all sequences in schema pgagent from dennis;
revoke all on all functions in schema pgagent from dennis;
Before
After


































