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;

4
  • 1
    Are you sure postgresql service is running and that you can connect with this connection string info using say pgAdmin4? Commented Mar 30 at 17:19
  • 2
    How about Console.writeline(connectionString)? Could you try Host=127.0.0.1? Commented Mar 31 at 7:08
  • In my project I have used Host=localhost:5432; in connection string and it worked correctly. Commented Mar 31 at 11:26
  • I did try all of them but to no avail. Somehow the problem got away when i tried it on a simple console app after restarting my PC. Commented Mar 31 at 17:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.