Why is my connection refused when I try to connect to my DB from my website?
I have a website running react hosted on hostgater which contains a PHP that is simply trying to connect to my DB on neon and return a single line of data from the DB but I get this error:
"<b>Warning</b>: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection refused".
When I checked my neon dashboard, I can see the connection attempted to be started but was suspended. This is weird because when I run aamps and use the same PHP script I am able to connect with no problems. Is there a config script somewhere that I am missing?
12 Replies
notable-maroon•2y ago
this usually means invalid password, or username or malformed connection string or mybe you have ip blocking setup ?
conscious-sapphireOP•2y ago
I didn’t think that the connection string was the issue, only because it works when I use aamps/local host to connect. How would I check the ip blocking?
exotic-emerald•2y ago
You'd need to check HostGator's networking documentation. Though it's odd that you see the compute wake up in Neon's console...
notable-maroon•2y ago
exotic-emerald•2y ago
Ah good to share this too. An interesting fact about Neon's IP Allow is that the compute doesn't wake up if the IP is blocked!
conscious-sapphireOP•2y ago
Does that mean that since the compute wakes up, the IP isn't blocked?
notable-maroon•2y ago
waoo thats pretty neat! i didnt knew that
exotic-emerald•2y ago
Not on the Neon side, no. So it might be your hosting provider that's killing the connection or an application error perhaps?
I just checked, and found that Neon does not start the compute if you provide an incorrect password.
conscious-sapphireOP•2y ago
just so I'm clear, it should as simple to connect to my neon DB from a hosted website by using:
$connection = pg_connect("postgresql://[user]:[password]@[URL]/[DBname]?sslmode=require")
or is there a fundamental difference between connecting to the DB from a hosted services vs localhost/aamps that I am not aware of?
exotic-emerald•2y ago
It should be straightforward, but some hosting providers might have networking rules that only allow traffic (ingress and/or egress) on certain ports by default, e.g only 80 and 443 for HTTP/HTTPS, and 22 for SSH.
It looks like HostGator has a list of common ports (https://www.hostgator.com/help/article/commonly-used-port-numbers). I don't know if that means they block all other ports, but you might need to ask them or change settings on your host to open 5432
conscious-sapphireOP•2y ago
ok I will, thank you for your time!
exotic-emerald•2y ago
You're welcome! It seems like HostGator allows you to manage a host with cPanel? (https://www.hostgator.com/help/article/how-to-log-into-cpanel)
Maybe this guide (https://www.interserver.net/tips/kb/open-port-csf-firewall-cpanel/) or a newer one can get you unblocked
Jithin
Interserver Tips
How to Open a Port in CSF Firewall? ( cPanel ) - Interserver Tips
CSF is a common firewall software we use in cPanel servers. If you have CSF firewall installed in your system and want to open ports, then follow this tutorial