Configuration
Assuming pgexporter has been installed, it needs some configurations before it can be used.
PostgreSQL
Ensure PostgreSQL is running:
$ sudo systemctl status postgresql.service$ sudo systemctl status postgresql.serviceshould give you:
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: disabled)
Drop-In: /usr/lib/systemd/system/service.d
└─10-timeout-abort.conf
Active: active (running) since Tue 2023-09-05 19:51:27 IST; 26min ago
Process: 6820 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/SUCCESS)
Main PID: 6822 (postmaster)
Tasks: 7 (limit: 9129)
Memory: 23.4M
CPU: 435ms
CGroup: /system.slice/postgresql.service
├─6822 /usr/bin/postmaster -D /var/lib/pgsql/data
├─6823 "postgres: logger "
├─6824 "postgres: checkpointer "
├─6825 "postgres: background writer "
├─6827 "postgres: walwriter "
├─6828 "postgres: autovacuum launcher "
└─6829 "postgres: logical replication launcher "
Sep 05 19:51:27 fedora systemd[1]: Starting postgresql.service - PostgreSQL database server...
Sep 05 19:51:27 fedora postmaster[6822]: 2023-09-05 19:51:27.170 IST [6822] LOG: redirecting log output to logging collector process
Sep 05 19:51:27 fedora postmaster[6822]: 2023-09-05 19:51:27.170 IST [6822] HINT: Future log output will appear in directory "log".
Sep 05 19:51:27 fedora systemd[1]: Started postgresql.service - PostgreSQL database server.● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: disabled)
Drop-In: /usr/lib/systemd/system/service.d
└─10-timeout-abort.conf
Active: active (running) since Tue 2023-09-05 19:51:27 IST; 26min ago
Process: 6820 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/SUCCESS)
Main PID: 6822 (postmaster)
Tasks: 7 (limit: 9129)
Memory: 23.4M
CPU: 435ms
CGroup: /system.slice/postgresql.service
├─6822 /usr/bin/postmaster -D /var/lib/pgsql/data
├─6823 "postgres: logger "
├─6824 "postgres: checkpointer "
├─6825 "postgres: background writer "
├─6827 "postgres: walwriter "
├─6828 "postgres: autovacuum launcher "
└─6829 "postgres: logical replication launcher "
Sep 05 19:51:27 fedora systemd[1]: Starting postgresql.service - PostgreSQL database server...
Sep 05 19:51:27 fedora postmaster[6822]: 2023-09-05 19:51:27.170 IST [6822] LOG: redirecting log output to logging collector process
Sep 05 19:51:27 fedora postmaster[6822]: 2023-09-05 19:51:27.170 IST [6822] HINT: Future log output will appear in directory "log".
Sep 05 19:51:27 fedora systemd[1]: Started postgresql.service - PostgreSQL database server.Add Linux User
Add linux user named pgexporter:
$ sudo su -
$ useradd -ms /bin/bash pgexporter$ sudo su -
$ useradd -ms /bin/bash pgexporterSecure the user with a suitable password, say secretpassword after typing:
$ passwd pgexporter$ passwd pgexporterand then exit:
$ exit$ exitAdd User to PostgreSQL
Switch to postgres user using:
$ sudo -i -u postgres$ sudo -i -u postgresand add user pgexporter to PostgreSQL:
$ createuser -P pgexporter$ createuser -P pgexporterwith a suitable password, say, again, secretpassword.
pg_monitor
As postgres user, enter psql like:
$ psql -d postgres$ psql -d postgresand grant pgexporter user the role of pg_monitor:
GRANT pg_monitor TO pgexporter;GRANT pg_monitor TO pgexporter;pg_hba.conf
As postgres user, go into psql like:
$ sudo -i -u postgres
$ psql$ sudo -i -u postgres
$ psqland locate the pg_hba.conf file using:
SHOW hba_file;SHOW hba_file;Using the acquired path, in pg_hba.conf remove (or comment out):
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trusthost all all 127.0.0.1/32 trust
host all all ::1/128 trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trustand instead put:
local postgres pgexporter scram-sha-256
host postgres pgexporter 127.0.0.1/32 scram-sha-256
host postgres pgexporter ::1/128 scram-sha-256local postgres pgexporter scram-sha-256
host postgres pgexporter 127.0.0.1/32 scram-sha-256
host postgres pgexporter ::1/128 scram-sha-256This will add a user in your linux system named pgexporter.
WARNING
Check the value of password_encryption in postgresql.conf (usually in a location like /var/lib/pgsql/data/postgresql.conf) to setup the correct authentication type above (scram-sha-256, md5, etc.).
Server
Once done, restart the PostgreSQL server:
$ sudo systemctl restart postgresql.service$ sudo systemctl restart postgresql.serviceVerify Access
$ psql -h localhost -p 5432 -U pgexporter -d postgres$ psql -h localhost -p 5432 -U pgexporter -d postgresand in psql:
\q\qIf no errors occur in this process, proceed below.
User vault
A user vault is required for the pgexporter account. As pgexporter user, create a master key:
$ pgexporter-admin master-key$ pgexporter-admin master-keywith the password of the user (secretpassword).
Store the same password using:
$ pgexporter-admin -f pgexporter_users.conf add-user$ pgexporter-admin -f pgexporter_users.conf add-userpgexporter.conf
A pgexporter.conf file is required to specify some information to pgexporter. As the pgexporter user, create a pgexporter.conf file with the following contents:
[pgexporter]
host = *
metrics = 5002
log_type = file
log_level = info
log_path = /tmp/pgexporter.log
unix_socket_dir = /tmp/
[primary]
host = localhost
port = 5432
user = pgexporter[pgexporter]
host = *
metrics = 5002
log_type = file
log_level = info
log_path = /tmp/pgexporter.log
unix_socket_dir = /tmp/
[primary]
host = localhost
port = 5432
user = pgexporterA breakdown of this configuration file can be viewed here for understanding its components.
Running pgexporter
As pgexporter user, run pgexporter:
$ pgexporter -c pgexporter.conf -u pgexporter_users.conf$ pgexporter -c pgexporter.conf -u pgexporter_users.confIf this does not give any output or stop on its own, then it pgexporter is live and accepting responses. It can be stopped pressing Ctrl+C (^C) in the console where you started it, or by sending the SIGTERM signal to the process using kill <pid> (replace <pid> with the PID of the process).
View Metrics
Go to http://localhost:5002 in a browser, or in a terminal:
$ curl http://localhost:5002/metrics$ curl http://localhost:5002/metricsHere 5002 refers to the port written in pgexporter.conf for metrics.