Vagrant - How to access guest Postgres server from host


Horse O'Houlihan

I installed a boring box with a postgres server running on a port 5432and added it to Vagrantfile:

  config.vm.network "private_network", ip: "192.168.33.33"
  config.vm.network "forwarded_port", guest: 5432, host: 3333

But when I try to connect to the Postgres server via192.168.33.33:3333

I get this error:

enter image description here

Connection refused and if I try to connect to any port that is not open I get the same error. I ran a port scanner on 192.168.33.33 and it didn't find any forwarded ports open. This is actually what I have in the Vagrantfile:

  config.vm.network "forwarded_port", guest: 8000, host: 8001
  config.vm.network "forwarded_port", guest: 3333, host: 3334
  config.vm.network "forwarded_port", guest: 5555, host: 5556
  config.vm.network "forwarded_port", guest: 5432, host: 3333

  config.vm.network "forwarded_port", guest: 80, host: 8888,
    auto_correct: true

Here are the results of the port scanner:

enter image description here

It doesn't detect ports higher than 995. How to access VM postgres server?

UPDATE: I found a way to connect via localhost (Char suggested) and it seems to config.vm.network "forwarded_port"only forward the port to localhost. Also, I followed this guide : https://pseudoscripter.wordpress.com/2016/04/26/allow-remote-hosts-to-connect-to-postgresql-database-server-on-vagrant-box/

and edit postgresql.confit to add everything to listen on all IPs:

listen_addresses = '*'         # what IP address(es) to listen on;

and modified pg_hba.confto allow guest IP addresses:

host    all             all             192.168.33.33/24        md5

^^ This step probably has nothing to do with why it works localhost:3333. The steps in this guide cannot be continued 192.168.33.33:3333, but I can localhost:3333.

Solved instead of trying to access the server on the guest IP through the port forwarded, I can access it on the guest port, so I can now access the server 192.168.33.33:5432. This didn't work for me when I tried it for the first time, so the steps in UPDATE 1 were required to access it.

Charlie

Reading your question again, I noticed the private_networkconfiguration. This gives your guest host an IP, so you should be able to connect to 192.168.33.33:5432 . PostgreSQL is listening on 5432 in the VM. Be careful, your host should be on the same network to be accessible. If not, you will need to add a static route between the host and the VM.

This forwarded_portoption will map the port on the host and the port on the guest, so

config.vm.network "forwarded_port", guest: 5432, host: 3333

will map localhost:3333(or any IP in the host) to .192.168.33.33:5432

Regarding the pg_hba.confconfiguration, it prevents unauthorized access, but you can connect to the server, so the error message will be different.

Related


Vagrant - How to access guest Postgres server from host

Horse O'Houlihan I installed a boring box with a postgres server running on a port 5432and added it to Vagrantfile: config.vm.network "private_network", ip: "192.168.33.33" config.vm.network "forwarded_port", guest: 5432, host: 3333 But when I try to conn

Vagrant - How to access guest Postgres server from host

Horse O'Houlihan I installed a boring box with a postgres server running on a port 5432and added it to Vagrantfile: config.vm.network "private_network", ip: "192.168.33.33" config.vm.network "forwarded_port", guest: 5432, host: 3333 But when I try to conn

Vagrant - How to access guest Postgres server from host

Horse O'Houlihan I installed a boring box with a postgres server running on a port 5432and added it to Vagrantfile: config.vm.network "private_network", ip: "192.168.33.33" config.vm.network "forwarded_port", guest: 5432, host: 3333 But when I try to conn

Vagrant - How to access guest Postgres server from host

Horse O'Houlihan I installed a boring box with a postgres server running on a port 5432and added it to Vagrantfile: config.vm.network "private_network", ip: "192.168.33.33" config.vm.network "forwarded_port", guest: 5432, host: 3333 But when I try to conn

connect from guest vagrant box to postgres db in docker on host

Dallas Kelly Not sure if this is possible, but I have a Vagrant box running ubuntu 18.04 running in VirtualBox on my macbook. I also have a docker container on my mac with a postgres database in it. I want to be able to connect to the database in docker from i

connect from guest vagrant box to postgres db in docker on host

Dallas Kelly Not sure if this is possible, but I have a Vagrant box running ubuntu 18.04 running in VirtualBox on my macbook. I also have a docker container on my mac with a postgres database in it. I want to be able to connect to the database in docker from i

How to access a vagrant guest from another virtualbox guest?

Adrian Garner The scenario is that my development environment is on a Vagrant box on a laptop (host) and I want to do browser testing in a vitualbox vm, so I need to look at another vm. The port of the vagrant box is: 8080, which is forwarded to the host on th

How to access a vagrant guest from another virtualbox guest?

Adrian Garner The scenario is that my development environment is on a Vagrant box on a laptop (host) and I want to do browser testing in a vitualbox vm, so I need to look at another vm. The port of the vagrant box is: 8080, which is forwarded to the host on th

How to access a vagrant guest from another virtualbox guest?

Adrian Garner The scenario is that my development environment is on a Vagrant box on a laptop (host) and I want to do browser testing in a vitualbox vm, so I need to look at another vm. The port of the vagrant box is: 8080, which is forwarded to the host on th

How to access a vagrant guest from another virtualbox guest?

Adrian Garner The scenario is that my development environment is on a Vagrant box on a laptop (host) and I want to do browser testing in a vitualbox vm, so I need to look at another vm. The port of the vagrant box is: 8080, which is forwarded to the host on th

How to access Apache from host (on VirtualBox guest)?

urok93 I have Apache installed on an Ubuntu VM. When I go into the client machine and load Firefox, I can see that Apache is running fine when I browse to localhost. I want to access the same page through the host. I tried using the guest's IP address, but not

How to access Apache from host (on VirtualBox guest)?

urok93 I have Apache installed on an Ubuntu VM. When I go into the client machine and load Firefox, I can see that Apache is running fine when I browse to localhost. I want to access the same page through the host. I tried using the guest's IP address, but not

Vagrant: How to sync folders from guest back to host?

kakat Why, because it is difficult to work and edit the code through the legacy editor in the guest. The whole point of vagrant is to make it easier for developers, right :)? So please can someone guide me in this case: Need to work on a project. This is the g

Vagrant: How to sync folders from guest back to host?

kakat Why, because it is difficult to work and edit the code through the legacy editor in the guest. The whole point of vagrant is to make it easier for developers, right :)? So please can someone guide me in this case: Need to work on a project. This is the g

Vagrant: How to sync folders from guest back to host?

kakat Why, because it is difficult to work and edit the code through the legacy editor in the guest. The whole point of vagrant is to make it easier for developers, right :)? So please can someone guide me in this case: Need to work on a project. This is the g

Vagrant: How to sync folders from guest back to host?

kakat Why, because it is difficult to work and edit the code through the legacy editor in the guest. The whole point of vagrant is to make it easier for developers, right :)? So please can someone guide me in this case: Need to work on a project. This is the g

Set ENV variable from host to guest in Vagrant

Romeo Michalcia Is there any way to set some ENV variables on boot from host to guest Vagrant OS? From the guest OS, I need to connect to the database and I want to pass the database URL from the host to the guest along with other parameters. I tried that, TES

Set ENV variable from host to guest in Vagrant

Romeo Mihalcea Is there any way to set certain ENV variables from the host to the guest Vagrant OS when starting the host? From my guest OS, I need to connect to the database and I want to pass the database URL from the host to the guest along with other param

Set ENV variable from host to guest in Vagrant

Romeo Mihalcea Is there any way to set certain ENV variables from the host to the guest Vagrant OS when starting the host? From my guest OS, I need to connect to the database and I want to pass the database URL from the host to the guest along with other param