0

QUESTION: PostgreSQL server is running, but when I run rake db:migrate, I obtain a ConnectionBad: could not connect to server error. What can I do to fix this error?

Update - Database.yml file

I've removed some points in the file for anonymity.

default: &default
  adapter: postgresql
  pool: 5
  timeout: 5000

development:
  adapter: postgresql
  encoding: unicode
  database: db_development
  pool: 5
  username: 
  password: 

test:
  adapter: postgresql
  encoding: unicode
  database: db_test
  pool: 5
  username: 
  password:

production:
  adapter: postgresql
  encoding: unicode
  database: db_production
  pool: 5
  username: 
  password:

I've looked through the API and tried to implement the answers, but nothing is working on my end.

When I run pg_lsclusters, I'm told:

Ver Cluster Port Status Owner    Data directory              Log file
10  main    5433 online postgres /var/lib/postgresql/10/main 
/var/log/postgresql/postgresql-10-main.log

So it's online, I usually start the server as sudo service postgresql start.

However, when I try and run rake db:migrate, I get the error:

PG::ConnectionBad: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

I haven't encountered this error before, is it possible I have uninstalled postgresql wrongly?

I did it as: sudo apt-get update sudo apt-get install postgresql postgresql-contrib libpq-dev gem install pg

And in the Ruby on Rails app:

bundle install bundle update

Everything installed perfectly, so I just don't know what I have done wrong, given I've done those steps before on another computer and it works fine for it.

4
  • Please enclose the contents of your database.yml file Commented Mar 15, 2019 at 2:51
  • Hi hd1, I've just added the file in now, with some points removed for anonymity. Commented Mar 15, 2019 at 2:56
  • Have you tried connecting with the command line? Commented Mar 15, 2019 at 3:04
  • How would I do that? Commented Mar 15, 2019 at 3:21

1 Answer 1

1

You need to add port: 5433 to your database.yml.

The default PostgreSQL port is 5432, but your pg_lsclusters command says the server is running on 5433. This is why Rails can’t find a server running on localhost:5432.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.