【问题标题】:Postgres remote connection - Windows serverPostgres 远程连接 - Windows 服务器
【发布时间】:2019-03-12 01:44:10
【问题描述】:

我在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 就不会启动。

寻求指导。

【问题讨论】:

  • 尝试重启你的 postgres..
  • 试过了,没有成功。我通过 Windows 服务重新启动,但仍然不行。我看到端口也打开了。

标签: postgresql remote-access pg-hba.conf


【解决方案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 服务器上都很有效。

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

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

【讨论】:

    【解决方案2】:

    要打开端口 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
    

    【讨论】:

      【解决方案3】:

      这是不正确的语法:myip 不是 IP 地址,并且它后面的 /32 丢失了。

      【讨论】:

      • 谢谢 - 就是这样。忘记在这里更新了。为您的回答 +1。
      猜你喜欢
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多