AltBrian
AltBrian

Reputation: 2562

Getting bad connection error when trying to migrate in rails.

I want to create a rails api and wanting to migrate the database. I have started postgresql with the following command brew services restart postgresql. The console gave me the following response ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql). The problem is when I run rails db:migrate I get the following error

rails db:migraterails aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

Any help would be appreciated. I did try the following link PG::ConnectionBad - could not connect to server: Connection refused

This did not seem to work.

Upvotes: 1

Views: 751

Answers (1)

xploshioOn
xploshioOn

Reputation: 4115

you need to setup the credentials of your database in config/database.yml file. you will have config for test, production and development.

change the development block to have username and password for your local DB

development: 
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: USERNAME
  password: PASSWORD
  timeout: 5000
  host: "localhost"
  database: NAME

change the ones that are on uppercase, in database, use the name you want or if you are trying to use an existing one, add that name there. and of course add the username and password that you already created on postgres

if you don't have an user, you can create one like this

sudo -u postgres createuser -s dev
sudo -u postgres psql
\password dev #here you can type the password you want for this user
\q

in this example, we are creating a user named 'dev' so you add dev to the username part and change the password for the one you typed

Upvotes: 1

Related Questions