Connection timeout error with Postgres

5,614 views
Skip to first unread message

Disha Patel

unread,
Apr 21, 2022, 3:47:43 PM4/21/22
to Django users
Hi, 
I am facing below connection timeout error while using Postgres as database for my Django application. 

"psycopg2.operationalerror: could not connect to server: connection timed out:

Solutions tried:
1. increased timeout at server end. 
2. added conn_max_age in the settings and tried multiple values ( eg. None, 60,120)

It is a random error but occurs very frequently.
Kindly share if anyone aware about the reason for this issue and any solutions for the same.


Antonis Christofides

unread,
Apr 22, 2022, 2:49:27 AM4/22/22
to django...@googlegroups.com

Hi!

I shall assume that PostgreSQL is listening on port 5432. "Connection timed out" means that the client (Django/psycopg2 in this case) is not getting any response—as if the PostgreSQL server machine did not exist. ("Connection refused", on the other hand, means that the PostgreSQL server machine replied that connection on port 5432 is not possible, because there is no process listening on that port.)

If Django/psycopg2 have the correct domain name or IP address for the PostgreSQL server, and the correct port, the most common explanation for "Connection timed out" is that a firewall (typically on the PostgreSQL server) is filtering that traffic. Is your PostgreSQL server on a different machine than Django?

Regards,

Antonis

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b0c59297-5125-46f5-843f-55c0c00eb4acn%40googlegroups.com.

Disha Patel

unread,
Apr 24, 2022, 2:15:34 PM4/24/22
to Django users
Hi Antonis, 

Yes, the PostgreSQL server is on a difference machine than Django. I have noticed that initial connections are successful but it fails in the later connections mainly in long running jobs. 

Thanks,
Disha

Antonis Christofides

unread,
Apr 24, 2022, 2:29:09 PM4/24/22
to django...@googlegroups.com

Since PostgreSQL is listening on a different machine, then it is more or less clear from the error message that the traffic is being filtered. It's strange it works sometimes and other times it isn't working. Does it have a static IP address? Is the PostgreSQL server machine running something like fail2ban?

What I'd do if I had such a problem would be to investigate the PostgreSQL server machine at the time when the problem is occurring (e.g. check /var/log/syslog and `iptables -L`).

Reply all
Reply to author
Forward
0 new messages