连接到 Redshift 集群时出错 - Npgsql.PostgresException: 28000: no pg_hba.conf entry for host
Posted
技术标签:
【中文标题】连接到 Redshift 集群时出错 - Npgsql.PostgresException: 28000: no pg_hba.conf entry for host【英文标题】:Error connecting to Redshift Cluster - Npgsql.PostgresException: 28000: no pg_hba.conf entry for host 【发布时间】:2020-05-18 11:12:04 【问题描述】:我正在尝试从 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));
尝试建立连接时,我得到:Npgsql.PostgresException: 28000: no pg_hba.conf entry for host
。在同一台机器上,我可以使用 JetBrains DataGrip 使用相同的凭据连接到集群。
【问题讨论】:
尝试将 SslMode 设置为 required,并将 TrustServerCertificate 设置为 true,如 here 所述 【参考方案1】:This 问题几乎相同,但不是手动创建连接字符串,而是使用构建器。所以这里是代码:
NpgsqlConnectionStringBuilder builder = new NpgsqlConnectionStringBuilder();
builder.TrustServerCertificate = true;
builder.SslMode = SslMode.Required;
【讨论】:
以上是关于连接到 Redshift 集群时出错 - Npgsql.PostgresException: 28000: no pg_hba.conf entry for host的主要内容,如果未能解决你的问题,请参考以下文章
如何从我的 Amazon EC2 实例中连接到 Amazon Redshift 集群