最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

postgresql - No such host is known when trying to connect to Postgres from C# code - Stack Overflow

programmeradmin0浏览0评论

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;

发布评论

评论列表(0)

  1. 暂无评论