无法连接到Postgresql

时间:2019-06-19 10:46:42

标签: postgresql psycopg2

确实有类似的问题,但对我没有用。

我在运行Ubuntu Server 16.04的服务器上安装了postgres。

在postgresql.conf中,我已设置:

listen_addresses = '*'                                          
port = 5432

postgres的状态为:

  

●postgresql.service-PostgreSQL RDBMS已加载:已加载   (/lib/systemd/system/postgresql.service;已启用;供应商预设:   活动的:活动的(退出)自UTC 2019-06-19 09:30:25   3分24秒前处理:23683 ExecStart = / bin / true(代码=已退出,   status = 0 / SUCCESS)主PID:23683(代码=已退出,status = 0 / SUCCESS)

     

6月19日09:30:25 ip-172-31-43-128 systemd [1]:启动PostgreSQL   RDBMS ... Jun 19 09:30:25 ip-172-31-43-128 systemd [1]:已启动   PostgreSQL RDBMS。

我在流复制模式下设置了3个从属的postgresql。

我的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             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

host    all             all             172.31.43.128/24        md5
host    replication     all             172.31.32.119/32        trust   # the slave1
host    replication     all             172.31.40.107/32        trust   # the slave2
host    replication     all             172.31.44.8/32          trust   # the slave3

host    postgres        pgpool          172.31.33.203/32        trust   # the client
host    postgres        postgres        172.31.33.203/32        trust   # new
host    all             all             172.31.33.203/32        md5     # the client

我尝试在python中连接到主实例:

import psycopg2
import matplotlib.pyplot as plt
import sys
import time
import pprint

def connect_postgrs():
    try:
        conn = psycopg2.connect("dbname=my_pgpool_db user=postgres password=antonis host=172.31.43.128")  
        print "Connected"
    except psycopg2.Error as e:
        print str(e)

if __name__ == "__main__":
    connect_postgrs()

我得到了错误:

  

无法连接到服务器:连接超时服务器是   在主机“ 172.31.43.128”上运行并接受TCP / IP连接   端口5432?

命令:netstat -nlt提供以下输出:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 172.31.43.128:5432      0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN

有人知道我在做什么错吗?

2 个答案:

答案 0 :(得分:0)

设置listen_addresses = '*'后,您是否重新启动了Postgres?

通常,您会看到

tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN

代替

tcp 0 0 172.31.43.128:5432 0.0.0.0:* LISTEN

答案 1 :(得分:0)

您可能需要在pg_hba.conf中更改此条目

    host    all             all             172.31.43.128/24        md5

到以下任何一个

    host    all             all             172.31.43.0/24        md5
    host    all             all             172.31.43.128/32        md5

然后重新加载您的配置:

  sudo service postgresql reload