I'm trying to connect to my postgres db located on a digitalocean's droplet using psycopg2 package.
However, I keep getting this error message :
psycopg2.OperationalError: connection to server at "***.***.***.***", port 5432 failed: Connection timed out (0x0000274C/10060)
Is the server running on that host and accepting TCP/IP connections?
I've made all the necessary modifications to both postgresql.conf and pg_hba.conf files, and restarted postgresql service.
#postgresql.conf
listen_addresses = '*' #both were uncommented
port = 5432
#pg_hba.conf
host all all 0.0.0.0/0 md5
host all all ::/0 md5
hostssl all all 0.0.0.0/0 md5 #tried with and without this line
When I run service postgresql status I see that my service is active.
And when I run netstat -na I get the following :
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.54:53 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN
tcp6 0 0 :::80 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
It looks like port 5432 is not open. I also tried using sudo ufw allow 5432/tcp but it didn't change anything.
Do you have any idea what I'm doing wrong ?
EDIT (to answer @Adrian Klaver's question) :
- I run
netstat -naboth in root and postgres. sudo ufw statusreturn the following :
To Action From
-- ------ ----
Apache ALLOW Anywhere
OpenSSH ALLOW Anywhere
5432/tcp ALLOW Anywhere
Apache (v6) ALLOW Anywhere (v6)
OpenSSH (v6) ALLOW Anywhere (v6)
5432/tcp (v6) ALLOW Anywhere (v6)
ps ax | grep postgresreturns the following :
9320 pts/0 S+ 0:00 sudo -u postgres psql
9321 pts/1 Ss 0:00 sudo -u postgres psql
9322 pts/1 S+ 0:00 /usr/lib/postgresql/15/bin/psql
9721 pts/2 S+ 0:00 sudo -i -u postgres
9722 pts/3 Ss 0:00 sudo -i -u postgres
10141 pts/5 S+ 0:00 sudo -i -u postgres
10142 pts/6 Ss 0:00 sudo -i -u postgres
10234 pts/7 S+ 0:00 grep --color=auto postgres
- there's no external firewall