Software⏱️ 4 min read📅 2026-06-04

How to Fix: "psql: could not connect to server: Connection refused" Error when connecting to remote database

Postgres connection refused error on remote server.

Quick Answer: Check the Postgres service status and ensure it is listening on port 5432. If not, you may need to configure the service or check firewall rules.

The 'psql: could not connect to server: Connection refused' error occurs when you are trying to establish a connection to a remote Postgres database, but the server is not accepting TCP/IP connections on port 5432. This issue affects users who attempt to connect to the remote database using the psql command-line tool.

This error can be frustrating because it prevents you from accessing your data and performing necessary operations on the remote database. In this troubleshooting guide, we will walk you through the possible causes of this error and provide steps to resolve the issue.

💡 Why You Are Getting This Error

  • The primary reason for this error is that the Postgres server may not be running or listening on port 5432. This can occur due to various reasons such as network configuration issues, firewall rules, or incorrect service configurations.
  • Another possible cause is that the PostgreSQL server is configured to listen on a different port than 5432. In this case, you would need to update your connection string to reflect the correct port number.

🚀 How to Resolve This Issue

Verify Server Configuration and Network Connectivity

  1. Step 1: Step 1: Check if the Postgres server is running on the remote host by using the command `sudo service postgresql status` (for Ubuntu-based systems) or `sudo systemctl status postgresql.service` (for CentOS-based systems). If the server is not running, start it using the corresponding command.
  2. Step 2: Step 2: Verify that the PostgreSQL server is listening on port 5432 by using the command `netstat -tlnp | grep 5432` (for Ubuntu-based systems) or `ss -tuln | grep 5432` (for CentOS-based systems). If the server is not listening on this port, update your connection string to use a different port number.
  3. Step 3: Step 3: Check the firewall rules on the remote host to ensure that incoming TCP connections on port 5432 are allowed. You can use the `ufw` command (for Ubuntu-based systems) or `firewall-cmd` command (for CentOS-based systems) to inspect and modify the firewall rules.

Verify Client Configuration and Network Connectivity

  1. Step 1: Step 1: Check if your client operating system has the necessary configuration to connect to the remote Postgres server. Ensure that the correct port number is specified in your connection string.
  2. Step 2: Step 2: Verify that your network connectivity allows you to establish a connection between your client and the remote host. Use tools like `ping` or ` traceroute` to check your network connectivity.

🎯 Final Words

To resolve the 'psql: could not connect to server: Connection refused' error, first verify that the Postgres server is running and listening on port 5432, and then check your client configuration and network connectivity. By following these steps, you should be able to establish a successful connection to your remote database.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions