如何对PostgreSQL配置文件进行语法检查?

28

我经常编辑pg_hda.conf文件,我想知道是否有一种方法可以确保我刚刚写的内容是正确的。

目前我使用测试服务器来检查我的更改。

就像Apache有其apache2ctl -t命令一样,Postgres是否有类似的东西?

3个回答

18

很长时间以来,现在可以检查pg_hba.conf文件:

select pg_hba_file_rules();

此外,您可以使用pg_file_settings视图在postgresql.conf中定义错误:

select sourcefile, name,sourceline,error from pg_file_settings where error is not null;

示例输出:

postgres=# select sourcefile, name,sourceline,error from pg_file_settings where error is not null;
               sourcefile               |  name  | sourceline |                error
----------------------------------------+--------+------------+--------------------------------------
 /var/lib/pgsql/11/data/postgresql.conf | lalala |          1 | unrecognized configuration parameter

在这里,我放了一些坏东西来检查它是否正确:)


这非常有趣。我一找到工作就会去看看! - SamK
第二个选项对我在检查postgresql.conf方面非常有用。谢谢! - Digital Impermanence
这还会报告参数错误,即使它们的值已更改且有效,但是如果不重新启动服务器,则无法应用。因此,并非每个报告的错误都会阻止服务器重启。 - qris

15

没有类似于 apache2ctl 的方法来做到这一点。如果重新加载配置文件时有语法错误,PostgreSQL服务器将在日志中发出投诉并拒绝加载新文件。因此,通过语法输错很少会出现搞乱东西的风险。(当然,这并不能防止您编写语义上错误的内容,但是 apache2ctl 也无法做到这一点。)除此之外,最好在测试服务器中测试更改,并且拥有一个以受控方式将这些更改传播到生产环境的系统。


6

参考:https://dba.stackexchange.com/a/151457/177071

你可以尝试另一种方法来测试配置文件是否正确。

在命令行中输入 select pg_reload_conf();

postgres=# select pg_reload_conf();
 pg_reload_conf
----------------
 t
(1 row)

这表示您的文件是正确的,否则它将失败。


9
不是真的。在使用“t”重新加载配置后,我在PostgreSQL日志中看到了这一行:[9692]LOG: configuration file "/var/lib/pgsql/data/postgresql.conf" contains errors; unaffected changes were applied。 - DShost
1
@DShost,可能需要重新启动配置更改。选择 sourcefile, name, sourceline, error from pg_file_settings where error is not null; - Keith Brings
1
@DShost,可能需要重新启动配置更改。选择 sourcefile、name、sourceline、error from pg_file_settings where error is not null; - Keith Brings

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接