pg_hba.conf和pg_ctl的路径 - 自动提取方式

vrt*_*234 1 postgresql

我正在编写对pg_hba.conf的一些更改脚本,我正在寻找一种自动方式来查找它的位置.同样,我也需要pg_ctl的路径来重新加载配置.我可以使用pg_clusters来查找数据目录,但我无法使用它来获取pg_hba.conf和pg_ctl的位置.有什么建议?

这是我在ubuntu上的这些东西的位置:

pg_hba.conf: /etc/postgresql/9.3/main/pg_hba.conf
PGDATA: /var/lib/postgresql/9.3/main
pg_ctl: /usr/lib/postgresql/9.3/bin
Run Code Online (Sandbox Code Playgroud)

谢谢.

Cra*_*ger 7

通过PostgreSQL协议连接,您可以获取datadir,配置文件和hba文件位置.

postgres=# SHOW hba_file;
              hba_file               
-------------------------------------
 /var/lib/pgsql/9.3/data/pg_hba.conf
(1 row)

postgres=# SHOW config_file ;
               config_file               
-----------------------------------------
 /var/lib/pgsql/9.3/data/postgresql.conf
(1 row)

postgres=# SHOW data_directory;
     data_directory      
-------------------------
 /var/lib/pgsql/9.3/data
(1 row)
Run Code Online (Sandbox Code Playgroud)

您也可以通过PostgreSQL协议重新加载配置:

postgres=# SELECT pg_reload_conf();
 pg_reload_conf 
----------------
 t
(1 row)
Run Code Online (Sandbox Code Playgroud)

所有这些细节都可以在一次psql调用中获得bash(如果你不介意编写bash特定的,非便携式shell),并正确处理空格分隔值,使用:

$ IFS='|' pgconf=($(psql -U postgres -qAt -F '|' -c "SELECT current_setting('hba_file'), current_setting('config_file'), current_setting('data_directory');"))
Run Code Online (Sandbox Code Playgroud)

它产生一个包含三个值的bash数组:

$ echo ${pgconf[0]}
/var/lib/pgsql/9.3/data/pg_hba.conf
$ echo ${pgconf[1]}
/var/lib/pgsql/9.3/data/postgresql.conf
$ echo ${pgconf[2]}
/var/lib/pgsql/9.3/data
Run Code Online (Sandbox Code Playgroud)