[英]Error connecting to Redshift Cluster - Npgsql.PostgresException: 28000: no pg_hba.conf entry for host
I am trying to connect to AWS Redshift from C# and perform a simple query:我正在尝试从 C# 连接到 AWS Redshift 并执行一个简单的查询:
public void GetData()
{
NpgsqlConnectionStringBuilder builder = new NpgsqlConnectionStringBuilder();
builder.Host = "myhostname";
builder.Port = 5439;
builder.Database = "mydb";
builder.Username = "myusername";
builder.Password = "mypassword";
builder.ServerCompatibilityMode = ServerCompatibilityMode.Redshift;
using (var conn = new NpgsqlConnection(builder.ConnectionString))
{
conn.Open();
if (conn.State == ConnectionState.Open)
{
using (var cmd = new NpgsqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "SELECT TOP 10 field FROM example;";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader.GetString(0));
}
}
}
}
}
}
When trying to make the connection I get: Npgsql.PostgresException: 28000: no pg_hba.conf entry for host
.尝试建立连接时,我得到: Npgsql.PostgresException: 28000: no pg_hba.conf entry for host
。 From the same machine I can connect to the cluster using JetBrains DataGrip using the same credentials.在同一台机器上,我可以使用 JetBrains DataGrip 使用相同的凭据连接到集群。
This question is almost the same, but instead of manually creating a connection string, your using the builder.这个问题几乎相同,但不是手动创建连接字符串,而是使用构建器。 So here is the code for that:所以这里是代码:
NpgsqlConnectionStringBuilder builder = new NpgsqlConnectionStringBuilder();
builder.TrustServerCertificate = true;
builder.SslMode = SslMode.Required;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.