Software⏱️ 3 min read📅 2026-06-03

How to Fix: PG::ConnectionBad - could not connect to server: Connection refused

Check the PostgreSQL server status and ensure it is running on localhost and accepting TCP/IP connections on port 5432.

Quick Answer: Verify that your PostgreSQL server is running and listening for incoming connections. You can do this by checking the process list or using the `pg_stat_host` view to confirm the server is active.

The 'PG::ConnectionBad' error occurs when your Rails application is unable to connect to your PostgreSQL database server. This issue affects developers who use PostgreSQL as their database management system, particularly those working on Rails applications.

This error can be frustrating for developers because it prevents them from accessing and manipulating data in the database, which is a crucial aspect of many web applications. Fortunately, there are steps you can take to resolve this issue.

🔍 Why This Happens

  • The primary reason for this error is that the PostgreSQL server is not running or not accepting TCP/IP connections on port 5432. This could be due to various reasons such as the server being down, misconfigured, or not listening on the specified port.
  • Another possible cause is that the Rails application is trying to connect to a different host or port than the one configured in the database.yml file.

🔧 Proven Troubleshooting Steps

Starting PostgreSQL Server

  1. Step 1: Step 1: Open a terminal and navigate to the directory where your PostgreSQL server is installed. Typically, this is located at /usr/local/bin on macOS or C:/Program Files/PostgreSQL on Windows.
  2. Step 2: Step 2: Run the command `pg_ctl start` (on Linux/Mac) or `net start postgresql` (on Windows) to start the PostgreSQL server.
  3. Step 3: Step 3: Verify that the server is running by checking the output of `ps aux | grep postgres` (on Linux/Mac) or `tasklist | findstr postgres` (on Windows).

Configuring Rails Application

  1. Step 1: Step 1: Check your database.yml file to ensure that the host, username, and password are correct. Make sure the PostgreSQL server is running on localhost (::1) and accepting TCP/IP connections on port 5432.
  2. Step 2: Step 2: If you're using a different host or port, update the configuration in your database.yml file accordingly. For example, if your PostgreSQL server is listening on port 5433, update the `host` field to `localhost:5433`.

✨ Wrapping Up

To resolve the 'PG::ConnectionBad' error, try starting the PostgreSQL server and verifying its status. If the issue persists, check your database.yml file for any configuration errors or discrepancies between the server's host and port settings.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions