This problem occurs when your server is blocking incoming connections to your pgsql database. Before you try to solve it, find out the version of pgsql you have installed on the server.
Now we will be editing the pg_hba.conf file and allow incoming connections from all IPs (we can allow from only your IP but if you have a dynamic IP, it will cause trouble again)
Edit pg_hba.conf
sudo nano /etc/postgresql/{version}/main/pg_hba.conf
Add the following line to allow connections from any IP address:
host all all 0.0.0.0/0 md5
Edit postgresql.conf
sudo nano /etc/postgresql/{version}/main/postgresql.conf
Find and modify the listen_addresses line to:
listen_addresses = '*'
After making changes to pg_hba.conf and postgresql.conf, restart the PostgreSQL service:
sudo systemctl restart postgresql