我正在尝试通过SSH隧道连接到远程Postgres数据库。我正在使用SSH.NET和Npgsql库。这是我的示例代码:
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();
}
代码执行后,我得到了以下结果:
Npgsql.NpgsqlException:
'从流中读取时出现异常'
EndOfStreamException:试图读取超出流的结尾。
我可以使用DBeaver连接到数据库。