# PostgreSQL Connection Refused: Server Not Accepting Connections

You're trying to connect to your PostgreSQL database, but instead of a prompt, you get this frustrating error:

bash
psql: could not connect to server: Connection refused
	Is the server running on host "localhost" (127.0.0.1) and accepting
	TCP/IP connections on port 5432?

This error means the PostgreSQL server either isn't running, isn't listening on the expected address, or a firewall is blocking the connection. Let's diagnose and fix this systematically.

Introduction

This article covers troubleshooting steps and solutions for PostgreSQL Connection Refused: Server Not Accepting Connections (database variant 2). The error typically occurs in production environments and can cause service disruptions if not addressed promptly.

Symptoms

Common error messages include:

bash
psql: could not connect to server: Connection refused
	Is the server running on host "localhost" (127.0.0.1) and accepting
	TCP/IP connections on port 5432?
bash
sudo systemctl status postgresql
bash
sudo service postgresql status

Common Causes

  • Configuration misconfiguration
  • Missing or incorrect credentials
  • Network connectivity issues
  • Version compatibility problems
  • Resource exhaustion or limits
  • Permission or access denied

Step-by-Step Fix

  1. 1.Check logs for specific error messages
  2. 2.Verify configuration settings
  3. 3.Test network connectivity
  4. 4.Review recent changes
  5. 5.Apply corrective action
  6. 6.Verify the fix

Diagnosing the Problem

Step 1: Check if PostgreSQL is Running

First, verify the PostgreSQL service status:

On Linux (systemd): ``bash sudo systemctl status postgresql

On Linux (older systems): ``bash sudo service postgresql status

On macOS (Homebrew): ``bash brew services list | grep postgresql

On Windows: ``cmd sc query postgresql-x64-16

If the service isn't running, start it:

bash
sudo systemctl start postgresql

Step 2: Check the Listening Address and Port

PostgreSQL might be running but not listening on the address you're connecting to. Check the actual listening ports:

```bash # Linux/macOS sudo ss -tlnp | grep postgres # or sudo netstat -tlnp | grep 5432

# Check PostgreSQL's view sudo -u postgres psql -c "SHOW port;" sudo -u postgres psql -c "SHOW listen_addresses;" ```

The listen_addresses setting controls which IP addresses PostgreSQL accepts connections on:

  • 'localhost' - Only local connections via 127.0.0.1
  • '*' - All interfaces (requires listen_addresses = '*')
  • Specific IP - Only that IP address

Step 3: Verify the Port in Configuration

Check the PostgreSQL configuration files:

```bash # Find the config file location sudo -u postgres psql -c "SHOW config_file;"

# View relevant settings sudo -u postgres psql -c "SHOW port;" ```

Common config file locations: - /etc/postgresql/16/main/postgresql.conf (Debian/Ubuntu) - /var/lib/pgsql/data/postgresql.conf (RHEL/CentOS) - /usr/local/var/postgres/postgresql.conf (macOS Homebrew)

Common Causes and Solutions

Cause 1: Server Not Running

Symptoms: No PostgreSQL process found, systemctl shows inactive/dead.

Solution: ``bash sudo systemctl start postgresql sudo systemctl enable postgresql # Auto-start on boot

Cause 2: Wrong Port Number

Symptoms: PostgreSQL running on non-default port (5433 instead of 5432).

Solution: Either connect with the correct port or change the configuration:

```bash # Connect with correct port psql -h localhost -p 5433 -U postgres

# Or change port in postgresql.conf port = 5432 ```

After changing configuration, restart:

bash
sudo systemctl restart postgresql

Cause 3: Not Listening on Expected Address

Symptoms: listen_addresses is set to 'localhost' but you're connecting from another host.

Solution: Edit postgresql.conf:

conf
listen_addresses = '*'  # Listen on all interfaces
# or
listen_addresses = '192.168.1.100'  # Specific IP

Then restart and update pg_hba.conf to allow remote connections:

conf
# pg_hba.conf
host    all    all    192.168.1.0/24    scram-sha-256

Cause 4: Firewall Blocking Connection

Symptoms: PostgreSQL running and listening, but connections time out or get refused.

Solution: Check firewall rules:

Using firewalld (RHEL/CentOS): ``bash sudo firewall-cmd --list-all sudo firewall-cmd --add-service=postgresql --permanent sudo firewall-cmd --reload

Using ufw (Ubuntu): ``bash sudo ufw status sudo ufw allow 5432/tcp

Using iptables: ``bash sudo iptables -L -n | grep 5432 sudo iptables -A INPUT -p tcp --dport 5432 -j ACCEPT

Cause 5: Unix Socket vs TCP Connection

Symptoms: Connection works with psql -U postgres but fails with psql -h localhost -U postgres.

Diagnosis: ``bash # Check unix socket directory sudo -u postgres psql -c "SHOW unix_socket_directories;"

Solution: If using TCP explicitly, ensure listen_addresses includes localhost:

conf
listen_addresses = 'localhost'

Verification

After making changes, verify everything works:

```bash # Test local connection via Unix socket sudo -u postgres psql -c "SELECT version();"

# Test TCP connection psql -h localhost -p 5432 -U postgres -c "SELECT version();"

# Test from remote host psql -h 192.168.1.100 -p 5432 -U postgres -c "SELECT version();" ```

Check the PostgreSQL logs for any remaining issues:

```bash # Find log location sudo -u postgres psql -c "SHOW log_directory;"

# Common log locations tail -f /var/log/postgresql/postgresql-16-main.log tail -f /var/lib/pgsql/data/log/postgresql-*.log ```

Verification

CheckCommand
Service statussudo systemctl status postgresql
Listening ports`sudo ss -tlnp \grep postgres`
PostgreSQL portpsql -c "SHOW port;"
Listen addressespsql -c "SHOW listen_addresses;"
Config filepsql -c "SHOW config_file;"
Active connectionspsql -c "SELECT * FROM pg_stat_activity;"
  • "No such file or directory" - Unix socket file missing; check unix_socket_directories
  • "Connection timed out" - Firewall blocking; check iptables/firewalld
  • "FATAL: no pg_hba.conf entry" - Authentication config; see our pg_hba.conf guide
  • [Database troubleshooting: Fix Backup Exclusive Lock Table Production Writes ](backup-exclusive-lock-table-production-writes)
  • [Fix Connection Pool Leak Application Not Closing Issue in Database](connection-pool-leak-application-not-closing)
  • [Fix Connection Reset Idle Timeout Firewall Issue in Database](connection-reset-idle-timeout-firewall)
  • [Fix Connection Reset Idle Timeout Serverless Database Issue in Database](connection-reset-idle-timeout-serverless-database)
  • [Fix Connection String Encoding Special Characters Issue in Database](connection-string-encoding-special-characters)

<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "PostgreSQL Connection Refused: Server Not Accepting Connections (database variant 2)", "description": "Learn how to diagnose and fix PostgreSQL connection refused errors. Step-by-step guide to resolve server not accepting connections issues.", "url": "https://www.fixwikihub.com/fix-postgresql-connection-refused", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2025-11-15T14:23:00.269Z", "dateModified": "2025-11-15T14:23:00.269Z" } </script>