I am trying to connect to the Postgres database on remote server through SSH tunnel. I am using SSH.NET and Npgsql library. Here is my example code:
using (var client = new SshClient("210.130.90.110", "root", "pasword"))
{
client.Connect();
if (!client.IsConnected)
{
// Display error
Console.WriteLine("Client not connected!");
}
else
{
Console.WriteLine("Client connected!");
}
var port = new ForwardedPortLocal("127.0.0.1", 15432, "210.130.90.110", 5432);
client.AddForwardedPort(port);
port.Start();
using (var conn = new NpgsqlConnection("Server=127.0.0.1;Database=dbname;Port=15432;User Id=dbuser;Password=dbpassword;"))
{
conn.Open();
}
port.Stop();
client.Disconnect();
}
After code execution I get:
Npgsql.NpgsqlException: 'Exception while reading from stream' EndOfStreamException: Attempted to read past the end of the stream.
I am able to connect to database using DBeaver.