本地邮政的 Npgsql 连接字符串

本文关键字:Npgsql 连接 字符串 | 更新日期: 2023-09-27 17:56:00

下午好。

我在连接到 Postgres 数据库时遇到问题。

我正在处理的应用必须在 .NET 4 上运行。我使用的是Npgsql,因为我仅限于.NET 4,所以我使用的是Npgsql版本2.2.7(我相信3+需要.NET 4.5)。

该应用程序将与 Postgres 在同一台计算机上运行。数据库由第三方安装和设置,因此我无法更改 pg_hba.conf 文件。

我第一次刺入连接字符串如下所示:

Server=localhost;Database=xyz;Integrated Security=true;

但是我得到了这个错误:

FATAL: 28000: no pg_hba.conf entry for host "::1", user "SYSTEM", database "xyz", SSL off

研究了该错误,并尝试了连接字符串的许多其他变体来解决此问题,包括:

Server=127.0.0.1;Database=xyz;Integrated Security=true;
Server=-h 127.0.0.1;Database=xyz;Integrated Security=true;
Server=127.0.0.1/32;Database=xyz;Integrated Security=true;
Server=::1;Database=xyz;Integrated Security=true;
Server=::1/128;Database=xyz;Integrated Security=true;

但没有任何效果。我要么得到

FATAL: 28000 ...

错误,或简单的

Failed to establish a connection to ...

pg_hba.conf 文件如下所示:

host all postgres 127.0.0.1/32  trust
host all xyz      127.0.0.1/32  trust
host all xyz      ::1/128       trust
host all postgres ::1/128       trust

这一切都在Windows 7机器上运行,并且IPv6在网络连接上关闭。

这可能很简单,但我能做什么?我无法更改 pg_hba.conf 文件,那么如何调整我的连接字符串才能正常工作?

本地邮政的 Npgsql 连接字符串

试试这个:"服务器=[您的服务器];端口=[您的端口];D atabase=[您的数据库];用户 ID =[您的用户];密码=[您的密码];"

例如:"服务器=本地主机;端口=5432;数据库=书店;用户 ID = 运算符;密码=1234;"

pg_hba.conf文件添加的开头(条目的顺序很重要!

  # Type    database      users      auth.method
  local     xyz           all        sspi

如果您不希望用户使用任何密码进行连接。(欲了解更多信息,请查看:https://www.postgresql.org/docs/9.1/static/auth-methods.html#SSPI-AUTHhttps://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html (auth-method -> sspi)

Secound选项是添加到您的ConnectionString中,正如@Cecilia Fernández所说。但是,您需要添加到pg_hba.conf文件中:

  # Type    database      users      auth.method
  local     xyz           all        md5