admin管理员组

文章数量:1355660

I am trying to connect to Postgres 17 in my local machine yet i'm getting an error of "No such host is known".

I established a connection to the instance through DBeaver yet doing so in code always results in the same error. At first i thought i uninstalled postgres to reinstall it but it didn't work.
On the other hand, localhost can be pinged and psql can connect to the database itself. What am i doing wrong?

Could it be related to the fact that i changed the way of retrieving the connection string once from .json file to environment variables?

Below my appsettings.json and program.cs files.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "ConnectionStrings": {
    "DefaultConnection": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=root;"
  }

}
using Npgsql;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

string connectionString = builder.Configuration.GetConnectionString("DefaultConnection")!;

using (var conn = new NpgsqlConnection(connectionString))
{
    try
    {
        conn.Open();
        Console.WriteLine("Connection successful!");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: " + ex.Message);
    }
}
return;

本文标签: postgresqlNo such host is known when trying to connect to Postgres from C codeStack Overflow