'Postgresql: No connection could be made because the target machine actively refused it

Running Postgresql 9.5 on a windows server 2012 R2 in Azure

While running some loadtests on my application, I get errors on not being able to connect to the postgres server. In the logs of postgres I get the following message:

could not receive data from client: No connection could be made because the target machine actively refused it.

This only happens when the loadtest goes to the next scenario, hitting a different part of the code. So new connections to the database are required. But after 10-20 seconds the rest of the scenario works flawlessly without hitting any other hiccups. So the problem seems to be the tcp connections. (My code retries a couple of times but it is not feasible to let it retry for 20 seconds)

I'm using the following settings in the config files

postgresql.conf

listen_addresses = '*'
max_connections = 500   
shared_buffers = 1024MB     
temp_buffers = 2MB
work_mem = 2MB  
maintenance_work_mem = 128MB        

pg_hba.conf

host    all             all             0.0.0.0/0               trust
host    all             all             ::/0                    trust

I know, I know.. It is not save to accept connections from everyone, but this is just for testing purposes and to make sure these settings are not blocking any connection. So this answer is void

I've been monitoring the number of connection on the server and under the load it is stable at 75. Postgres is using around 350mb of RAM. So given the config and the vm specs (7gb ram) there should be plenty of space to create more connections. However when the next scenario is spinning up the number of connections does not increase, it stays level and starts giving these log messages about no connection could be made.

What could be the problem here?



Solution 1:[1]

Most possible reason is a Firewall/Anti-virus:

  • Software/Personal Firewall Settings
  • Multiple Software/Personal Firewalls
  • Anti-virus Software
  • LSP Layer
  • (Virtual) Router Firmware

Does your current Azure infrastructure contain Firewall or Anti-virus ?

Additionally on doing some additional searches, it looks like this is a standard Windows "connection refused" message, which suggests that PostgreSQL is trying to connect to something and being refused.

Also possible that one network element in your network - assuming that you are still connected to the server - will delay or drop somes DB login/authentication network packets (considered for example as a fake auth.replay) ...

You may also use a packet analyzer (like Wireshark) to record/inspect network flow when the error appear.

Regards

Solution 2:[2]

I was facing the same issue in my AspNet core application while I was trying to connect the Postgresql from my application. The error was thrown in the Program.cs file when I was calling the Migrate function.

public static void Main(string[] args) {
    try {
        var host = BuildWebHost(args);
        using(var scope = host.Services.CreateScope()) {
            // Migrate once after app is started.
            scope.ServiceProvider.GetService <MyDatabaseContext>().Migrate();
        }
        host.Run();
    }
    catch(Exception e) {
        //NLog: catch setup errors
        _logger ? .Error(e, "Stopped program because of exception: ");
        throw;
    }
}

To fix this problem I did the following steps.

  1. Check whether the Postgresql service is running by going to the services.msc
  2. Tried to login to the pgAdmin with the user and password I provided in the database context

Everything was file, and as you know that 5432 is the default port of Postgresql and somehow I was using a different port in my application connection string, changing it to 5432 fixed this issue for me.

"ConnectionString": "User Id=postgres;Password=mypwd;Host=localhost;Port=5432;Database=mydb;"

Solution 3:[3]

I came across a similar issue whilst trying to beast my api, where I was seeing Npgsql.NpgsqlException No connection could be made because the target machine actively refused it..

However my issue was was down to the fact that I was re-creating my NpgsqlConnection for each query rather than re-using and keeping it alive.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sibeesh Venu
Solution 3 metoyou