Grafana监控PostgreSQL

1、下载postgres_exporter部署包

java 复制代码
curl -L -O https://github.com/prometheus-community/postgres_exporter/releases/download/v0.15.0/postgres_exporter-0.15.0.linux-amd64.tar.gz

2、解压并拷贝命令

java 复制代码
temdir=$(mktemp -d)
tar xvf postgres_exporter-0.15.0.linux-amd64.tar.gz -C ${temdir}
sudo cp ${temdir}/postgres_exporter-0.15.0.linux-amd64/postgres_exporter /usr/local/bin/

3、启动postgres_exporter服务

java 复制代码
cat <<'EOF' | sudo tee /usr/lib/systemd/system/postgres_exporter.service >> /dev/null
[Unit]
Description=postgres_exporter
Documentation=https://github.com/prometheus-community/postgres_exporter
Wants=network.service
After=network.service

[Service]
Type=simple
User=postgres
Environment=DATA_SOURCE_USER=postgres
Environment=DATA_SOURCE_PASS=123456
Environment=DATA_SOURCE_URI=localhost:5432/postgres?sslmode=disable
ExecStart=/usr/local/bin/postgres_exporter --web.listen-address=:9187 --collector.postmaster --collector.stat_statements
ExecStop=/bin/kill -s SIGTERM $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable postgres_exporter.service --now

4、清理文件

java 复制代码
sudo rm -rf ${temdir}

postgresql配置扩展

1、验证是否有编译pg_stat_statements扩展

java 复制代码
ls /app/postgresql/lib/pg_stat_statements.so

2、编译pg_stat_statements扩展

java 复制代码
cd ./postgresql-14.10/contrib/pg_stat_statements/
 make && sudo make install

3、修改postgresql配置文件

java 复制代码
grep shared_preload_libraries $PGDATA/postgresql.confshared_preload_libraries = 'pg_stat_statements' # (change requires restart)
sudo systemctl restart postgresql

4、验证是否有pg_stat_statements扩展

java 复制代码
psql -h localhost -p 5432 -U postgres -W -c "select name from pg_available_extensions where name = 'pg_stat_statements';"
Password: 
        name        
--------------------
 pg_stat_statements
(1 row)

5、添加pg_stat_statements扩展

java 复制代码
psql -h localhost -p 5432 -U postgres -W -c "CREATE EXTENSION pg_stat_statements;"

Prometheus和grafana配置

1、Prometheus采集postgres数据

java 复制代码
kubectl -n kube-system edit cm prometheus
    - job_name: "postgres-exporter"
      static_configs:
      - targets:
        - "172.139.20.17:9187"
        - "172.139.20.81:9187"
        - "172.139.20.177:9187"

2.验证是否采集成功

java 复制代码
curl -s $(kubectl -n kube-system get svc prometheus -ojsonpath='{.spec.clusterIP}:{.spec.ports[0].port}')/prometheus/api/v1/query --data-urlencode 'query=up{job="postgres-exporter"}' 
{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","instance":"172.139.20.17:9187","job":"postgres-exporter"},"value":[1731314524.901,"1"]},{"metric":{"__name__":"up","instance":"172.139.20.81:9187","job":"postgres-exporter"},"value":[1731314524.901,"1"]},{"metric":{"__name__":"up","instance":"172.139.20.177:9187","job":"postgres-exporter"},"value":[1731314524.901,"1"]}]}}
相关推荐
安当加密12 分钟前
MySQL数据库透明加密(TDE)解决方案:基于国密SM4的合规与性能优化实践
数据库·mysql·性能优化
JH30731 小时前
第七篇:Buffer Pool 与 InnoDB 其他组件的协作
java·数据库·mysql·oracle
板凳坐着晒太阳1 小时前
ClickHouse 配置优化与问题解决
数据库·clickhouse
数据库生产实战1 小时前
解析Oracle 19C中并行INSERT SELECT的工作原理
数据库·oracle
AAA修煤气灶刘哥2 小时前
服务器指标多到“洪水泛滥”?试试InfluxDB?
数据库·后端·面试
阿沁QWQ2 小时前
MySQL服务器配置与管理
服务器·数据库·mysql
程序新视界3 小时前
MySQL“索引失效”的隐形杀手:隐式类型转换,你了解多少?
数据库·mysql·dba
Logintern094 小时前
windows如何设置mongodb的副本集
数据库·windows·mongodb
Java 码农5 小时前
CentOS 7 上安装 PostgreSQL
linux·postgresql·centos
RestCloud5 小时前
在制造业数字化转型浪潮中,数据已成为核心生产要素。然而,系统割裂、数据滞后、开发运维成本高等问题,却像顽固的 “数据枷锁”,阻碍着企业发展。ETLCloud与
数据库·postgresql