Getting a PostgreSQL connection refused error right when your backend app tries to query your database halts your local or staging environment immediately. Your terminal throws a fatal message saying connection to server at localhost port 5432 failed because the database daemon is either stopped or rejecting your network socket.
Backend engineers hit this lockout across Linux macOS Windows and Docker setups whenever default localhost bindings block external container traffic. Below is how to match your exact error symptom and execute the first two fixes right now.
7 Ways to Fix PostgreSQL Connection Refused Errors
1. Verify That the PostgreSQL Service Is Actually Running
A system reboot or crashed background worker often leaves the database daemon completely offline. Check your service status first before editing configuration files so you do not waste time debugging an inactive process:
sudo systemctl status postgresql && sudo systemctl start postgresql
brew services start postgresql@16
On Windows open Services press P to locate postgresql-x64 and click Start if the status column is blank. Review the official PostgreSQL server startup documentation to inspect log file paths if the daemon fails to boot.
2. Replace Localhost With host.docker.internal in Containers
Calling localhost from inside an application container points to that container itself rather than your host machine which triggers a PostgreSQL connection refused failure. Update your connection string to use host.docker.internal when your app runs in Docker and Postgres runs natively on your laptop. Container network isolation frequently blocks database and web ports when bridges are misconfigured so checking how Docker Compose port bindings route host traffic helps keep multi-container setups aligned.
3. Allow Network Interfaces in postgresql.conf
By default PostgreSQL only binds to the loopback address 127.0.0.1 so any connection coming from another container or remote server gets dropped immediately. Open your postgresql.conf file and update listen_addresses so the server accepts connections on your local network or all interfaces:
listen_addresses = ‘*’ # or specific subnet IP like ‘127.0.0.1,172.17.0.1’
port = 5432
Save the configuration file and restart the PostgreSQL daemon so the updated socket binding takes effect. Network schema and connection misconfigurations also break cloud-synced backends when containers fail to authenticate cleanly so reviewing how database integration and schema sync rules behave helps prevent repeat connection drops.
4. Add Your Client Subnet to pg_hba.conf
Opening listen_addresses alone still triggers a PostgreSQL connection refused or authentication rejection if your client IP range is missing from pg_hba.conf. Add a host rule granting your Docker bridge subnet or office IP access via scram-sha-256 password authentication:
host all all 172.17.0.0/16 scram-sha-256
Reload PostgreSQL right after saving so the new access policy loads into memory without dropping active sessions.

5. Open Port 5432 in UFW or Cloud Security Groups
Cloud servers on AWS EC2 DigitalOcean and Ubuntu VMs block inbound TCP traffic on port 5432 by default. Run sudo ufw allow 5432/tcp on your Linux host and verify your cloud provider security group permits inbound connections from your application server IP. Strict firewall rules and timeout ceilings also impact hosted serverless workers when external database handshakes take too long so checking how database query bottlenecks stall backend functions helps keep cloud pipelines responsive.
6. Check Non-Default Ports and Stale postmaster.pid Locks
Installing multiple PostgreSQL versions on macOS or Ubuntu often assigns port 5433 instead of 5432 to the newer instance. Run grep -E “^port” /etc/postgresql/*/main/postgresql.conf to confirm the exact listening port and update your application DATABASE_URL if they do not match.
If Postgres crashed after a sudden power loss a leftover postmaster.pid file inside your data directory can block restarts and cause a PostgreSQL connection refused error. Verify zero active postgres processes remain via ps aux | grep postgres before removing that stale lock file and starting the service cleanly.
7. Test Direct Connectivity Using pg_isready
After updating your network bindings verify that PostgreSQL accepts TCP handshakes from your application host using the built-in pg_isready utility:
Seeing accepting connections confirms your socket and firewall rules are healthy. Review the official PostgreSQL pg_hba.conf authentication documentation if you need fine-grained SSL or subnet rules. Teams managing multi-service repositories can also review how full-stack deployment environment variables stay synchronized across staging and production builds.
Conclusion
Fixing a PostgreSQL connection refused error comes down to confirming the daemon is running switching Docker containers to host.docker.internal and opening listen_addresses alongside pg_hba.conf. Testing your endpoint with pg_isready verifies that port 5432 accepts connections reliably before you deploy.
Frequently Asked Questions
Why does PostgreSQL say connection refused on localhost port 5432?
This message means no active PostgreSQL daemon is listening on port 5432 or the service crashed after a reboot. Starting the system service resolves local socket refusals immediately.
How do I fix PostgreSQL connection refused inside a Docker container?
Replace localhost with host.docker.internal in your app connection string and set listen_addresses = ‘*’ in postgresql.conf so the host accepts container traffic.
What is the difference between listen_addresses and pg_hba.conf?
The listen_addresses setting controls which network interfaces PostgreSQL listens on while pg_hba.conf controls which client IP addresses and users are permitted to authenticate.
How do I check if PostgreSQL is listening on port 5432?
Run pg_isready -h 127.0.0.1 -p 5432 in your terminal. If the database is ready to accept TCP connections it outputs accepting connections.

