如何安全地配置pg_hba.conf postgresql.conf以连接到postgresql数据库服务器



我想配置我的DigitalOcean Postgresql数据库,以便在家使用pg_dumb,但我无法使其正常工作:

它与pg_hba-conf:一起工作

host all all all md5

以及在postgresaql.conf 中

listen_addresses = '*'

但我不想使用"all"one_answers"*",因为我想,这是不安全的。

我认为,在阅读了Postgresql文档(pg_hba.conf和身份验证等)后,我应该在以下两个文件中使用我的IP:

  • pg_hba.conf:host all all xxx.xxx.xx.xx/32 md5

  • postgresql.conf:listen_addresses = 'localhost,xxx.xxx.xx.xx'

但我不能理解这个问题:

  1. 如果我把listen_addresses='*'放在postgresql.conf中,那么我把什么IP放在pg_hba.conf中并不重要它适用于任何ip??例如host all all 113.147.107.52/0 md5
  2. 如果我把listen_addresses='localhost,[my_IP]'放在postgresql.conf中,它不起作用:connection to database "c..ion" failed: could not connect to server: Connection refused

    [my_IP]中,我把我从ipecho.net得到的东西放进去,这个Ip正确吗?

这是pg_hba.conf,最后一个条目是我希望只访问我的

# Database administrative login by Unix domain socket
local    all            postgres                               md5
# TYPE  DATABASE        USER            ADDRESS                 METHOD
# "local" is for Unix domain socket connections only
local   all             rails                                   peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# For copy from home with pg_dumb, this IP is random: all IPs allow me to execute pg_dum if in listen_address='*'
host    all              all            my_ip_from_ipecho.net/32           md5

我想我需要更多的IP知识,除此之外,我完全失去了

(PD:最后,我想我会在服务器内执行pg_dump,然后,从我的家用电脑上,我会用scp复制这些备份文件。但我想了解发生了什么以及它是如何工作的)

非常感谢

如果你想在postgresql.conf中指定ip地址,你必须确认你的客户端将从中混合的所有ip(在这种情况下,包括你正在使用的PC ip或VPN)。

这就是为什么当你使用"*"时它会起作用,因为它会监听来自所有ip(包括你的)的客户端。

pghba.conf将充当防火墙,因此您也需要在那里配置入口。

在您的情况下,我认为最好使用listen_addresss='*',并在pg_hba中配置ip地址,因为当您想要更改参数listen_adress时,需要重新启动它。

pg_hba.conf: host all all xxx.xxx.xx.xx/0 md5
postgresql.conf: listen_addresses = '*'

如果您需要进一步的帮助来配置您的pg_hba,请告诉我。

祝你好运!

注意:listen_addresses参数的更多信息http://www.postgresql.org/docs/9.4/static/runtime-config-connection.html

最新更新