MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Step by Step Guide to Monitor PostGreSQL using Zabbix Agent

To monitor PostgreSQL with the classic Zabbix agent, you install the agent on the database host, create a read-only monitoring role, and deploy the user parameters and SQL scripts that come with the official "PostgreSQL by Zabbix agent" template. Then you link that template to the host and set its macros. The outline above covers the agent install. This guide adds the parts it leaves out, fixes the monitoring role and shows how to check that metrics are arriving.

# Step 1: Install Zabbix Agent on the PostgreSQL Server

# Install Zabbix repository (Ubuntu example)
sudo wget https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.0-1+ubuntu20.04_all.deb
sudo dpkg -i zabbix-release_6.0-1+ubuntu20.04_all.deb
sudo apt update

# Install Zabbix Agent
sudo apt install zabbix-agent

# Step 2: Configure Zabbix Agent

# Edit Zabbix agent configuration
sudo nano /etc/zabbix/zabbix_agentd.conf
# Make changes to the file:
# Server=<Zabbix server IP>
# ServerActive=<Zabbix server IP>
# Hostname=<Name of the PostgreSQL server>

# Restart Zabbix Agent
sudo systemctl restart zabbix-agent

# Step 3: Configure PostgreSQL for Monitoring

# Edit PostgreSQL configuration file
sudo nano /etc/postgresql/12/main/postgresql.conf
# Add or uncomment the following lines:
# shared_preload_libraries = 'pg_stat_statements'
# pg_stat_statements.track = all

# Restart PostgreSQL to apply changes
sudo systemctl restart postgresql

# Create a monitoring role in PostgreSQL
sudo -u postgres psql -c "CREATE ROLE zabbix WITH LOGIN PASSWORD 'zabbix_password';"
sudo -u postgres psql -c "GRANT SELECT ON ALL TABLES IN SCHEMA public TO zabbix;"

# Step 4: Set up the Zabbix Template and Items

# Import PostgreSQL monitoring template in Zabbix server (manually via Zabbix UI)
# Path: Configuration > Templates > Import

# Link the template to your host in Zabbix server
# Path: Configuration > Hosts > select your host > Templates tab > Add the PostgreSQL template > Update

# Step 5: Test and Validate

# Check if data is being collected in Zabbix server
# Path: Monitoring > Latest Data > Filter by your PostgreSQL server's name

# Troubleshoot any issues by checking logs
# Check Zabbix Agent logs
cat /var/log/zabbix/zabbix_agentd.log
# Check PostgreSQL logs
cat /var/log/postgresql/postgresql-12-main.log
Code language: PHP (php)

How Zabbix collects PostgreSQL metrics

Zabbix ships two official PostgreSQL templates, and each one needs a different agent. The steps above install the classic agent (zabbix-agent, which runs as zabbix_agentd). With that agent, the template does nothing until the matching UserParameter definitions and SQL files are on the database host. Each item runs psql with one of those SQL files and returns the result to Zabbix. The steps above leave this part out, so linking the template alone produces only unsupported items.

TemplateAgentWhat you must deploy
PostgreSQL by Zabbix agentzabbix-agent (agentd)UserParameter file, SQL scripts, psql client, .pgpass or trust rule
PostgreSQL by Zabbix agent 2zabbix-agent2PostgreSQL plugin (built in or a separate package, depending on version) and connection macros

Note: The pg_stat_statements change in Step 3 above is not needed by either official template. It is safe but optional. If you do enable it, add it to the existing shared_preload_libraries list instead of replacing that list, restart PostgreSQL, and run CREATE EXTENSION pg_stat_statements; in the database you want to inspect.

Install and configure the agent correctly

The repository package in Step 1 is pinned to Zabbix 6.0 on Ubuntu 20.04, and that Ubuntu release is out of standard support. Package file names in the Zabbix repository also change between releases. Copy the current repository command from the Zabbix download page for your OS and server version. The agent major version should match your Zabbix server, or be older than it.

# After adding the Zabbix repository for your version and OS
sudo apt update
sudo apt install -y zabbix-agent postgresql-client git

# /etc/zabbix/zabbix_agentd.conf - key settings
Server=192.0.2.10          # Zabbix server or proxy IP (passive checks)
ServerActive=192.0.2.10    # same, for active checks
Hostname=pg-prod-01        # must match the host name in the Zabbix UI exactly
Timeout=10                 # some PostgreSQL queries take longer than the 3s default

sudo systemctl enable --now zabbix-agent
sudo systemctl restart zabbix-agent

# Allow the server to reach the passive agent port
sudo ufw allow from 192.0.2.10 to any port 10050 proto tcpCode language: PHP (php)

Also confirm that Include=/etc/zabbix/zabbix_agentd.d/*.conf is present and uncommented in zabbix_agentd.conf. The official packages set it by default, and the PostgreSQL user parameters are loaded from that directory.

Create a proper monitoring role

The role in Step 3 above is granted SELECT on tables in the public schema. That gives it access to your application data, which the monitoring user should not have. It also does not give access to the statistics views and functions the template needs. PostgreSQL 10 and later include the built-in pg_monitor role for exactly this purpose. Set the password interactively with \password so it does not end up in shell history.

sudo -u postgres psql -c "CREATE USER zbx_monitor WITH INHERIT;"
sudo -u postgres psql -c "GRANT pg_monitor TO zbx_monitor;"
sudo -u postgres psql -c "\password zbx_monitor"Code language: JavaScript (javascript)

Next, allow this role to connect locally. Find the right file with sudo -u postgres psql -c 'SHOW hba_file;'. Don't hard-code a version path such as /etc/postgresql/12/. Put these lines above any broader rules, because PostgreSQL uses the first rule that matches.

# TYPE  DATABASE  USER         ADDRESS        METHOD
host    all       zbx_monitor  127.0.0.1/32   scram-sha-256
host    all       zbx_monitor  ::1/128        scram-sha-256Code language: PHP (php)
sudo systemctl reload postgresql

Deploy the template's user parameters and SQL files

The user parameter file and SQL scripts live in the Zabbix source tree under templates/db/postgresql. Check out the branch that matches your server version (for example release/6.0 or release/7.0). A sparse checkout downloads only that directory.

git clone --depth 1 --branch release/7.0 --filter=blob:none --sparse https://github.com/zabbix/zabbix.git
cd zabbix
git sparse-checkout set templates/db/postgresql
ls templates/db/postgresql      # README.md, postgresql/, template_db_postgresql.conf, ...

sudo mkdir -p /var/lib/zabbix
sudo cp -r templates/db/postgresql/postgresql /var/lib/zabbix/
sudo cp templates/db/postgresql/template_db_postgresql.conf /etc/zabbix/zabbix_agentd.d/
sudo chown -R zabbix:zabbix /var/lib/zabbix

# Password file used by psql when the agent runs a query
sudo -u zabbix tee /var/lib/zabbix/.pgpass >/dev/null <<'EOF'
localhost:5432:*:zbx_monitor:YourStrongPassword
EOF
sudo chmod 600 /var/lib/zabbix/.pgpass

sudo systemctl restart zabbix-agentCode language: PHP (php)

The shipped .conf file points to /var/lib/zabbix/postgresql/*.sql, so keep that path. The host field in .pgpass (localhost here) must match the host that psql is given, which is the {$PG.HOST} macro. If file names in your branch differ, follow the README.md in that directory. It always matches the template version.

Link the template and set macros

  1. Open the hosts list: Configuration > Hosts in Zabbix 6.0, or Data collection > Hosts in 6.4 and 7.x. Create or edit the host. The Host name must equal the agent's Hostname.
  2. Add an Agent interface with the database server's IP and port 10050.
  3. On the Templates tab, link PostgreSQL by Zabbix agent. It ships with the server, so no import is needed unless you are upgrading it.
  4. On the Macros tab, choose Inherited and host macros and check {$PG.HOST}, {$PG.PORT}, {$PG.USER} and {$PG.DB}. The defaults are typically localhost, 5432, zbx_monitor and postgres. Override any value that differs on your server.
  5. Click Update, then wait a few minutes for the first checks and for low-level discovery of databases.

Verify from the command line

Test each layer separately before you look in the UI. First check that psql can log in as the agent would. Then run an item key through the agent on the database host, and finally query it from the Zabbix server.

# 1. psql login as the zabbix OS user (uses /var/lib/zabbix/.pgpass)
sudo -u zabbix -H psql -h localhost -p 5432 -U zbx_monitor -d postgres -c "SELECT version();"

# 2. List the keys the agent now knows about
grep -o '^UserParameter=[^,]*' /etc/zabbix/zabbix_agentd.d/template_db_postgresql.conf | head

# 3. Run one key locally through the agent
sudo -u zabbix -H zabbix_agentd -t 'pgsql.ping["localhost","5432","zbx_monitor","postgres"]'

# 4. From the Zabbix server (package zabbix-get)
zabbix_get -s 192.0.2.20 -k 'pgsql.ping["localhost","5432","zbx_monitor","postgres"]'

# Follow logs instead of cat-ing the whole file
sudo tail -f /var/log/zabbix/zabbix_agentd.log
sudo tail -f /var/log/postgresql/postgresql-*-main.logCode language: PHP (php)

Steps 3 and 4 should return a value, not ZBX_NOTSUPPORTED or a psql error. The UserParameter line for each key shows what a healthy result looks like. After that, Monitoring > Latest data filtered by the host should show PostgreSQL items with recent timestamps.

Common errors and fixes

SymptomLikely causeFix
Unknown metric pgsql.*UserParameter file not loadedCheck the Include= line and the file location, then restart the agent
psql: command not foundNo PostgreSQL client on the agent hostsudo apt install postgresql-client
fe_sendauth: no password supplied.pgpass missing, wrong permissions, host mismatch, or the zabbix user's home is not /var/lib/zabbixSet mode 600, owner zabbix, and match the host field. Or run sudo systemctl edit zabbix-agent and add Environment=PGPASSFILE=/var/lib/zabbix/.pgpass under [Service]
no pg_hba.conf entry for hostNo matching rule, or a broader rule matches firstAdd the zbx_monitor rules above the others and reload PostgreSQL
permission denied for ...Role lacks monitoring privilegesGRANT pg_monitor TO zbx_monitor;
could not open file /var/lib/zabbix/postgresql/...SQL scripts not copied, or not readable by zabbixCopy the postgresql/ directory and chown -R zabbix:zabbix
Agent interface unavailableServer= mismatch or port 10050 blockedFix Server= and the firewall, then test with zabbix_get
Item times outQuery slower than the agent TimeoutRaise Timeout in the agent config (in 7.x, item timeouts can also be set in the UI)

Alternative: Zabbix agent 2

Agent 2 has a native PostgreSQL plugin, so you don't need SQL files, .pgpass or psql. Install zabbix-agent2, plus zabbix-agent2-plugin-postgresql on releases where the plugin ships separately. Link PostgreSQL by Zabbix agent 2 and fill in the template's connection macros (URI, user and password). You can also define a named session in the plugin config under /etc/zabbix/zabbix_agent2.d/plugins.d/. Use the same pg_monitor role and pg_hba.conf rules.

Note: Both agents listen on port 10050 by default. Replace zabbix-agent with zabbix-agent2 rather than running both on the same port.

Frequently asked questions

Which permissions does the Zabbix PostgreSQL monitoring user need?

On PostgreSQL 10 and later, grant the built-in pg_monitor role to a dedicated login role. It does not need superuser rights or SELECT on application tables.

Why are all PostgreSQL items unsupported after linking the template?

With the classic agent, the template needs its UserParameter file in /etc/zabbix/zabbix_agentd.d/ and its SQL scripts in /var/lib/zabbix/postgresql/. Without those files the agent doesn't recognise the pgsql.* keys.

Should I use Zabbix agent or Zabbix agent 2 for PostgreSQL?

Agent 2 is simpler for new setups because its native plugin needs no scripts, psql or .pgpass. The classic agent template still works and suits hosts that are standardised on zabbix_agentd.

Do I need pg_stat_statements for Zabbix PostgreSQL monitoring?

No. The official templates use standard statistics views. Enable pg_stat_statements only if you want query-level analysis outside the template.

Can one Zabbix agent monitor a remote PostgreSQL server?

Yes. Point {$PG.HOST} at the remote server, add a matching .pgpass entry and allow the agent's IP in the remote pg_hba.conf. Installing the agent on the database host is still the more common setup.

Related tutorials

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Zabbix Tutorials: Example of zabbix_proxy.conf

The example above is the full, commented sample zabbix_proxy.conf shipped with Zabbix 7.0. It includes 7.0-only options such as ProxyBufferMode, ProxyMemoryBufferSize and the asynchronous agent, HTTP and…

Read More

Zabbix Tutorials: Example of zabbix_agentd.conf

zabbix_agentd.conf is the configuration file for the classic Zabbix agent on Linux and Unix. It sets which Zabbix servers may poll the agent, where it sends active…

Read More

Zabbix Tutorials: Example of zabbix_agentd.win.conf

zabbix_agentd.win.conf is the sample configuration file that comes with the classic Zabbix agent for Windows. The file above is the stock sample from a recent release, but…

Read More

Zabbix Tutorials: Example of zabbix_agent2.win.conf

The example above is the stock zabbix_agent2.win.conf that ships with Zabbix agent 2 for Windows. Nearly every line is a comment that documents a default value. Only…

Read More

Rundeck Database Migration From H2 to MySql and postgresql

This guide moves Rundeck off its embedded H2 database and onto MySQL or PostgreSQL, which is what you should use in production. The steps above set up…

Read More

Ansible Playbook for Zabbix Agent Deployment

This page shows an Ansible playbook that installs and configures Zabbix agent 6.0 on CentOS and RHEL hosts. Below the original playbook you will find a corrected,…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x