Install and Configure PostgreSQL Server Exporter
tar zxf postgres_exporter-0.11.1.linux-amd64.tar.gz
mv postgres_exporter-0.11.1.linux-amd64 postgres_exporter
cp postgres_exporter/postgres_exporter /usr/local/bin/
Create a configuration file
In the file, provide the DATA_SOURCE_NAME(DSN) variable for the PostgreSQL instance. For example:
#DATA_SOURCE_NAME="postgresql://postgres:postgres@localhost:5432/?sslmode=disable"
###To monitor a Specific database, uncomment and edit the line below###
DATA_SOURCE_NAME="postgresql://username:password@localhost:5432/database-name?sslmode=disable"
You can replace localhost with the IP address if you are running PostgreSQL on a remote host. You also need to ensure that remote connectivity is enabled on PostgreSQL.
Now create a systemd service for the PostgreSQL Server Exporter.
Add the below lines to the file:
[Unit]
Description=Prometheus exporter for Postgresql
Wants=network-online.target
After=network-online.target
[Service]
User=postgres
Group=postgres
WorkingDirectory=/opt/postgres_exporter
EnvironmentFile=/opt/postgres_exporter/postgres_exporter.env
ExecStart=/usr/local/bin/postgres_exporter --web.listen-address=:9187 --web.telemetry-path=/metrics
Restart=always
[Install]
WantedBy=multi-user.target
On RHEL-based systems, you need to configure SELinux as shown:
如果使用 postgres 系统用户 Allow the user to own the created directory:
Or 创建一个新的用户 postgres_exporter
Create a system user for the service:
Allow the user to own the created directory:
Reload the daemon:
Start postgres_exporter :
Start and enable the service:
Verify if the service is running:
● postgres_exporter.service
Loaded: loaded (/etc/systemd/system/postgres_exporter.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-09-05 09:48:38 CEST; 5s ago
Main PID: 33323 (postgres_export)
Tasks: 4 (limit: 23441)
Memory: 11.0M
CPU: 12ms
CGroup: /system.slice/postgres_exporter.service
└─33323 /usr/local/bin/postgres_exporter --web.listen-address=:9187 --web.telemetry-path=/metrics
If you have a firewall running, allow the port through it:
sudo ufw allow 9187
sudo firewall-cmd --add-port=9187/tcp --permanent
sudo firewall-cmd --reload
没有评论