How to fix could not connect to server: Connection refused in PostgreSQL

PostgreSQLINTERMEDIATEHIGHUpdated Dec 25, 2025· 121 views

PostgreSQL connection refused error occurs when a client cannot establish a connection to the database server. This typically happens when the server is not running, not listening on the expected port, or has firewall/network restrictions.

Quick fix

Fixes could not connect to server: Connection refused

sudo systemctl status postgresql
  1. Check if PostgreSQL server is running
  2. Verify the correct host and port in your connection string
  3. Check PostgreSQL listen configuration
  4. Configure pg_hba.conf for client authentication
postgresql — error
sudo systemctl status postgresql
could not connect to server: Connection refused

What this error means

This error indicates that your PostgreSQL client cannot establish a TCP/IP or Unix socket connection to the PostgreSQL server. The "Connection refused" message specifically means the server is not listening on the port/socket you're trying to connect to. This is different from authentication failures—the server isn't even accepting the connection attempt.

Typical symptoms

  • psql command fails immediately with 'Connection refused'
  • Database connection poolers or ORMs timeout trying to connect
  • Applications cannot reach the database after a restart or migration
  • Local connections work but remote connections fail
  • Connection works from one machine but not another

Common causes

  1. PostgreSQL server daemon is not running or has crashed
  2. Server is bound to localhost only and you're trying to connect remotely
  3. Port number mismatch (default is 5432) or port already in use by another process
  4. Firewall rules block connections to the PostgreSQL port
  5. Network connectivity issues between client and server
  6. PostgreSQL service corrupted or in a bad state
  7. Socket path mismatch for Unix domain socket connections

How to fix "could not connect to server: Connection refused"

1Check if PostgreSQL server is running

On Linux/macOS, verify the PostgreSQL service status:

bash
sudo systemctl status postgresql

On macOS with Homebrew:

bash
brew services list

If the service is inactive or not running, start it:

bash
sudo systemctl start postgresql

Or with Homebrew:

bash
brew services start postgresql@15

Verify the process is listening on port 5432:

bash
sudo lsof -i :5432
# or
sudo netstat -ltnp | grep postgres
2Verify the correct host and port in your connection string

Check your connection string parameters. The default PostgreSQL port is 5432.

If connecting locally, try these approaches:

bash
# Using socket (fastest for local connections)
psql -U postgres

# Explicitly using localhost
psql -h localhost -U postgres

# Using IP address
psql -h 127.0.0.1 -U postgres

# Specifying port
psql -h localhost -p 5432 -U postgres

If your connection string has hostname/port mismatches, update your application config.

3Check PostgreSQL listen configuration

PostgreSQL must be configured to listen on the address you're connecting to. Edit the PostgreSQL configuration file (typically at /etc/postgresql/15/main/postgresql.conf on Linux):

bash
sudo nano /etc/postgresql/15/main/postgresql.conf

Find the listen_addresses parameter:

bash
# For local connections only:
listen_addresses = 'localhost'

# For all interfaces (less secure, use with firewall):
listen_addresses = '*'

# For specific IP:
listen_addresses = '192.168.1.5'

After editing, reload the configuration:

bash
sudo systemctl reload postgresql

Verify it's listening on the correct address:

bash
sudo netstat -ltnp | grep postgres
4Configure pg_hba.conf for client authentication

The pg_hba.conf file controls which hosts can connect. It's usually at /etc/postgresql/15/main/pg_hba.conf.

Open it:

bash
sudo nano /etc/postgresql/15/main/pg_hba.conf

Add rules for your connection. For example:

bash
# For local socket connections (most common)
local   all             all                                     trust

# For localhost connections
host    all             all             127.0.0.1/32            md5

# For remote connections from specific IP
host    all             all             192.168.1.0/24          md5

# For all remote connections (less secure)
host    all             all             0.0.0.0/0               md5

After changes, reload PostgreSQL:

bash
sudo systemctl reload postgresql
5Check firewall rules

If connecting remotely, verify your firewall allows port 5432.

On Linux with UFW:

bash
sudo ufw status
sudo ufw allow 5432/tcp

On Linux with iptables:

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

If using AWS/cloud providers, check security groups and network ACLs to ensure port 5432 is open.

Test connectivity from client:

bash
telnet hostname 5432
nc -zv hostname 5432
6Check for port conflicts

Verify port 5432 isn't already in use by another PostgreSQL instance or process:

bash
sudo lsof -i :5432
sudo netstat -ltnp | grep 5432

If another PostgreSQL instance is running, decide which one to keep and stop the other:

bash
sudo systemctl stop postgresql
# or for specific version
sudo systemctl stop postgresql@15

If a different service is using port 5432, either change that service's port or change PostgreSQL's port in postgresql.conf (look for the port parameter).

Advanced notes

Unix Socket vs TCP/IP: PostgreSQL supports both Unix domain sockets (faster, local-only) and TCP/IP connections. If you're on the same machine, using the socket (psql -U postgres without -h) is preferred. Remote connections always use TCP/IP and require proper listen_addresses configuration.

Connection Pooling: If using a connection pool (PgBouncer, pgpool2), the error may originate from the pool itself. Verify the pool's backend settings and that it can reach the PostgreSQL server.

Docker/Kubernetes: In containerized environments, the PostgreSQL service DNS name or IP must be correct. Use the service name for Kubernetes DNS (postgres.default.svc.cluster.local) or the container network IP for Docker.

Database Cluster Issues: In rare cases where none of the above works, the PostgreSQL data directory may be corrupted. Try reinitializing the cluster (this destroys existing data):

bash
sudo rm -rf /var/lib/postgresql/15/main
sudo -u postgres /usr/lib/postgresql/15/bin/initdb -D /var/lib/postgresql/15/main
sudo systemctl start postgresql

Frequently asked questions

What does "could not connect to server: Connection refused" mean?
This error indicates that your PostgreSQL client cannot establish a TCP/IP or Unix socket connection to the PostgreSQL server. The "Connection refused" message specifically means the server is not listening on the port/socket you're trying to connect to. This is different from authentication failures—the server isn't even accepting the connection attempt.
How do I fix "could not connect to server: Connection refused"?
PostgreSQL connection refused error occurs when a client cannot establish a connection to the database server. This typically happens when the server is not running, not listening on the expected port, or has firewall/network restrictions.
What causes "could not connect to server: Connection refused"?
PostgreSQL server daemon is not running or has crashed Server is bound to localhost only and you're trying to connect remotely Port number mismatch (default is 5432) or port already in use by another process
Is the "could not connect to server: Connection refused" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading