寒玉 Blog
  • Home
  • Books
  • About Me
  • Categories
  • Tags
  • Archives

PostgreSQL Allow Remote Connection


PostgreSQL 允许远程机器访问

查看默认配置

PostgreSQL 默认的配置绑定的是localhost, 不允许其他机器链接.

$ netstat -nlt
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:11211         0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:3737          0.0.0.0:*               LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN

从上图可以看到端口5432绑定到了127.0.0.1,意味着任何其他企图从其他机器链接到postgresql server都会被拒绝. 使用telnet链接将产生如下信息:

$ telnet 107.170.11.79 5432
Trying 107.170.11.79...
telnet: connect to address 107.170.11.79: Connection refused
telnet: Unable to connect to remote host

配置postgresql.conf

要处理这问题需要修改postgresql.conf文件.

$ find \ -name "postgresql.conf"
/var/lib/pgsql/9.4/data/postgresql.conf

打开postgresql.conf,找到并替换

listen_addresses = 'localhost'

替换为

listen_addresses = '*'

重启postgresql server之后查看

$ netstat -nlt
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:11211         0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:2812          0.0.0.0:*               LISTEN
tcp6       0      0 ::1:11211               :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 :::5432                 :::*                    LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN

可以看到端口5432的Local Address已经变成0.0.0.0,这样就可以从其他机器访问到PostgreSQL的服务器了


  • « JAVA8 G1
  • JAVA8 Timer & TimerTask »

Published

1 17, 2018

Category

bigdata

Tags

  • bigdata 12
  • postgresql 1
  • Powered by Pelican. Theme: Elegant by Talha Mansoor