从 Win7 连接到 Linux VirtualBox 中的 PostgreSql 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18121666/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:03:32  来源:igfitidea点击:

Connect to PostgreSql database in Linux VirtualBox from Win7

linuxpostgresqlwindows-7virtualbox

提问by Filip

As said in headline, from Win7 host I'm trying to access Postgres 9.3 established in Linux Centos 5.8 which is in VirtualBox on the same machine. I'm trying to access it from PGAdmin and everything is OK when I start the Postgre from Win7 services, so PGAdmin is well configured.

正如标题中所说,我试图从 Win7 主机访问在 Linux Centos 5.8 中建立的 Postgres 9.3,它位于同一台机器上的 VirtualBox 中。我试图从 PGAdmin 访问它,当我从 Win7 服务启动 Postgre 时一切正常,因此 PGAdmin 配置良好。

What have I tried? I've read many articles about this subject, and even some questions on this forum but nothing worked. I have:

我试过什么?我已经阅读了很多关于这个主题的文章,甚至在这个论坛上也有一些问题,但没有任何效果。我有:

  1. switched to NAT and forwarded port 5432 in VirtualBox GUI
  2. set listenadresses = '*' in postgresql.conf file
  3. put host all all 10.0.2.1/24 md5 line in the pg_hba.conf file
  4. put 5432 port inbound and outbound rule in win7 firewall settings
  5. disabled linux firewall with #service iptables stop
  1. 在 VirtualBox GUI 中切换到 NAT 并转发端口 5432
  2. 在 postgresql.conf 文件中设置监听地址 = '*'
  3. 将主机所有 10.0.2.1/24 md5 行放在 pg_hba.conf 文件中
  4. 将 5432 端口入站和出站规则放在 win7 防火墙设置中
  5. 使用 #service iptables stop 禁用 linux 防火墙

Just to mention. When service is started in virtual linux, I can access it from linux, so service is properly started. Problem is that windows doesn't see that service. And when service is started from linux, I can start the same service in Win and vice-versa although the port 5432 should be occupied.

顺便提一下。当服务在虚拟linux中启动时,我可以从linux访问它,所以服务正常启动。问题是 Windows 没有看到该服务。当从linux启动服务时,我可以在Win中启动相同的服务,反之亦然,尽管端口5432应该被占用。

The most suspicious part to me is point 3) because I'm not sure whether i have put good address in rule. That address vary from article to article, and I would appreciate if someone could explain me how to be sure which address (or range) to put there, according to my network. Or some other advice if possible. Thanks.

对我来说最可疑的部分是第 3) 点,因为我不确定我是否在规则中设置了正确的地址。根据我的网络,该地址因文章而异,如果有人能解释我如何确定将哪个地址(或范围)放在那里,我将不胜感激。如果可能的话,或者其他一些建议。谢谢。

采纳答案by Filip

Solved.

解决了。

Replacing:

更换:

"host all all 10.0.2.1/24 md5" with "host all all 0.0.0.0/0 trust" solved it.

“host all all 10.0.2.1/24 md5”和“host all all 0.0.0.0/0 trust”解决了这个问题。

回答by Lukasz Wiktor

In my case adding the below line to pg_hba.confwas enough:

在我的情况下,添加以下行pg_hba.conf就足够了:

host    all    all    10.0.0.0/16    md5

and then restart:

然后重新启动:

sudo /etc/init.d/postgresql restart