繁体   English   中英

无法连接到Postgresql

[英]Can't connect to Postgresql

有类似的问题,但对我来说没有任何作用。

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

在postgresql.conf我设置:

listen_addresses = '*'                                          
port = 5432

postgres的状态给出:

●postgresql.service - PostgreSQL RDBMS已加载:已加载(/lib/systemd/system/postgresql.service;已启用;供应商预设:已启用)活动:已激活(已退出)自2019-06-19 Wed 09:30:25 UTC; 3min 24s ago Process:23683 ExecStart = / bin / true(code = exited,status = 0 / SUCCESS)主PID:23683(代码=退出,状态= 0 /成功)

Jun 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。

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

我的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中的master实例:

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”上运行并接受端口5432上的TCP / IP连接?

命令: 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

有谁知道我做错了什么?

设置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

您可能需要在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM