DBAtlas · 数据库运维图谱

验证 pg_hba.conf 规则顺序并安全地重新加载

PostgreSQL安全

命令

-- File location
SHOW hba_file;

# Rules are read top to bottom and the FIRST matching rule wins;
# specific rules must come BEFORE general ones:
#   hostssl  appdb  web_app  10.10.0.0/16  scram-sha-256
#   host     all    all      10.10.0.0/16  reject
#   host     all    all      0.0.0.0/0     scram-sha-256

-- Validate BEFORE reloading: pg_hba_file_rules reads the file on disk
SELECT line_number, type, database, user_name, address, netmask, auth_method, error
FROM pg_hba_file_rules
ORDER BY line_number;

-- Apply if the error column is empty
SELECT pg_reload_conf();

作用

在 reload 之前显示 pg_hba.conf 的位置、PostgreSQL 如何解释它(行、类型、数据库、用户、地址、方法)以及任何语法错误;然后重新加载配置。

适用场景

在添加新的客户端网络/应用、从 md5 迁移到 scram-sha-256,或排查 'no pg_hba.conf entry for host' 错误时。

注意

pg_hba_file_rules 显示的是文件当前的内容,而不是生效中的规则。如果任何一行的 error 非空,reload 根本不会加载 pg_hba.conf,旧规则继续生效(日志显示 'pg_hba.conf was not reloaded')。变更只影响新连接。放在前面的宽泛规则(例如 host all all 0.0.0.0/0 trust)会使其后的所有限制失效。SSL 关闭时,hostssl 行无法匹配,并在 error 列中显示消息。默认只有超级用户可以读取该视图。确保以 root 编辑文件的工具不会改变其属主;如果 postgres 用户无法读取,reload 会失败。

在 DBAtlas 中打开 →

相关内容