简体   繁体   中英

Ruby on Rails and Postgresql - Server is running, but I'm obtaining a could not connect to server error

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM