You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已设置sslmode=disable仍报错:postgres_exporter提示服务器未启用SSL

解决postgres_exporter设置sslmode=disable仍报SSL未启用的问题

问题分析

从错误信息Error opening connection to database (%22user=postgres): pq: SSL is not enabled on the server可以看出,实际使用的连接串仅包含user=postgres,完整的host和sslmode参数未生效,同时还带有转义的双引号(%22),说明连接串解析出现异常。结合你的配置,核心问题出在连接串格式错误和环境变量与配置文件的优先级冲突上。

修复步骤

方法一:修正systemd环境变量格式(单数据源场景)

修改postgres_exporter.service中的DATA_SOURCE_NAME,移除外层多余的双引号,确保连接串参数完整传递:

[Unit]
Description=Prometheus PostgreSQL Exporter
After=network.target

[Service]
Type=simple
Restart=always
User=postgres
Group=postgres
Environment=DATA_SOURCE_NAME=user=postgres host=/var/run/postgresql/ sslmode=disable
ExecStart=/usr/local/bin/postgres_exporter --log.level=debug

[Install]
WantedBy=multi-user.target

同时注释或删除postgres_exporter.yml(无需配置文件时),然后重新加载systemd并重启服务:

systemctl daemon-reload
systemctl restart postgres_exporter

方法二:使用配置文件完整配置(多数据源场景)

如果需要通过配置文件管理数据源,需确保exporter完全读取配置文件,不依赖环境变量:

  1. 修改postgres_exporter.service,移除DATA_SOURCE_NAME环境变量:
[Unit]
Description=Prometheus PostgreSQL Exporter
After=network.target

[Service]
Type=simple
Restart=always
User=postgres
Group=postgres
ExecStart=/usr/local/bin/postgres_exporter --config.file=/usr/local/bin/postgres_exporter.yml --log.level=debug

[Install]
WantedBy=multi-user.target
  1. 完善postgres_exporter.yml,添加collector配置关联auth模块,并补全连接参数:
auth_modules:
  foo1:
    type: userpass
    userpass:
        username: postgres
        password: postgres
    options:
        host: /var/run/postgresql/
        sslmode: disable

collector:
  enabled_collectors:
    - pg_stat_database
  datasources:
    - name: main_db
      auth_module: foo1
  1. 重新加载systemd并重启服务:
systemctl daemon-reload
systemctl restart postgres_exporter

验证方式

重启后查看服务日志,确认连接串包含完整的host和sslmode=disable参数:

journalctl -u postgres_exporter -f

若日志显示成功连接数据库且无SSL相关错误,说明问题已解决。

内容的提问来源于stack exchange,提问作者Axel HK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 05:05:16