title: Postgres connection failing
body: <p>Trying to connect to a remote vm database via port forwarding. The VM host is a remote server (Ubuntu x64), guest VM is Ubuntu x64, and port forwarding is set up for 15432 => 5432 (host => guest). </p>
  
  <p>When I try to connect via psql or pgAdmin3, I get a connection refused error.</p>
  
  <p>psql command from local machine:</p>
  
  <pre><code>psql -h remote.server.address -p 15432 -U postgres database_name
  </code></pre>
  
  <p>response:</p>
  
  <pre><code>psql: server closed the connection unexpectedly
  This probably means the server terminated abnormally
  before or while processing the request.
  </code></pre>
  
  <p>postgresql.conf listen_addresses is set to all:</p>
  
  <pre><code>$ cat /etc/postgresql/9.4/main/postgresql.conf | grep listen
  listen_addresses = '*'      # what IP address(es) to listen on;
  </code></pre>
  
  <p>My pg_hba.conf:</p>
  
  <pre><code># Database administrative login by Unix domain socket
  local   all             postgres                                peer
  
  # TYPE  DATABASE        USER            ADDRESS                 METHOD
  
  # "local" is for Unix domain socket connections only
  local   all             all                                     peer
  # IPv4 local connections:
  host    all             all             127.0.0.1/32            md5
  host    all             all             10.0.0.0/16            md5
  host    all             all             0.0.0.0/0            md5
  # IPv6 local connections:
  host    all             all             ::1/128                 md5
  # Allow replication connections from localhost, by a user with the
  # replication privilege.
  #local   replication     postgres                                peer
  #host    replication     postgres        127.0.0.1/32            md5
  #host    replication     postgres        ::1/128                 md5
  
  host    informatics     nmr_user         192.168.20.223/32          md5
  host    all         all         192.168.8.0/22          md5
  host    all         all         192.168.50.0/24         md5
  host    all         all         192.168.7.0/24          md5
  host    all         all         192.168.4.0/24          md5
  host    all         all         192.168.6.0/24          md5
  </code></pre>
  
  <p>SSL certs exist at the paths specified in pg_hba.conf
  Restarting the postgres service also doesn't fix the problem</p>
  
  <p>Pretty sure my problem is in pg_hba. I'm a networking amateur. On the vm:</p>
  
  <pre><code>$ ip addr show
  1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 16436 qdisc noqueue state UNKNOWN
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
  inet6 ::1/128 scope host
     valid_lft forever preferred_lft forever
  2: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state      UP qlen 1000
  link/ether 08:00:27:9a:8e:c6 brd ff:ff:ff:ff:ff:ff
  inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
  inet6 fe80::a00:27ff:fe9a:8ec6/64 scope link
     valid_lft forever preferred_lft forever
  </code></pre>
  
  <p>Any help would be appreciated!!</p>
  
  <p><strong>Edit: Solved</strong></p>
  
  <p>The problem was not the postgres configuration, but the Virtualbox VM configuration on the remote host. Port forwarding was set up using NAT with the host listening on 127.0.0.1:15432. Because it was only listening on loopback interface, outside world saw port 15432 as closed. Once NAT was changed to 0.0.0.0:15432 on the host, the port was open and connections went through.</p>
  
  <p>Thanks for your suggestions</p>