Rails: PG::ConnectionBad: 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?
Rails can't reach PostgreSQL — the server is down, wrong host/port/socket, or credentials/pg_hba block it.
PG::ConnectionBad: could not connect to server: Connection refusedPG::ConnectionBad: connection to server at "127.0.0.1", port 5432 failed: Connection refusedPG::ConnectionBad: FATAL: role "app" does not existPG::ConnectionBad: FATAL: database "app_development" does not existcould not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket Meaning
Start PostgreSQL (brew services start postgresql, sudo systemctl start postgresql), check database.yml/DATABASE_URL, the socket path (/var/run/postgresql), and Docker networking (host db not localhost). For role errors use createuser; for "no pg_hba.conf entry" fix pg_hba.conf.
Common causes
- PostgreSQL not running
- Wrong host/port/socket
- Docker: using localhost instead of the service name
- Role/database missing
pg_hba.confrejecting
⚡ Quick fix
- Start PostgreSQL and test
psql -h HOST -U USER DB - Fix
host:in database.yml (service name in Docker) - Create role/db:
createuser -s $USER; createdb app_dev
Detailed fix by platform
Linux
pg_isready -h localhost -p 5432; sudo systemctl status postgresql
How to diagnose
- Server — pg_isready?
- Host — Correct?
- Role — Exists?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- ActionMailer: SMTP From address may not be blank / Net::SMTPAuthenticationError Rails: ArgumentError: SMTP From address may not be blank: nil / Net::SMTPAuthenticationError: 535 5.7.8 Username and Password not accepted
- ActiveRecord::AssociationTypeMismatch Rails: ActiveRecord::AssociationTypeMismatch: User(#1234) expected, got "42" which is an instance of String(#5678)
- ActiveRecord::ConnectionNotEstablished Rails: ActiveRecord::ConnectionNotEstablished: No connection pool for 'ActiveRecord::Base' found
Most viewed in Ruby / Rails
- ActiveRecord::PendingMigrationError Rails: ActiveRecord::PendingMigrationError — Migrations are pending. To resolve this issue, run: bin/rails db:migrate
- undefined method for nil Ruby: NoMethodError: undefined method `x' for nil (nil:NilClass)
- ActionController::InvalidAuthenticityToken Rails: ActionController::InvalidAuthenticityToken (Can't verify CSRF token authenticity)
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 5 Nov 2026