连接到 Redshift 集群时出错 - Npgsql.PostgresException: 28000: 主机没有 pg_hba.conf 条目
Error connecting to Redshift Cluster - Npgsql.PostgresException: 28000: no pg_hba.conf entry for host
我正在尝试从 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 连接到集群。
This 问题几乎是一样的,但不是手动创建连接字符串,而是使用构建器。所以这里是代码:
NpgsqlConnectionStringBuilder builder = new NpgsqlConnectionStringBuilder();
builder.TrustServerCertificate = true;
builder.SslMode = SslMode.Required;
我正在尝试从 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 连接到集群。
This 问题几乎是一样的,但不是手动创建连接字符串,而是使用构建器。所以这里是代码:
NpgsqlConnectionStringBuilder builder = new NpgsqlConnectionStringBuilder();
builder.TrustServerCertificate = true;
builder.SslMode = SslMode.Required;