0

我在pg_hba.conf. Postgres 安装在 Windows 服务器上。

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
#host   all             all             myip            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5

我想只允许来自另外一个公共 IP 地址的连接。我怎样才能做到这一点?一旦我启用了上面的线路 IP: Postgres 就不会启动。

寻求一些指导。

4

3 回答 3

1

要打开端口 5432,请编辑您的/Program Files/PostgreSQL/10/data/postgresql.conf并更改

# Connection Settings -

listen_addresses = '*'          # what IP address(es) to listen on;

/Program Files/PostgreSQL/10/data/pg_hba.conf

# IPv4 local connections:
host    all             all             0.0.0.0/0           md5

现在重述 Postgres 服务器使用 cmd

pg_ctl -D "C:\Program Files\PostgreSQL\10\data" restart
于 2020-08-10T06:24:33.057 回答
1

我在几个 Windows 服务器(Windows Server 2012 R2、Windows Server 2016)上运行 postgres,作为在 Apache tomcat 网络服务器上运行的商业框架的一部分。本地连接工作正常。但是,我希望同一服务器场上的另一台服务器(Red Hat Enterprise Linux)上的另一个框架(Cakephp)可以访问同一个 postgres 服务器。这在我升级到 postgres 9 之前一直有效。现在我不得不升级到 postgres 10。无论我尝试什么,我都失败了。这是我为解决问题所做的:找到您本地的 postgres 配置文件。它们通常与 postgres 表空间位于同一目录中,在我的例子中:d:\PG10Data\postgresql.conf。该文件必须包含以下几行:

# - Connection Settings:
listen_addresses = '*'  # what IP addresses/interfaces to listen on
port = 5432

下一个要修改的文件是 pg_hba.conf(hba = 基于主机的访问):

# TYPE  DATABASE        USER            ADDRESS                 METHOD
# some sample entries:
# this will open UP ALL REMOTE IPv4 connections, do not open up permanently
host    all             all             0.0.0.0/0               md5
# only 1 database for 1 user from 1 IPv4 address:
host    yourdatabasename    yourusername        10.15.17.13/32          md5
# don not forget the "/32", otherwise the postgres server will not start up!

编辑这些文件后,重新启动 postgres 服务器。你可以跑

netstat -a -n | findstr 5432

查看 postgres 侦听器是否正在运行。

您还可以从 Windows 命令提示符运行以下命令来测试连接性:

psql -Uyourusername -dyourdatabasename -p5432 -hlocalhost

这应该在任何时候都有效。下一级将使用您计算机的本地 IPv4 地址。这你可以找到

ipconfig

这将告诉您您的本地 IPv4 地址。在以下命令中使用它:

psql -Uyourusername -dyourdatabasename -p5432 -hyourlocalip

我的问题是,这个命令失败了。由于我直接在我的服务器上运行它,它不可能是本地 Windows 防火墙。

解决方案:有第二个配置文件:d:\PG10Data\postgresql.auto.conf 该文件以以下两行开头不祥:

# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command

它结束了:

listen_addresses = 'localhost'

当我通过 psql 作为 postgres 在本地登录时,我尝试了 ALTER SYSTEM 命令,但没有成功。最后我厚着脸皮把 d:\PG10Data\postgresql.auto.conf 中的条目改成了;

listen_addresses = '*'

答对了!在 postgres 重新启动后,远程访问在两台 Windows 服务器上都很有效。

注意不要忘记 Windows 防火墙:打开端口 5432 进行远程访问。还要检查网络中没有防火墙阻止远程客户端访问端口 5432。

如果有人能够告诉我应该如何在不编辑 postgresql.auto.conf 的情况下更改参数,那就太好了,但至少我的两个框架 - 在本地和远程服务器上 - 都在工作。

于 2019-10-20T13:53:31.507 回答
0

那是不正确的语法:myip不是 IP 地址,并且/32缺少它之后。

于 2019-03-12T07:16:05.570 回答