Postgres 似乎忽略了 listen_addresses 设置

stv*_*mth 10 postgresql configuration pg-hba.conf

我正在尝试在我的 Postgres 9.5.7 数据库(运行 Ubuntu 16.04)上禁用远程客户端访问。

我做了我认为正确的更改(见下文)但无济于事。我可以想到我的更改不起作用的两个原因:

  • 我正在编辑错误的文件
  • 我没有正确重启服务

我以为我排除了这些,但远程客户端仍然可以通话。那么我错过了什么?

实际上,我想要做的是恢复启用远程客户端访问的更改,以便我可以捕获这些更改并使用我们的盐堆栈重新应用它们。在我开始通过 salt 运行部署测试之前,我想知道远程访问被禁用,以便我可以确信我的 salt 堆栈正在进行更改。

一些数据

我正在使用我正在编辑的相同配置文件启动 postgres(ps重新格式化输出以提高可读性)

postgres@testweb:~$ ps -eaf | grep bin/post
postgres [snip] /usr/lib/postgresql/9.5/bin/postgres 
                -D /var/lib/postgresql/9.5/main 
                -c config_file=/etc/postgresql/9.5/main/postgresql.conf
Run Code Online (Sandbox Code Playgroud)

postgres 配置为仅侦听 localhost 端口(之前是 '*')

postgres@testweb:~$ grep listen_addresses /etc/postgresql/9.5/main/postgresql.conf
listen_addresses = 'localhost'      # what IP address(es) to listen on;
Run Code Online (Sandbox Code Playgroud)

基于主机的身份验证设置为 localhost。如果我禁用访问,这应该无关紧要,对吗?我的理解是,如果listen_addresses='localhost'那时远程客户端访问将不起作用。

postgres@testweb:~$ grep ^host /etc/postgresql/9.5/main/pg_hba.conf
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5
Run Code Online (Sandbox Code Playgroud)

我正在编辑正确的配置文件,但 listen_addresses 声称 *

postgres@testweb:~$ sql
psql (9.5.7)
Type "help" for help.

postgres=# show config_file;
           config_file
------------------------------------------
/etc/postgresql/9.5/main/postgresql.conf
(1 row)

postgres=# show hba_file;
               hba_file
--------------------------------------
/etc/postgresql/9.5/main/pg_hba.conf
(1 row)

postgres=# show listen_addresses;
 listen_addresses
------------------
*
Run Code Online (Sandbox Code Playgroud)

在操作系统级别证明我们仍在侦听所有接口(即show list_addresses;命令有效。

postgres@testweb:~$ netstat -nlt | grep 5432
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN
tcp6       0      0 :::5432                 :::*                    LISTEN
Run Code Online (Sandbox Code Playgroud)

我在 .conf 中有一个自定义 conf 文件conf.d,但我不清楚它是否正在被读取,因为我没有触摸它以允许远程客户端访问。无论如何,它现在设置为我想要的

postgres@testweb:~$ cat /etc/postgresql/9.5/main/conf.d/custom.conf
listen_addresses = 'localhost'
shared_buffers = 4011MB
work_mem = 320MB
maintenance_work_mem = 1018MB
effective_cache_size = 8022MB
shared_preload_libraries = 'pg_stat_statements'
Run Code Online (Sandbox Code Playgroud)

服务器上没有出现杂散的配置文件

postgres@testweb:~$ locate postgresql.conf
/etc/postgresql/9.5/main/postgresql.conf
/etc/postgresql/9.5/main/postgresql.conf.bak
/usr/lib/tmpfiles.d/postgresql.conf
/usr/share/postgresql/9.5/postgresql.conf.sample

postgres@testweb:~$ locate pg_hba.conf
/etc/postgresql/9.5/main/pg_hba.conf
/usr/share/postgresql/9.5/pg_hba.conf.sample
Run Code Online (Sandbox Code Playgroud)

我重新启动了 postgres 实例service postgresql restart(我什至尝试重新启动服务器(呃)只是为了确保。仍然没有运气。我已经验证,如果我执行service postgresql stop我的远程客户端会失败。

显然,为了启用远程客户端访问,我必须调整其他一些东西;但对于我的生活,我想不出那是什么。我已经撤消了我记得所做的更改(在两个 conf 文件中)。我一定错过了什么......但是什么?

编辑

根据评论,如果我指定主机登录,我会看到相同的结果(AFAIK,我从未设置过 postgres 用户密码,所以我以 root 身份访问)。

root@testweb:~# sudo -u postgres -h localhost psql
psql (9.5.7)
Type "help" for help.

postgres=# show listen_addresses;
 listen_addresses
------------------
 *
(1 row)

postgres=# show config_file;
               config_file
------------------------------------------
 /etc/postgresql/9.5/main/postgresql.conf
(1 row)
Run Code Online (Sandbox Code Playgroud)

根据另一条评论,这是执行后的输出service postgresql restart......似乎很干净(不确定最后一行......研究(感谢丹尼尔)表明它“无关紧要”)

2017-07-18 17:43:16.440 MDT [4911] LOG:  database system was shut down at 2017-07-18 17:43:15 MDT
2017-07-18 17:43:16.444 MDT [4911] LOG:  MultiXact member wraparound protections are now enabled
2017-07-18 17:43:16.447 MDT [4910] LOG:  database system is ready to accept connections
2017-07-18 17:43:16.447 MDT [4915] LOG:  autovacuum launcher started
2017-07-18 17:43:16.848 MDT [4917] [unknown]@[unknown] LOG:  incomplete startup packet
Run Code Online (Sandbox Code Playgroud)

Ark*_*ena 10

查看postgresql.auto.conf$PGDATA中的文件。

如果有人使用该ALTER SYSTEM命令更改设置,您会在那里找到值。

postgresql.auto.conf之后读取,postgresql.conf并且放入此文件中的每个设置都会覆盖postgresql.conf...

值得一看...