title: SSH Tunnel for PostgreSQL - Connection Refused
body: <p>I am, for the first time, attempting to set up an application on a remote/cloud VPS (I am using Digital Ocean if it matters). I am attempting to create an SSH tunnel from my client to the remote database. As this is not something I have attempted before, I referenced <a href="https://linuxaria.com/howto/online-budget-with-gnucash-mysql-ssh" rel="nofollow noreferrer">this</a>, <a href="https://www.postgresql.org/docs/9.1/static/ssh-tunnels.html" rel="nofollow noreferrer">this</a>, and <a href="https://support.cloud.engineyard.com/hc/en-us/articles/205408088-Access-Your-Database-Remotely-Through-an-SSH-Tunnel" rel="nofollow noreferrer">this</a>. </p>
  
  <p>After looking over the articles, I ran the following on my client/local machine:</p>
  
  <pre><code> ssh -L 5433:localhost:5432 user@REMOTE_IP
  </code></pre>
  
  <p>then I tried to connect:</p>
  
  <pre><code> psql -h localhost -p 5433 postgres;
  </code></pre>
  
  <p>However, I receive the following error:</p>
  
  <pre><code>psql: could not connect to server: Connection refused
  Is the server running on host "localhost" (127.0.0.1) and accepting
  TCP/IP connections on port 5433?
  </code></pre>
  
  <p>To my knowledge, my pg_hba.conf (on the remote server) is the default:</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
  # IPv6 local connections:
  host    all             all             ::1/128                 md5
  </code></pre>
  
  <p>I changed "listen_addresses" in postgresql.conf to *</p>
  
  <pre><code>    # - Connection Settings -
  
  listen_addresses = '*'          # what IP address(es) to listen on;
                                          # comma-separated list of addresses;
                                          # defaults to 'localhost'; use '*' for all
                                          # (change requires restart)
  port = 5432                             # (change requires restart)
  max_connections = 100                   # (change requires restart)
  </code></pre>
  
  <p>I also tried substituting 127.0.0.1 forlocalhost with no success.</p>
  
  <p>Any advice would be appreciated; SSH tunnels and the like are not something I am familiar with.</p>
  
  <p>Thanks.
  EDIT:</p>
  
  <p>Per @drdaeman excellent advice, I ran the following:</p>
  
  <p>sudo ssh -N -vvv -L 5433:localhost:5432 user@host</p>
  
  <p>The last few debug lines are as follows:</p>
  
  <pre><code>    debug1: Local forwarding listening on 127.0.0.1 port 5433.
  debug2: fd 5 setting O_NONBLOCK
  debug3: fd 5 is O_NONBLOCK
  debug1: channel 1: new [port listener]
  debug2: fd 3 setting TCP_NODELAY
  debug3: ssh_packet_set_tos: set IP_TOS 0x10
  debug1: Requesting no-more-sessions@openssh.com
  debug3: send packet: type 80
  debug1: Entering interactive session.
  debug1: pledge: network
  debug3: receive packet: type 80
  debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
  </code></pre>
  
  <p>Output from <code>sudo netstat -ltpn | grep 5432
  </code></p>
  
  <pre><code>tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      5835/postgres 
  </code></pre>
  
  <p>It stops there, unresponsive to any commands.</p>
  
  <p>Thanks for any direction.</p>