【发布时间】:2019-02-11 12:27:17
【问题描述】:
我无法使用某些连接字符串从 C# .NET 代码连接到远程 PostgreSQL 服务器。 (我的连接字符串包含主机地址、端口、用户名、密码、数据库名称等基本信息)。
我可以使用 SSH 隧道从 PGAdmin 4 连接到 PostgreSQL 服务器, 我有远程登录(用户名和密码)、端口 (22) 和 RSA 私钥。
我的连接字符串:
Server=HOST_ADDRESS;Port=HOST_PORT;User Id=POSTGRES_USERNAME;Password=POSTGRES_PASSWORD;Database=POSTGRES_DATABASE_NAME;
注意:我正在使用Npgsql .NET 程序集
我得到的错误是:
No connection could be made because the target machine actively refused it
Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
at Npgsql.NpgsqlConnector.<RawOpen>d__153.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Npgsql.NpgsqlConnector.<Open>d__149.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Npgsql.ConnectorPool.<AllocateLong>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Npgsql.NpgsqlConnection.Open()
【问题讨论】:
-
查看连接字符串。 connectionstrings.com/postgresql
-
@jdweng 我的连接字符串看起来很好,就像您在链接中给出的那样,请注意,我可以连接到本地的 PostgreSQL 服务器,但是当我尝试连接远程服务器时出现错误Linux 机器。
-
我假设 PGAdmin 4 应用程序与您的应用程序在同一台机器上失败并连接到同一个远程服务器。然后我会使用像wireshark或fiddler这样的嗅探器,并将工作的PGAdmin 4与你失败的c#应用程序进行比较。通常这是由于 http 标头不同,您必须使您的应用程序看起来像工作中的 PGAdmin 4。我还假设您查看了 sslmode=require 选项的连接字符串。当您使用嗅探器时,使用 PGAdmin 4 查看用户的连接字符串。
标签: c# postgresql ssh connection remote-connection