本地Postgres的Npgsql连接字符串

时间:2016-07-08 18:38:58

标签: c# .net postgresql npgsql

下午好。

我很难连接到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文件,那么如何调整连接字符串才能工作?

2 个答案:

答案 0 :(得分:2)

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

例如: “Server = localhost; Port = 5432; Database = BookStore; User ID = operator; Password = 1234;”

答案 1 :(得分:1)

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

  # Type    database      users      auth.method
  local     xyz           all        sspi

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

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

  # Type    database      users      auth.method
  local     xyz           all        md5