windows 如何允许远程访问 PostgreSQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18580066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Allow Remote Access to PostgreSQL database
提问by Ali4356
I have PostgreSQL 9.2 Installed in Windows 7 and I have windows XP installed in Virtual Machine, how do I connect these two databases and allow remote access to add/edit the database from both Systems ?
我在 Windows 7 中安装了 PostgreSQL 9.2,在虚拟机中安装了 Windows XP,如何连接这两个数据库并允许远程访问以从两个系统添加/编辑数据库?
回答by AndreaBoc
In order to remotely access a PostgreSQL database, you must set the two main PostgreSQL configuration files:
为了远程访问 PostgreSQL 数据库,您必须设置两个主要的 PostgreSQL 配置文件:
postgresql.conf
pg_hba.conf
postgresql.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:
open the file postgresql.conf
(usually located in C:\Program Files\PostgreSQL\9.2\data) and sets the parameter
首先配置PostgreSQL服务在Windows 7机器所有网络接口上监听5432端口:
打开文件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.
For example, if the machine with Windows XP have 192.168.56.2 IP address, add in the pg_hba.conf
file:
检查WindowsXP虚拟机的网络地址,并在pg_hba.conf文件(与postgresql.conf同目录)中设置参数,使postgresql可以接受来自虚拟机主机的连接。
例如,如果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). Install pgAdmin on windows XP machine and try to connect to PostgreSQL.
在 Windows 7 中重新启动 PostgreSQL 服务(服务-> PosgreSQL 9.2:右键单击并重新启动服务)。在 Windows XP 机器上安装 pgAdmin 并尝试连接到 PostgreSQL。
回答by S.Hossein Asadollahi
After set listen_addresses = '*'
in postgresql.conf
listen_addresses = '*'
在postgresql.conf 中设置后
Edit the pg_hba.conffile 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 filesthis linkmight help you.
要查找配置文件,此链接可能对您有所帮助。
回答by Jim C
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 (e.g. 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 portsand type in the port your using, usually 5432 >(defaults settings for the rest and type any name you'd like)
前往控制面板\系统和安全\Windows Defender 防火墙 > 高级设置 > 操作(右侧选项卡)> 入站规则 > 新规则... > 端口 > 特定本地端口并输入您使用的端口,通常为 5432 >(默认设置为休息并输入您喜欢的任何名称)
Now, try connecting again from pgAdmin on the client computer. Restarting the service is not required.
现在,尝试从客户端计算机上的 pgAdmin 再次连接。不需要重新启动服务。
回答by 176coding
回答by Jonathan Egerton
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. If you don't use Virtualbox you have to replace the IP address.
这适用于 VirtualBox 和仅主机适配器启用。如果您不使用 Virtualbox,则必须替换 IP 地址。