简体   繁体   English

如何允许远程访问 PostgreSQL 数据库

[英]How to Allow Remote Access to PostgreSQL database

我在 Windows 7 中安装了 PostgreSQL 9.2,在虚拟机中安装了 Windows XP,如何连接这两个数据库并允许远程访问以从两个系统添加/编辑数据库?

In order to remotely access a PostgreSQL database, you must set the two main PostgreSQL configuration files:为了远程访问 PostgreSQL 数据库,您必须设置两个主要的 PostgreSQL 配置文件:

postgresql.conf配置文件
pg_hba.conf pg_hba.conf

Here is a brief description about how you can set them (note that the following description is purely indicative: To configure a machine safely, you must be familiar with all the parameters and their meanings)这里是关于如何设置它们的简要说明(请注意,以下说明纯粹是指示性的:要安全地配置机器,您必须熟悉所有参数及其含义)

First of all configure PostgreSQL service to listen on port 5432 on all network interfaces in Windows 7 machine:首先配置 PostgreSQL 服务以侦听 Windows 7 机器上所有网络接口上的端口 5432:
open the file postgresql.conf (usually located in C:\\Program Files\\PostgreSQL\\9.2\\data) and sets the parameter打开文件postgresql.conf (通常位于C:\\Program Files\\PostgreSQL\\9.2\\data)并设置参数

listen_addresses = '*'

Check the network address of WindowsXP virtual machine, and sets parameters in pg_hba.conf file (located in the same directory of postgresql.conf) so that postgresql can accept connections from virtual machine hosts.检查WindowsXP虚拟机的网络地址,并在pg_hba.conf文件(与postgresql.conf同目录)中设置参数,使postgresql可以接受来自虚拟机主机的连接。
For example, if the machine with Windows XP have 192.168.56.2 IP address, add in the pg_hba.conf file:例如,如果Windows XP的机器有192.168.56.2 IP地址,则在pg_hba.conf文件中添加:

host all all 192.168.56.1/24 md5

this way, PostgreSQL will accept connections from all hosts on the network 192.168.1.XXX.这样,PostgreSQL 将接受来自网络 192.168.1.XXX 上所有主机的连接。

Restart the PostgreSQL service in Windows 7 (Services-> PosgreSQL 9.2: right click and restart sevice).在 Windows 7 中重新启动 PostgreSQL 服务(服务-> PosgreSQL 9.2:右键单击并重新启动服务)。 Install pgAdmin on windows XP machine and try to connect to PostgreSQL.在 Windows XP 机器上安装 pgAdmin 并尝试连接到 PostgreSQL。

After set listen_addresses = '*' in postgresql.confpostgresql.conf 中设置listen_addresses = '*'

Edit the pg_hba.conf file and add the following entry at the very end of file:编辑pg_hba.conf文件并在文件的最后添加以下条目:

host    all             all              0.0.0.0/0                       md5
host    all             all              ::/0                            md5

For finding the config files this link might help you.查找配置文件, 此链接可能对您有所帮助。

In addition to above answers suggesting (1) the modification of the configuration files pg_hba.conf and (2) postgresql.conf and (3) restarting the PostgreSQL service, some Windows computers might also require incoming TCP traffic to be allowed on the port (usually 5432 ).除了上述建议 (1) 修改配置文件 pg_hba.conf 和 (2) postgresql.conf 和 (3) 重新启动 PostgreSQL 服务之外,某些 Windows 计算机可能还需要在端口上允许传入 TCP 流量(通常为5432 )。

To do this, you would need to open Windows Firewall and add an inbound rule for the port (eg 5432).为此,您需要打开 Windows 防火墙并为端口添加入站规则(例如 5432)。

Head to Control Panel\\System and Security\\Windows Defender Firewall > Advanced Settings > Actions (right tab) > Inbound Rules > New Rule… > Port > Specific local ports and type in the port your using, usually 5432 > (defaults settings for the rest and type any name you'd like)前往控制面板\\系统和安全\\Windows Defender 防火墙 > 高级设置 > 操作(右侧选项卡)> 入站规则 > 新规则... > 端口 > 特定本地端口并输入您使用的端口,通常为 5432 > (默认设置为休息并输入您喜欢的任何名称)

Windows 防火墙设置

Now, try connecting again from pgAdmin on the client computer.现在,尝试从客户端计算机上的 pgAdmin 再次连接。 Restarting the service is not required.不需要重新启动服务。

If using PostgreSql 9.5.1, please follow the below configuration:如果使用 PostgreSql 9.5.1,请按照以下配置:

  1. Open hg_hba.conf in pgAdmin在 pgAdmin 中打开 hg_hba.conf 管理员
  2. Select your path, and open it, then add a setting选择你的路径,并打开它,然后添加一个设置pg_hba.conf
  3. Restart postgresql service重启 postgresql 服务

You have to add this to your pg_hba.conf and restart your PostgreSQL.您必须将其添加到 pg_hba.conf 并重新启动 PostgreSQL。

host all all 192.168.56.1/24 md5托管所有所有 192.168.56.1/24 md5

This works with VirtualBox and host-only adapter enabled.这适用于 VirtualBox 和仅主机适配器启用。 If you don't use Virtualbox you have to replace the IP address.如果您不使用 Virtualbox,则必须替换 IP 地址。

This is a complementary answer for the specific case of you using AWS cloud computing (either EC2 or RDS machines).这是针对您使用 AWS 云计算(EC2 或 RDS 机器)的特定情况的补充答案。

Besides doing everything proposed above, when using AWS cloud computing you will need to set you inbound rules in a way that let you access to the ports.除了执行上面建议的所有操作之外,在使用 AWS 云计算时,您还需要以一种允许您访问端口的方式设置入站规则。 Please check this post , which is valid for EC2 and RDS.请查看这篇文章,它对 EC2 和 RDS 有效。

For PostgreSQL 13, I could not use scram-sha-256 encryption for remote connections for some reason.对于 PostgreSQL 13,出于某种原因,我无法对远程连接使用 scram-sha-256 加密。 This worked.这奏效了。

# TYPE  DATABASE        USER            ADDRESS                 METHOD
local   all             all                                     scram-sha-256 # "local" is for Unix domain socket connections only
host    all             all             127.0.0.1/32            scram-sha-256 # IPv4 local connections:
host    all             all             ::1/128                 scram-sha-256 # IPv6 local connections
local   replication     all                                     scram-sha-256 # Allow replication connections from localhost, by a user with the replication privilege.
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256
host    all             all             0.0.0.0/0               trust # <---------- remote connections

A fast shortcut for restarting service on Windows:在 Windows 上重新启动服务的快速快捷方式:

1) Press Windows Key + R 1)按Windows键+ R

2) Type "services.msc" 2) 输入“services.msc”

在此处输入图片说明

3) Order by name 3) 按名称排序

4) Find "PostgreSQL" service and restart it. 4) 找到“PostgreSQL”服务并重新启动它。

在此处输入图片说明

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

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