PG::ConnectionBad: could not connect to server Ruby / Rails Origin: Application & API Last updated 5 Nov 2026

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.

Exact message you may see 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
Seen on: Ruby PostgreSQL

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.conf rejecting

⚡ Quick fix

  1. Start PostgreSQL and test psql -h HOST -U USER DB
  2. Fix host: in database.yml (service name in Docker)
  3. Create role/db: createuser -s $USER; createdb app_dev

Detailed fix by platform

Linux

  1. pg_isready -h localhost -p 5432; sudo systemctl status postgresql

How to diagnose

  1. Server — pg_isready?
  2. Host — Correct?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

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 (Ruby / Rails)ActionMailer: SMTP Fr…ActiveRecord::AssociationTypeMismatch — Rails: ActiveRecord::AssociationTypeMismatch: User(#1234) expected, got "42" which is an instance of String(#5678) (Ruby / Rails)ActiveRecord::Associa…ActiveRecord::ConnectionNotEstablished — Rails: ActiveRecord::ConnectionNotEstablished: No connection pool for 'ActiveRecord::Base' found (Ruby / Rails)ActiveRecord::Connect…ActiveRecord::Deadlocked: PG::TRDeadlockDetected — Rails: ActiveRecord::Deadlocked: PG::TRDeadlockDetected: ERROR: deadlock detected (Ruby / Rails)ActiveRecord::Deadloc…ActiveRecord::IrreversibleMigration — Rails: ActiveRecord::IrreversibleMigration — This migration uses remove_column without a type; db:rollback can't reverse it (Ruby / Rails)ActiveRecord::Irrever…ActiveRecord::ReadOnlyRecord — Rails: ActiveRecord::ReadOnlyRecord: Order is marked as readonly (Ruby / Rails)ActiveRecord::ReadOnl…ActiveRecord::RecordInvalid: Validation failed — Rails: ActiveRecord::RecordInvalid: Validation failed: Email can't be blank, Name is too short (Ruby / Rails)ActiveRecord::RecordI…ActiveRecord::RecordNotFound: Couldn't find X with 'id'= — Rails: ActiveRecord::RecordNotFound: Couldn't find User with 'id'=42 (Ruby / Rails)ActiveRecord::RecordN…PG::Conne…

Explore the map of this error Full error map →

🧠 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.