0

I am getting the following exception in C# Visual Studio 2022/.NET6 when I try to connect PostgreSQL running as a Docker container:

How I am installing PostgreSQL as Docker container

Step 1 - Install Postgres

docker pull postgres
docker run --name gb-postgres -e POSTGRES_PASSWORD=xxxxxx -d postgres

Step 2 - Once Postgres is installed you need to find out your instance IP address

Run
docker ps

copy the {id  of the container} of the container and run

docker inspect {id  of the container}

copy the IPAddress of the container

Step 3 - Configure API

Use IP address in your Program.cs

Exception

Npgsql.NpgsqlException (0x80004005): Failed to connect to 172.17.0.3:5432
 ---> System.TimeoutException: Timeout during connection attempt
   at Npgsql.Internal.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
   at Npgsql.Internal.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
   at Npgsql.Internal.NpgsqlConnector.RawOpen(SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)
   at Npgsql.Internal.NpgsqlConnector.<Open>g__OpenCore|203_1(NpgsqlConnector conn, SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)
   at Npgsql.Internal.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.PoolingDataSource.OpenNewConnector(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.PoolingDataSource.<Get>g__RentAsync|28_0(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnection.<Open>g__OpenAsync|45_0(Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnection.Open()
   at Program.Main(String[] args) in D:\data\code\Dust-IO\388013gb3\cloud\API\GB3API\ConsoleApp1\Program.cs:line 14

C# Program

using System;
using Npgsql;

class Program
{
    static void Main(string[] args)
    {
        string connectionString = "Host=172.17.0.3;Database=GB3;Username=postgres;Password=xxxxx";

        using (var connection = new NpgsqlConnection(connectionString))
        {
            try
            {
                connection.Open();
                Console.WriteLine("Connected to PostgreSQL database.");

                // Perform database operations here

                connection.Close();
                Console.WriteLine("Connection closed.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        Console.ReadLine();
    }
}

Even when I try to connect using Visual Studio 2022 extension Npgsql PostgreSQL Integration, I get an error Exception while connecting. And when I try to connect using VS Code extension PostgreSQL by Chris Kolkman, I get an error to connect ETIMEDOUT 172.17.0.2:5432.

docker inspect

            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "d9371cf11864c75a9c35d03ce24bf90bb78af8f44b43e6336b0630a6d79d9503",
                    "EndpointID": "b235ae6eb713404f9fbbcd35453acb6e9dcb213ad6d51fa8809272912a569e15",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
Syed Rafey Husain
  • 153
  • 1
  • 1
  • 7
  • 1
    Du you have any port mapping / do you expose the ports from within the container to outside of it? I.e.: `(...) 5432:5432 postgres` see here: https://stackoverflow.com/a/37704532/2590375 – nilsK Jul 05 '23 at 08:18
  • Where are you connecting from? The IP address you retrieved would not be expected to work from places other than the docker host machine. – jjanes Jul 05 '23 at 16:04
  • @jjanes, I follow this process to get PostgreSQL IP: 1) docker ps 2) copy the {id of the container} of the container and run 3) docker inspect {id of the container} 4) copy the IPAddress of the container... If you see in the question docker inspect return `"IPAddress": "172.17.0.3"` – Syed Rafey Husain Jul 06 '23 at 12:06
  • Yes, I understand that. But where are you trying to connect from? Is your C# program running on another computer, or on the docker host, or in another container? – jjanes Jul 06 '23 at 19:55
  • @jjanes, I am running C# program, Postgres Docker everything locally on my laptop. – Syed Rafey Husain Jul 07 '23 at 07:33

0 Answers0