psql无法连接到IP和端口5432上的PostgreSQL服务器(postmaster)?

Nul*_*ion 14 postgresql

请在回复之前阅读(因为它可能会发生).我正在运行我的postmaster(postgres)服务器.请参阅下面的'sudo netstat -anp | grep 5432'输出?

tcp  0  0 127.0.0.1:5432    0.0.0.0:*     LISTEN      29606/postmaster       
unix  2      [ ACC ]     STREAM     LISTENING     1650581 29606/postmaster /var/run/postgresql/.s.PGSQL.5432
unix  2      [ ACC ]     STREAM     LISTENING     1650582 29606/postmaster    /tmp/.s.PGSQL.5432               
Run Code Online (Sandbox Code Playgroud)

我可以使用localhost连接

psql -h localhost (OR 127.0.0.1) -d <DB> -U user -W
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用tcp从其他主机连接时,通过指定

psql -h ip_add_postmaster -d <DB> -U user -W
Run Code Online (Sandbox Code Playgroud)

它抛出:

psql:无法连接到服务器:连接被拒绝服务器是否在主机XXXXXX上运行并接受端口5432上的TCP/IP连接?

这有什么不对?

pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only                  
local   all             all                                     peer  
# IPv4 local connections:                                             
host    all             all             127.0.0.1/32            md5   
# IPv6 local connections:                                             
host    all             all             ::1/128                 md5   
Run Code Online (Sandbox Code Playgroud)

在postgresql.conf中,

listen_addresses = 'localhost, 127.0.0.1, ip_add_postmaster'

Note: ip_add_postmaster is same as my Elastic IP and not public DNS. If this information
matters.
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?计算机托管在Amazon EC2上,并打开了端口5432.

Wol*_*lph 16

正如您的netstat输出所示,它正在侦听127.0.0.1:5432localhost.那只能从localhost连接;)

listen_addresses='*'在您的配置中设置它将起作用.

[edit]要检查的其他事项:

  • 亚马逊防火墙阻止什么?
  • 是iptables阻止什么?

但首先要确保侦听地址正确,你的netstat输出显示它不会像这样工作.

  • 似乎手册中说[对面](http://www.postgresql.org/docs/current/static/runtime-config-connection.html#GUC-LISTEN-ADDRESSES):"该值采用逗号的形式 - 主机名和/或数字IP地址的分隔列表." (3认同)

Nul*_*ion 4

Listen_addresses='localhost, private_ip' 修复了该问题。我无法在弹性 IP 上启动 postmaster 服务器。一旦 postgres 服务器在本地主机和私有 IP 上启动,我就能够连接。