简体   繁体   中英

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:

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 . From the same machine I can connect to the cluster using JetBrains DataGrip using the same credentials.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM