Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 08006 Connection Failure Fixes

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

PostgreSQL error 08006 is a connection failure that occurs when the client application cannot establish a communication link with the database server. This is most commonly caused by the database service being stopped, a firewall blocking the port, or incorrect network configuration in the server settings. To fix it, first verify that the PostgreSQL service is running and that the server is configured to listen for connections on the correct IP address.

What You'll Need

  • Administrative or root access to the server hosting the PostgreSQL database.
  • A terminal or command-line interface (CLI) for the server's operating system.
  • A text editor (such as Nano, Vim, or Notepad) to modify configuration files.
  • Knowledge of the port number the database is using (default is 5432).

Step-by-Step

  1. Verify the Database Service Status

    The most frequent cause of error 08006 is that the PostgreSQL server has crashed or was never started. Check the status based on your operating system:

    • Linux (systemd): Run systemctl status postgresql. If it is inactive, run sudo systemctl start postgresql.
    • Windows: Open the Services manager (search for "Services" in the Start menu), locate the postgresql-x64 service, and ensure the status is "Running." If not, right-click it and select Start.
    • macOS (Homebrew): Run brew services list. If postgresql is stopped, run brew services start postgresql.
  2. Check the Listen Address

    By default, PostgreSQL often only listens on the local loopback interface (localhost). If your application is on a different machine, the server will reject the connection. Locate the postgresql.conf file (usually found in the data directory). Look for the listen_addresses line. Change it from listen_addresses = 'localhost' to listen_addresses = '*' to allow connections from any IP address. Save the file and restart the PostgreSQL service for changes to take effect.

  3. Configure Client Authentication (pg_hba.conf)

    Even if the server is listening, it will block connections that aren't explicitly permitted in the Host-Based Authentication (HBA) file. Open pg_hba.conf in your data directory. Add a line to allow your specific client IP or network range. For example, to allow all connections from a specific subnet, add: host all all 192.168.1.0/24 md5. Replace the IP range with your actual network settings. Save the file and reload or restart the service.

  4. Open the Firewall Port

    If the service is running and configured correctly but the error persists, a firewall is likely blocking the traffic. Ensure that port 5432 (or your custom port) is open for inbound traffic:

    • Linux (ufw): Run sudo ufw allow 5432/tcp.
    • Linux (firewalld): Run sudo firewall-cmd --add-port=5432/tcp --permanent followed by sudo firewall-cmd --reload.
    • Windows: Open Windows Defender Firewall with Advanced Security and create a new "Inbound Rule" for TCP port 5432.
  5. Test Connectivity with pg_isready

    Use the built-in PostgreSQL utility to check if the server is accepting connections without needing to log in. Run pg_isready -h [server_ip] -p 5432. If the output says "accepting connections," the 08006 error is resolved. If it says "no response," the issue is still at the network or service level.

Common Mistakes

  • Forgetting to Restart: Modifying postgresql.conf or pg_hba.conf does nothing until the service is restarted or the configuration is reloaded.
  • Typing Errors in IP Ranges: Using an incorrect CIDR notation (e.g., 192.168.1.0 instead of 192.168.1.0/24) in the pg_hba.conf file will cause authentication failures.
  • Confusing Port Numbers: Assuming the database is on 5432 when a custom port was set during installation. Always verify the port in postgresql.conf.
  • Overlooking Cloud Security Groups: If the database is hosted on AWS, Azure, or GCP, opening the OS firewall is not enough; you must also open the port in the cloud provider's security group or virtual firewall settings.

Tips

  • Use a Connection String: When testing, use a full connection URI (e.g., postgresql://user:password@host:port/dbname) to ensure no parameters are being omitted by the client.
  • Check Server Logs: If the service fails to start, check the PostgreSQL log files. They will tell you exactly which line in the configuration file has a syntax error.
  • Least Privilege: In pg_hba.conf, avoid using 0.0.0.0/0 (allow all) in production environments. Instead, list only the specific IP addresses of your application servers.

Troubleshooting

If the steps above do not resolve the 08006 error, try these deeper diagnostics:

  • Ping the Server: Run ping [server_ip]. If the ping fails, you have a general network connectivity issue, not a PostgreSQL-specific problem.
  • Telnet/NC Test: Run nc -zv [server_ip] 5432 or telnet [server_ip] 5432. If the connection is refused, the port is still blocked or the service is not listening on that interface.
  • Check for Resource Exhaustion: Check if the server is out of RAM or disk space using df -h and free -m. PostgreSQL may shut down automatically if the disk is 100% full.
  • Review Zombie Processes: Sometimes a crashed instance leaves a "postmaster.pid" file in the data directory, preventing a new instance from starting. If the service refuses to start and logs mention a pid file, ensure no postgres processes are running and then remove the postmaster.pid file manually.

Frequently Asked Questions

Is error 08006 the same as "Password Authentication Failed"?

No. Password failure is an authentication error (usually code 28P01). Error 08006 is a connection failure, meaning the client couldn't even reach the point where it could send a password.

Why does it work on localhost but not from another server?

This is usually because listen_addresses is set to 'localhost' or the pg_hba.conf file does not have an entry allowing the remote server's IP address.

Can a VPN cause this error?

Yes. If your database is on a private network, you must be connected to the correct VPN to reach the server's internal IP. If the VPN drops, you will see a connection failure.

Does restarting the server fix 08006?

It can, if the database service had crashed or hung. However, if the issue is a firewall or configuration setting, a restart will not fix it.

Which log file should I check for connection errors?

Check the postgresql.log file, typically located in the pg_log subdirectory within your database data folder.