Unable to connect to PostgreSQL server via SSH: No connection could be made because the target machine actively refused it

c# entity-framework-core portforwarding postgresql ssh.net


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.

When I open the SSH connection with PuTTY, the program runs fine. This is the PuTTY command:

//plink.exe -i "C:\Users\user.name\Desktop\host_private_key.ppk" -L 6544: -N user@address.io -pw *PASSWORD*"

(login details removed for privacy)

This is the ssh.net code that I have trying to open the same connection:

public void MakeSSHTunnel()
            string password = "password";
            // path of RSA private key in openSSH format:
            string privateKeyPath = "C:/Users/user.name/.ssh/id_rsa";

                // 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))
                    var tunnel = new ForwardedPortLocal(6544, "", 6543);

                    // testing weather the connection has been successful:
                    if (client.IsConnected)
                        Console.WriteLine("OPENTUNNEL.CS: Connection to {0} successful.", serverAddress);
                        state = "Open";
                        Console.WriteLine("Connection to {0} failed.");
                        state = "Closed";

                    tunnel.Exception += delegate (object sender, ExceptionEventArgs e)

                    // ... closes the port ... //
            catch (UnauthorizedAccessException e)

I am confused since in the above, the if (client.IsConnected) returns true.

The error seems to be occurring when the Entity Framework Core OnConfiguring() method passes details of the connection with its optionsBuilder:

optionsBuilder.UseNpgsql($"Host=;Port=6544;Database=user;Username=user;Password=databasePassworh;CommandTimeout=300;Timeout=300;SSL Mode=Require;Trust Server Certificate=true;Convert Infinity DateTime=true");

The errors that are arising are:

NpgsqlException: Exception while connecting


ExtendedSocketException: No connection could be made because the target machine actively refused it.

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.

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.

1/21/2020 1:40:23 AM

Popular Answer

This code is now working. I believe the problem was that in line:

var tunnel = new ForwardedPortLocal(6544, "", 6543);

the 'bound port' did not include an address. I had seen examples where this was undefined, and had followed these. On stepping through the code, I noticed that the field was blank, and decided to try This is now successfully connecting to the database. It works with:

var tunnel = new ForwardedPortLocal("", 6544, "", 6543); 

Thanks for looking into this and for your contributions.

1/23/2020 9:52:31 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow