无法远程连接到Postgres数据库

时间:2018-08-15 19:03:04

标签: postgresql ubuntu-16.04 postgresql-9.5

在Ubuntu 16.04上运行Postgresql 9.5。

我的/etc/postgresql/9.5/main/pg_hba.conf:

local   all             postgres                                peer

# 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
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5
#remote connection 
host    all             all            0.0.0.0/0              trust

我的/etc/postgresql/9.5/main/postgresql.conf:

# - Connection Settings -

listen_addresses = '*'      # what IP address(es) to listen on;
                    # comma-separated list of addresses;
                    # defaults to 'localhost'; use '*' for all
                    # (change requires restart)
port = 5432             # (change requires restart)
max_connections = 100           # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
unix_socket_directories = '/var/run/postgresql' # comma-separated list of directories
                    # (change requires restart)
#unix_socket_group = ''         # (change requires restart)
#unix_socket_permissions = 0777     # begin with 0 to use octal notation
                    # (change requires restart)
#bonjour = off              # advertise server via Bonjour
                    # (change requires restart)
#bonjour_name = ''          # defaults to the computer name
                    # (change requires restart)

# - Security and Authentication -

#authentication_timeout = 1min      # 1s-600s
#ssl = true             # (change requires restart)
#ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers
                    # (change requires restart)

我的netstat -nlp | grep 5432:

tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      22621/postgres  
tcp6       0      0 :::5432                 :::*                    LISTEN      22621/postgres  
unix  2      [ ACC ]     STREAM     LISTENING     10261951 22621/postgres      /var/run/postgresql/.s.PGSQL.5432

我可以本地连接,但是不能远程连接。当我尝试从另一台计算机连接时,该命令超时。

1 个答案:

答案 0 :(得分:0)

确保您没有与阻塞5432的数据库在同一主机上运行的防火墙。我不是Ubuntu向导,但我认为您可以做到

ufw disable

将其关闭。如果不行,请尝试

service iptables stop

或可能

iptables -F

请注意,这些当然会关闭防火墙,因此请注意。最后一个将刷新防火墙规则,因此如果要重新打开它,则需要重新阅读规则集。

并按照@Pancake的建议,看看如果您只是简单地从另一台主机telnet到5432(或使用netcat等),会发生什么情况。