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:
```c#
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 at line `conn.Open()`, 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.
Exception message:
Npgsql.NpgsqlException (0x80004005): Exception while reading from stream ---> System.IO.EndOfStreamException: Attempted to read past the end of the stream.
at Npgsql.NpgsqlReadBuffer.<>c__DisplayClass34_0.<
at Npgsql.NpgsqlReadBuffer.<>c__DisplayClass34_0.<
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Npgsql.NpgsqlConnector.<>c__DisplayClass159_0.<
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Npgsql.NpgsqlConnector.
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Npgsql.NpgsqlConnector.
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Npgsql.ConnectorPool.
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.ValueTask`1.get_Result()
at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Npgsql.NpgsqlConnection.Open()
```
Npgsql version: 4.1
PostgreSQL version: PostgreSQL 10.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
Operating system: Windows 10
@peropata Can you connect to the database if the tunnel is setup outside of your program? Something along the lines of:
ssh -L 15432:210.130.90.110:5432
psql -h localhost -p 15432
c#
using var conn = new NpgsqlConnection("Server=localhost;Port=15432");
conn.Open();
@austindrenski You are correct. If I use tunnel outside of my program, the connection to database is successful. So there is no problem with npgsql. I will close the issue and try to resolve the ssh part.
@peropata Great, thanks for giving that a try and letting us know.
This is a rather interesting scenario for Npgsql, so please feel free to post back here if you're able to get the integrated version working.
All I had to do is change the line;
var port = new ForwardedPortLocal("127.0.0.1", 15432, "210.130.90.110", 5432);
to:
var port = new ForwardedPortLocal("127.0.0.1", 15432, "127.0.0.1", 5432);
and is working now.
Most helpful comment
All I had to do is change the line;
var port = new ForwardedPortLocal("127.0.0.1", 15432, "210.130.90.110", 5432);
to:
var port = new ForwardedPortLocal("127.0.0.1", 15432, "127.0.0.1", 5432);
and is working now.