如何配置postgresql远程登录和本地登录

如何配置postgresql远程登录和本地登录

为了能够从另一台机器连接到我的 postgresql 数据库,我必须postgresql.con像这样配置我的 f 文件:

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = '10.14.4.4'                # 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)

我尝试使用 127.0.0.1 但没有成功。 “本地主机”也没有。我能够完成这项工作的唯一方法是使用服务器的实际 IP 地址。我检查以确保在我的“hosts”文件中定义了 localhost......

无论如何,我现在可以通过执行以下操作从不同的服务器进行连接:

psql -U test test -h 10.14.4.4

但现在我发现我无法登录本地使用以下语法:

psql -U test test -h 127.0.0.1

本地登录的唯一方法是

psql -U test test

我尝试将 postgresql.conf 文件更改为使用“*”...这让我可以远程登录,但在本地,我仍然无法使用 127.0.0.1 或“localhost”进行连接。

如何设置才能使远程登录和本地登录都正常工作?

答案1

我必须编辑我的 pg_hba.conf 文件:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                trust
#host    replication     postgres        127.0.0.1/32            trust
#host    replication     postgres        ::1/128                 trust
host    all             all             10.14.4.0/24             md5
host    replication     postgres        10.14.0.0/16              trust

我将方法从“trust”更改为“md5”,这解决了我的问题。

相关内容