14

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.

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

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.

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.

1
  • 1
    Always show the PostgreSQL version and the exact text of the error message Aug 8, 2013 at 12:53

3 Answers 3

12

Solved.

Replacing:

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

0
5

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

host    all    all    10.0.0.0/16    md5

and then restart:

sudo /etc/init.d/postgresql restart
1
  • 1
    This should be the accepted answer. I think Virtualbox gives NAT hosts addresses in the 10.0.0.0/16 range, so this configuration works while being more secure than letting all connections in (0.0.0.0/0). Mar 23, 2018 at 20:48
5

The Solution by Filip works, but you can tailor it further.
First, enable Adapter 2 in VM and set it to Host-only Adapter:

enter image description here

Second go to your host machine and find it's ip address.
This can be found by running ipconfig in your windows host machine.

Now you need to edit two files in your VMBox.

First is postgresql.conf

sudo nano /etc/postgresql/<version>/main/postgresql.conf

and add the following line:

listen_addresses = '*'

save it and then edit pg_hba.conf

sudo nano /etc/postgresql/<version>/main/pg_hba.conf

Here you need to add your host machine ip (in my case it was 192.168.56.1:

host    all             all             192.168.56.1/0          trust

Save it and restart postgresql

sudo /etc/init.d/postgresql restart

Now you can use pgadmin to connect to vm postgresql.

Convenience!

2
  • I had a similar issue but the other way. I had my PostgreSQL setup in Windows and Linux was running on VBox. Steps 1/2 helped here as I could find my IPConfig of the network connection for the VBox and then use that as the Postgres server address to connect to. The rest setup of tables/users/etc... in the windows postgres was ok, just couldn't see it from VBox linux.
    – Angelo
    Apr 13, 2022 at 5:59
  • Does the IP Mask /0 has a special use case in the ph_hba.conf file ? Because a 0bit prefix length would just mean a subnet mask of 0.0.0.0, basically allowing ANY IPv4 and not just the host's one ? 192.168.0.0/16 should allow any machine on the local network to connect.
    – A. Delran
    Aug 1 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.