1

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.

AltBrian
  • 2,392
  • 9
  • 29
  • 58

1 Answers1

1

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

xploshioOn
  • 4,035
  • 2
  • 28
  • 37
  • Do I add it to the config file like this ```development: <<: *default adapter: postgresql encoding: unicode pool: 5 username: USERNAME password: PASSWORD timeout: 5000 host: "localhost" database: aquastarsbackend_development``` – AltBrian Dec 04 '18 at 21:02
  • I am getting the following error ```rails aborted! PG::ConnectionBad: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? 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?``` – AltBrian Dec 04 '18 at 21:03
  • look at the default block to see what is there, and remove from development the lines that are in default if they are correct, because you are already adding them with `<<: *default` and remember to change USERNAME and PASSWORD with the ones you created for postgres – xploshioOn Dec 04 '18 at 21:04
  • I have not created one in Postgres. How do I create one in Postgres? – AltBrian Dec 04 '18 at 21:15
  • 1
    I think there is something wrong with my postgres as when I tried to create a simple rails app I get the same sort of error. – AltBrian Dec 04 '18 at 21:44
  • I edited to add instructions to create a new user on postgres – xploshioOn Dec 04 '18 at 22:49
  • I am missing something as this is not doesn't seem straight forward. – AltBrian Dec 04 '18 at 22:50