title: Vagrant - How to access guests postgres server from host
body: <p>I have a vagrant box installed with a postgres server running on it on port <code>5432</code>, and I added this to the <code>Vagrantfile</code>:</p>
  
  <pre><code>  config.vm.network "private_network", ip: "192.168.33.33"
    config.vm.network "forwarded_port", guest: 5432, host: 3333
  </code></pre>
  
  <p>But when I try to connect to the postgres server by connecting to <code>192.168.33.33:3333</code></p>
  
  <p>I get this error:</p>
  
  <p><a href="https://i.stack.imgur.com/e0Pgv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e0Pgv.png" alt="enter image description here"></a></p>
  
  <p>Connection refused, this is the same error I'll get if I try to connect to any ports that aren't open. I ran a port scanner on 192.168.33.33 and didn't see any of the forwarded ports open. This is actually what I have in the Vagrantfile:</p>
  
  <pre><code>  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
  </code></pre>
  
  <p>and heres the results from the port scanner:</p>
  
  <p><a href="https://i.stack.imgur.com/aejrh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aejrh.png" alt="enter image description here"></a></p>
  
  <p>It didn't detect any ports above 995. How do I access the VMs postgres server? </p>
  
  <p><strong>UPDATE:</strong>
  I found a way to connect via localhost (charlies suggestion), it seems that <code>config.vm.network "forwarded_port"</code> only forwards ports to <code>localhost</code>. Additionally, I followed this guide:
  <a href="https://pseudoscripter.wordpress.com/2016/04/26/allow-remote-hosts-to-connect-to-postgresql-database-server-on-vagrant-box/" rel="nofollow noreferrer">https://pseudoscripter.wordpress.com/2016/04/26/allow-remote-hosts-to-connect-to-postgresql-database-server-on-vagrant-box/</a></p>
  
  <p>and edited <code>postgresql.conf</code> so that it listens to all IPs, by adding this line:</p>
  
  <pre><code>listen_addresses = '*'         # what IP address(es) to listen on;
  </code></pre>
  
  <p>and edited <code>pg_hba.conf</code> to allow the guests IP address:</p>
  
  <pre><code>host    all             all             192.168.33.33/24        md5
  </code></pre>
  
  <p>^^ That step probably has nothing to do with why it works on <code>localhost:3333</code>. The steps from that guide didn't work connecting on <code>192.168.33.33:3333</code>, but I can connect on <code>localhost:3333</code>.</p>
  
  <p><strong>SOLVED</strong>
  Rather than trying to access the server on the guest IP via the forwarded ports, it can be accessed on the guests port, so I can access the server on <code>192.168.33.33:5432</code> now. This didn't work for me when I first tried, so the steps in UPDATE 1 were required to make it accessible. </p>