无法在端口 5432 上连接到 Postgresql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38466190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't connect to Postgresql on port 5432
提问by Rodrigo
I have PostgreSQL 9.3 installed on a server running Ubuntu Server 14.04.
我在运行 Ubuntu Server 14.04 的服务器上安装了 PostgreSQL 9.3。
If I ssh into the server via terminal, I'm able to connect with psql. But when I try to configure pgAdmin III to do the remote connection, I get:
如果我通过终端 ssh 进入服务器,我就可以连接 psql。但是当我尝试配置 pgAdmin III 进行远程连接时,我得到:
Server doesn't listen The server doesn't accept connections: the connection library reports could not connect to server: Connection refused Is the server running on host "172.24.3.147" and accepting TCP/IP connections on port 5432?
服务器不侦听服务器不接受连接:连接库报告无法连接到服务器:连接被拒绝 服务器是否在主机“172.24.3.147”上运行并接受端口 5432 上的 TCP/IP 连接?
When I run on the server service postgresql status
it gives me:
当我在服务器上运行时,service postgresql status
它给了我:
9.3/main (port 5432): online
So of course I'm missing something important here.
所以当然我在这里遗漏了一些重要的东西。
EDIT
编辑
When running netstat -na
on the server, I get (relevant portion, I guess):
在netstat -na
服务器上运行时,我得到(相关部分,我猜):
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
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 172.24.3.147:22 172.24.3.240:61950 ESTABLISHED
tcp 0 0 172.24.3.147:22 172.24.3.240:60214 ESTABLISHED
回答by Jan Marek
You have to edit postgresql.conffile and change line with 'listen_addresses'.
您必须编辑postgresql.conf文件并使用“listen_addresses”更改行。
This file you can find in the /etc/postgresql/9.3/main
directory.
您可以在/etc/postgresql/9.3/main
目录中找到该文件。
Default Ubuntu config have allowed only localhost (or 127.0.0.1) interface, which is sufficient for using, when every PostgreSQL client work on the same computer, as PostgreSQL server. If you want connect PostgreSQL server from other computers, you have change this config line in this way:
默认的 Ubuntu 配置只允许 localhost(或 127.0.0.1)接口,当每个 PostgreSQL 客户端在同一台计算机上工作时,这足以使用,作为 PostgreSQL 服务器。如果您想从其他计算机连接 PostgreSQL 服务器,您可以通过这种方式更改此配置行:
listen_addresses = '*'
Then you have edit pg_hba.conffile, too. In this file you have set, from which computers you can connect to this server and what method of authentication you can use. Usually you will need similar line:
然后你也编辑了pg_hba.conf文件。在此文件中,您已设置可以从哪些计算机连接到此服务器以及可以使用哪种身份验证方法。通常你需要类似的行:
host all all 192.168.1.0/24 md5
Please, read comments in this file...
请阅读此文件中的评论...
EDIT:
编辑:
After the editing postgresql.conf and pg_hba.conf you have to restart postgresql server.
编辑 postgresql.conf 和 pg_hba.conf 后,您必须重新启动 postgresql 服务器。
EDIT2: Highlited configuration files.
EDIT2:高亮配置文件。
回答by San Emmanuel James
This has bitten me a second time so I thought might be worth mentioning. The line listen_addresses = '*' in the postgresql.conf is by default commented. Be sure to uncomment (remove the pound sign, # at the beginning) it after updating otherwise, remote connections will continue to be blocked.
这让我第二次被咬了,所以我认为可能值得一提。postgresql.conf 中的 listen_addresses = '*' 行默认被注释。请务必在更新后取消注释(删除开头的井号,#),否则,远程连接将继续被阻止。
回答by max56
Had same problem with psql via command line connecting and pgAdmin not connecting on RDS with AWS. I did have my RDS set to Publicly Accessible. I made sure my ACL and security groups were wide open and still problem so, I did the following:
sudo find . -name *.conf
then sudo nano ./data/pg_hba.conf
then added to top of directives in pg_hba.conf file host all all 0.0.0.0/0 md5
and pgAdmin automatically logged me in.
通过命令行连接 psql 有同样的问题,而 pgAdmin 没有在 RDS 上与 AWS 连接。我确实将 RDS 设置为公开访问。我确信我的ACL和安全组是敞开的,仍然问题,所以,我做了以下内容:
sudo find . -name *.conf
然后sudo nano ./data/pg_hba.conf
再加入到pg_hba.conf文件指令的顶部host all all 0.0.0.0/0 md5
和pgAdmin的自动登录我进去。
This also worked in pg_hba.conf file
host all all md5
without any IP address and this also worked with my IP address host all all <myip>/32 md5
这也适用于host all all md5
没有任何 IP 地址的pg_hba.conf 文件
,这也适用于我的 IP 地址host all all <myip>/32 md5
As a side note, my RDS was in my default VPC. I had an identical RDS instance in my non-default VPC with identical security group, ACL and security group settings to my default VPC and I could not get it to work. Not sure why but, that's for another day.
作为旁注,我的 RDS 在我的默认 VPC 中。我的非默认 VPC 中有一个相同的 RDS 实例,其安全组、ACL 和安全组设置与我的默认 VPC 相同,但我无法使其工作。不知道为什么,但那是另一天。
回答by Alex
Remember to check firewall settings as well. after checking and double-checking my pg_hba.conf
and postgres.conf
files I finally found out that my firewall was overriding everything and therefore blocking connections
请记住还要检查防火墙设置。检查并仔细检查我的pg_hba.conf
和postgres.conf
文件后,我终于发现我的防火墙覆盖了所有内容,因此阻止了连接
回答by Adrian Stanica
I had the same problem after a MacOS system upgrade. Solved it by upgrading the postgres with brew. Details: it looks like the system was trying to access Postgres 11 using older Postgres 10 settings. I'm sure it was my mistake somewhere in the past, but luckily it all got sorted out with the upgrade above.
MacOS系统升级后我遇到了同样的问题。通过使用 brew 升级 postgres 解决了它。详细信息:看起来系统正在尝试使用较旧的 Postgres 10 设置访问 Postgres 11。我确定这是我过去某个地方的错误,但幸运的是,通过上面的升级,这一切都得到了解决。
回答by Daniel Kravetz Malabud
You probably need to either open up the port to access it in your LAN (or outside of it) or bind the network address to the port (make PostgreSQL listen on your LAN instead of just on localhost)
您可能需要打开端口以在您的 LAN(或外部)中访问它,或者将网络地址绑定到该端口(让 PostgreSQL 在您的 LAN 上而不是仅在 localhost 上侦听)