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

时间:2016-05-11 13:20:19

标签: linux postgresql security digital-ocean

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

适用于pg_hba-conf:

host all all all md5

和postgresaql.conf

listen_addresses = '*'

但是我不想使用“全部”和“*”,因为我认为它不安全。

我认为,在阅读Postgresql文档(pg_hba.confauthentication等等)后,我应该在这两个文件中使用我的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中,那我在pg_hba.conf中输入的IP无关紧要! 适用于任何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得到的东西,是对的吗?

  3. 这是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,然后,从我的家用PC,我将用scp复制这些备份文件。但我想了解发生了什么,它是如何工作的)

    非常感谢

1 个答案:

答案 0 :(得分:0)

如果你想在postgresql.conf中指定ip地址,你必须确认你的客户将来自的所有ips(包括你的PC ip或VPN,如果你使用的话)。

这就是为什么它在你使用'*'时有效,因为它会监听来自所有ips(包括你的)的客户端。

pg_hba.conf将作为防火墙使用,因此您还需要在那里配置入口。

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

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

告诉我您是否需要进一步的帮助来配置您的pg_hba。

祝你好运!

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

相关问题