使用 psycopg2 拒绝与 postgresql 的连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39929258/
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
Connection refused with postgresql using psycopg2
提问by M1nt_zwy
psycopg2.OperationalError: could not connect to server: Connection refused
psycopg2.OperationalError:无法连接到服务器:连接被拒绝
Is the server running on host "45.32.1XX.2XX" and accepting TCP/IP connections on port 5432?
服务器是否在主机“45.32.1XX.2XX”上运行并接受端口 5432 上的 TCP/IP 连接?
Here,I've open my sockets.
在这里,我打开了我的套接字。
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 11516/postgres
tcp6 0 0 ::1:5432 :::* LISTEN 11516/postgres
I googled that I should modify this pg_hba.conf
,but in my postgresql
root files, I didn't find this file at all.
我google了一下说我应该修改这个pg_hba.conf
,但是在我的postgresql
根文件中,我根本没有找到这个文件。
Also I've succeed in connecting my another server.
我也成功连接了我的另一台服务器。
Thanks.
谢谢。
Here,I've modified the pg_hba.conf
,updated thishost all all 218.3.A.B trust
and reloaded.But it didn't work either.
在这里,我修改了pg_hba.conf
,更新了这个host all all 218.3.A.B trust
并重新加载。但它也不起作用。
采纳答案by larsks
Your netstat output shows that postgres is listening on 127.0.0.1
, but your error suggests you are trying to connect to 45.32.1XX.2XX
. I am pretty sure you have already diagnosed your problem.
您的 netstat 输出显示 postgres 正在侦听127.0.0.1
,但您的错误表明您正在尝试连接到45.32.1XX.2XX
. 我很确定您已经诊断出您的问题。
You will need to modify the listen_addresses
setting your postgresql.conf
file (notpg_hba.conf
). The postgresql.conf
file is found in your postgresql data directory, which is often something like /var/lib/postgresql/data
or /var/lib/pgsql/data
.
您将需要修改文件的listen_addresses
设置postgresql.conf
(不是pg_hba.conf
)。该postgresql.conf
文件位于您的 postgresql 数据目录中,通常类似于/var/lib/postgresql/data
或/var/lib/pgsql/data
。
The listen_addresses
parameter is documented here.
该listen_addresses
参数记录在此处。
回答by Rt.Tong
1. Modify two configure files
# vi /var/lib/pgsql/data/postgresql.conf
Replace the line:
listen_addresses = 'localhost' -> listen_addresses = '*'
# vi /var/lib/pgsql/data/pg_hba.conf
Add the line at the very end:
host all all 0.0.0.0/0 trust
(If IPv6:
host all all ::/0 trust)
2. Restart the database service
# service postgresql restart
3. Disable the firewall
# rcSuSEfirewall2 stop
# chkconfig SuSEfirewall2 off
# chkconfig SuSEfirewall2_init off