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
版权声明:本文标题:postgresql - No such host is known when trying to connect to Postgres from C# code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744005000a2574537.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论