psql: could not connect to server: Connection refused

Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
PostgreSQL connection refused

psql: could not connect to server: Connection refused

Error

psql: could not connect to server: Connection refused

PostgreSQL Connection Refused – Error Explained

One of the most frustrating issues for DBAs and developers is the PostgreSQL connection refused error. The full message usually looks like this:

psql: could not connect to server: Connection refused
Is the server running on host "localhost" and accepting
TCP/IP connections on port 5432?
Common Causes of PostgreSQL Connection Refused Error

The PostgreSQL connection refused problem can occur due to:

  • PostgreSQL service is not running

  • Wrong port number (default is 5432)

  • Firewall blocking the connection

  • TCP/IP connections not enabled in postgresql.conf

  • Host not allowed in pg_hba.conf

Solution:
1. Check if PostgreSQL service is running
sudo systemctl status postgresql
sudo systemctl start postgresql
2. Verify the port (default: 5432)
netstat -nlt | grep 5432

If PostgreSQL is listening on another port, update your connection string.

3. Restart the PostgreSQL server

sudo systemctl restart postgresql

4. Enable TCP/IP connections

Edit postgresql.conf and ensure:

listen_addresses = '*'

5. Configure pg_hba.conf

Add a line like:

host    all   all   0.0.0.0/0   md5

Then reload PostgreSQL:

sudo systemctl reload postgresql

6. Check firewall settings

Allow traffic on port 5432:

sudo ufw allow 5432/tcp

7. Test connection manually

psql -h localhost -U username -d dbname

Conclusion

 At Learnomate Technologies, we’re here to support you every step of the way with top-notch training in PostgreSQL DBA and more.

For more detailed insights and tutorials, do check out our YouTube channelwww.youtube.com/@learnomate, where we regularly share practical tips and deep dives into essential database topics. And if you’re serious about mastering PostgreSQL DBA, head over to our website for our full training program: learnomate.org/training/postgresql-training/.

I’d love to connect with you, so don’t forget to follow my LinkedInhttps://www.linkedin.com/in/ankushthavali/. If you’re eager to read more about various technologies, explore our blog page here: https://learnomate.org/blogs/. Happy learning, and remember—tuning and training go hand-in-hand for database success!