PostgreSQL 13 - OpenAsync causes NpgsqlException

0

Recently I stumbled upon problem when using Npgsql (db driver for C#) with EF Core. I am using PostgreSQL 13 database setup on CentOS 8. I can login to database without any problems using DataGrip, but I have some problems when using Npgsql (NpgsqlConnection). I can use Open() method without any problems. I can send queries and get the results back. However OpenAsync() always results in NpgsqlException. I checked and tried to connect to docker image with Postgres and did not have any problem with that. It seems that CentOS 8 configuration maybe lack something. Thank you for help.

Example of client code:

        private static async Task Main(string[] args)
        {
            CancellationToken cancellationToken = CancellationToken.None;
            using (NpgsqlConnection connection = new NpgsqlConnection("Server=xxxx;Port=5432;Database=xxxx;User Id=postgres;Password=xxxx"))
            {
                //connection.Open();
                await connection.OpenAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
                using (NpgsqlCommand command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT * From Role";
                    NpgsqlDataReader res = command.ExecuteReader();
                    while (res.HasRows)
                    {
                        //...
                    }
                }
            }
        }

Additional debug info:

Unhandled exception. Npgsql.NpgsqlException (0x80004005): Exception while connecting
 ---> System.TimeoutException: The operation has timed out.
   at Npgsql.TaskExtensions.WithTimeout(Task task, NpgsqlTimeout timeout)
   at Npgsql.NpgsqlConnector.ConnectAsync(NpgsqlTimeout timeout, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnector.ConnectAsync(NpgsqlTimeout timeout, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.ConnectorPool.AllocateLong(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at ConsoleTests.Program.Main(String[] args) in C:\Users\Piotr\Desktop\Codesk\codesk\ConsoleTests\Program.cs:line 16
   at ConsoleTests.Program.<Main>(String[] args)
centos
npgsql
asked on Stack Overflow Oct 23, 2020 by pg89 • edited Oct 23, 2020 by pg89

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0