System.Net.Sockets.SocketException on postgres connection

3

I have this app where I connect to a Postgres DB. I have my Connection function, but every time I try to connect to the DB i get the exception:

{System.Net.Sockets.SocketException (0x80004005): Connection refused at Npgsql.NpgsqlConnector.Connect (Npgsql.NpgsqlTimeout timeout) [0x001f5] in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:733 at Npgsql.NpgsqlConnector+d__153.MoveNext () [0x005a0] in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:648 --- End of stack trace from previous location where exception was thrown --- at Npgsql.NpgsqlConnector+d__149.MoveNext () [0x003e1] in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:455 --- End of stack trace from previous location where exception was thrown --- at Npgsql.ConnectorPool+d__19.MoveNext () [0x001cf] in C:\projects\npgsql\src\Npgsql\ConnectorPool.cs:300 --- End of stack trace from previous location where exception was thrown --- at System.Threading.Tasks.ValueTask1[TResult].get_Result () [0x00022] in <1d288dd8ebaf4c6f890e1e99a5a184f0>:0 at System.Runtime.CompilerServices.ValueTaskAwaiter1[TResult].GetResult () [0x00000] in <1d288dd8ebaf4c6f890e1e99a5a184f0>:0 at Npgsql.NpgsqlConnection+<>c__DisplayClass32_0+<g__OpenLong|0>d.MoveNext () [0x0046d] in C:\projects\npgsql\src\Npgsql\NpgsqlConnection.cs:331 --- End of stack trace from previous location where exception was thrown --- at Npgsql.NpgsqlConnection.Open () [0x00000] in C:\projects\npgsql\src\Npgsql\NpgsqlConnection.cs:153 at App1.MainPage.conexao () [0x00054] in C:\Users\Mario\Documents\projetos\App1\App1\App1\MainPage.xaml.cs:40 }

here is the code I'm trying to use:

   private void conexao()
    {
        NpgsqlConnection pgsqlConnection = null;
        var connString = string.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};",
                                             serverName, port, userName, password, databaseName);
        DataTable dt = new DataTable();

        try
        {
            using (pgsqlConnection = new NpgsqlConnection(connString))
            {
                usuario usuario = new usuario();
                // abre a conexão com o PgSQL e define a instrução SQL
                pgsqlConnection.Open();
                string cmdSeleciona = "Select * from funcionarios order by id_funcionario";
                List<string> lista = new List<string>();
                NpgsqlCommand cmd = new NpgsqlCommand(cmdSeleciona, pgsqlConnection);
                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    usuario.nome_funcionario = reader["nome_funcionario"].ToString();
                    usuario.email = reader["email"].ToString();
                }
                //using (NpgsqlDataAdapter Adpt = new NpgsqlDataAdapter(cmdSeleciona, pgsqlConnection))
                //{
                //}
            }
        }
        catch (NpgsqlException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            pgsqlConnection.Close();
        }
    }

The exception occurs on:pgsqlConnection.Open();

c#
postgresql
xamarin.forms
socketexception
asked on Stack Overflow Dec 2, 2018 by Mario • edited Dec 2, 2018 by Jazb

1 Answer

0

This question is open for over a year so I dare to give a less concrete answer about things you could check...

Check the following things:

  1. Win+R services.msc or go into taskmanager and check if the postgresql service is started
  2. Run PgAdmin which opens in the browser. If PgAdmin can connect and you can browse your db you can rule out a lot.
  3. Check your connection string and make sure the user has all roles (if you just want to test just give him all permissions via pgadmin)

If you are using a dockerized environment you need to set the containers into the same network and use host.docker.internal instead of localhost as host.

Here an example of my working connection string of my linux docker container environment:

host=host.docker.internal;port=5432;database=mydatabase;username=userxyz;password=password123
answered on Stack Overflow May 11, 2020 by CodingYourLife

User contributions licensed under CC BY-SA 3.0