title: Unable to connect to PostgreSQL server via SSH: No connection could be made because the target machine actively refused it
body: <p>I am trying to automatically create an SSH connection for a program that uses an SSH tunnel to update a local database from a remote PostgreSQL server. Up to this time, I have been manually opening a tunnel with PuTTY (including local port forwarding instructions with the -L command). I want to use ssh.net to automatically open the port when the program is run. Once the connection is made, the program uses Entity Framework Core to access the remote database.</p>
  
  <p>When I open the SSH connection with PuTTY, the program runs fine. This is the PuTTY command:</p>
  
  <pre><code>//plink.exe -i "C:\Users\user.name\Desktop\host_private_key.ppk" -L 6544:111.22.33.66:6543 -N user@address.io -pw *PASSWORD*"
  </code></pre>
  
  <p>(login details removed for privacy)</p>
  
  <p>This is the ssh.net code that I have trying to open the same connection:</p>
  
  <pre><code>public void MakeSSHTunnel()
          {
              string password = "password";
              // path of RSA private key in openSSH format:
              string privateKeyPath = "C:/Users/user.name/.ssh/id_rsa";
  
              try
              {
                  // creates variable to transmit RSA private key + passphrase to server via SSH.NET, openSSH compatible.
                  var privateKeyFile = new PrivateKeyFile(privateKeyPath, password);
                  string serverAddress = "address.io";
                  string user = "user";
  
                  // allows for the remote port forwarding options required by the server 
                  using (var client = new SshClient(serverAddress, user, privateKeyFile))
                  {
                      client.Connect();
                      var tunnel = new ForwardedPortLocal(6544, "111.22.33.66", 6543);
                      client.AddForwardedPort(tunnel);
  
                      // testing weather the connection has been successful:
                      if (client.IsConnected)
                      {
                          Console.WriteLine("OPENTUNNEL.CS: Connection to {0} successful.", serverAddress);
                          state = "Open";
                      }
                      else
                      {
                          Console.WriteLine("Connection to {0} failed.");
                          state = "Closed";
                      }
  
                      tunnel.Exception += delegate (object sender, ExceptionEventArgs e)
                      {
                          Console.WriteLine(e.Exception.ToString());
                      };
                      tunnel.Start();
                      Program.RunBackup();
  
  
                      // ... closes the port ... //
                      tunnel.Stop();
                      client.Disconnect();
                  }
              }
              catch (UnauthorizedAccessException e)
              {
                  Console.WriteLine(e);
              }
          }
  </code></pre>
  
  <p>I am confused since in the above, the <code>if (client.IsConnected)</code> returns true.</p>
  
  <p>The error seems to be occurring when the Entity Framework Core OnConfiguring() method passes details of the connection with its optionsBuilder:</p>
  
  <pre><code>optionsBuilder.UseNpgsql($"Host=127.0.0.1;Port=6544;Database=user;Username=user;Password=databasePassworh;CommandTimeout=300;Timeout=300;SSL Mode=Require;Trust Server Certificate=true;Convert Infinity DateTime=true");
  </code></pre>
  
  <p>The errors that are arising are:</p>
  
  <pre><code>NpgsqlException: Exception while connecting
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>ExtendedSocketException: No connection could be made because the target machine actively refused it. 127.0.0.1:6544
  </code></pre>
  
  <p>I have double checked all passwords, and read through all the SSH.NET documentation and code examples, and left all the previously working (via PuTTY) code untouched. </p>
  
  <p>If anyone can see what I'm doing wrong, I would be grateful. C#, SSH.NET and port forwarding are new to me, please tell me where I'm being an idiot.</p>