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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16333319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:53:47  来源:igfitidea点击:

How to syntax check PostgreSQL config files?

postgresqlsyntaxconfiguration-files

提问by SamK

I am often editing the pg_hda.conf file and I was just wondering if there is a way to make sure that what I just wrote is correct.

我经常编辑 pg_hda.conf 文件,我只是想知道是否有办法确保我刚刚写的内容是正确的。

So far I'm using a test server to check my changes.

到目前为止,我正在使用测试服务器来检查我的更改。

Like Apache has its apache2ctl -tcommand, does Postgres has something similar?

就像 Apache 有它的apache2ctl -t命令一样,Postgres 有类似的东西吗?

采纳答案by Peter Eisentraut

There is no way to do this that is similar to apache2ctl. If you reload the configuration files and there is a syntax error, the PostgreSQL server will complain in the log and refuse to load the new file. So there is very little risk of messing something up by making a syntax typo. (Of course, this won't guard you against writing semantically wrong things, but apache2ctlwon't do that either.) Other than that, it is probably a good idea to test changes in a test server, and have a system that propagates those changes to production in a controlled way.

没有办法做到这一点,类似于apache2ctl. 如果重新加载配置文件出现语法错误,PostgreSQL 服务器会在日志中抱怨并拒绝加载新文件。因此,由于语法错误而搞砸的风险很小。(当然,这不会防止您编写语义错误的东西,但apache2ctl也不会这样做。)除此之外,在测试服务器中测试更改并拥有一个传播这些更改的系统可能是个好主意以可控的方式改变生产。

回答by Mikhail Aksenov

It's bee a long time, but now check is available for pg_hba.conf file:

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

select pg_hba_file_rules();

Also, you may use pg_file_settings view to define error in postgresql.conf:

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

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

here I put some bad stuff to check if it's true :)

在这里,我放了一些不好的东西来检查它是否属实:)

回答by u445908

refer to: https://dba.stackexchange.com/a/151457/177071

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

you have another approach to test if the config file is correct.

您有另一种方法来测试配置文件是否正确。

into the command line, type select pg_reload_conf();

进入命令行,输入 select pg_reload_conf();

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

This indicates your file is correct. otherwise it would fail.

这表明您的文件是正确的。否则会失败。