首页
/ 【亲测免费】 PostgreSQL Exporter 安装与使用指南

【亲测免费】 PostgreSQL Exporter 安装与使用指南

2026-01-16 10:22:58作者:薛曦旖Francesca

目录结构及介绍

prometheus-community/postgres_exporter 的仓库中,主要的文件和目录包括:

  • postgres-metrics-get-changes.sh: 脚本用于获取PostgreSQL的指标变化。
  • postgres_exporter.rc: 配置脚本可能被其他脚本调用以设置环境变量。
  • postgres_exporter_integration_test_script.sh: 运行集成测试时使用的shell脚本。
  • queries.yaml: YAML文件,包含了PostgreSQLExporter查询数据库时所使用的SQL语句集合。

此外,还有标准的仓库管理文件如 .gitignore, LICENSEREADME.md 等。

启动文件介绍

Postgres Exporter 快速启动

Postgres Exporter 可通过Docker容器快速启动。以下是一组命令示例来演示如何运行一个PostgreSQL实例以及如何连接该实例到Postgres Exporter:

步骤一: 启动PostgreSQL实例

docker run --net=host -it --rm \
-e POSTGRES_PASSWORD=password \
postgres

步骤二: 连接到PostgreSQL实例并启用监控导出器

docker run \
--net=host \
-e DATA_SOURCE_URI="localhost:5432/postgres sslmode=disable" \
-e DATA_SOURCE_USER=postgres \
-e DATA_SOURCE_PASS=password \
quay.io/prometheuscommunity/postgres-exporter

这些步骤确保Postgres Exporter可以采集目标PostgreSQL服务器的指标数据。

配置文件介绍

默认情况下,PostgreSQL Server Exporter 使用内置的默认值进行工作,在大多数场景下无需修改配置。然而,为了更深入地自定义其行为,可以通过编辑 /etc/gitlab/gitlab.rb 文件中的相关选项来调整。重要的配置项有:

  1. postgres_exporter['dbname'] - 数据库名称,默认是 'pgbouncer'
  2. postgres_exporter['user'] - 登陆数据库的用户名,默认是 'gitlab-psql'
  3. postgres_exporter['password'] - 用户密码。
  4. postgres_exporter['host'] - 数据库主机地址,支持Unix域套接字路径或IP地址,默认为 'localhost'
  5. postgres_exporter['port'] - 数据库监听端口,默认为 '5432'
  6. postgres_exporter['sslmode'] - 是否使用SSL及其验证模式(例如 'disable', 'require', 'verify-ca', 'verify-full')。
  7. postgres_exporter['fallback_application_name'] - 当未提供应用名时作为回退值的应用名称。
  8. postgres_exporter['connect_timeout'] - 最大等待时间秒数,零或者不指定则无限期等待。
  9. postgres_exporter['sslcert']postgres_exporter['sslkey'] - 分别指明证书和私钥的位置。

需要注意的是,上述配置也可以通过环境变量来设置。任何由 PG_ 开头的环境变量都将覆盖相应的CLI选项,这提供了更高的灵活性和动态性。

以上就是基于 prometheus-community/postgres_exporter 的安装和基本使用的指南,希望能够帮助你顺利开展监测任务。

登录后查看全文
热门项目推荐
相关项目推荐