2

I deploy Rails 5 with PostgreSQL to free app in Heroku. This is my configuration from database.yml:

production:
  adapter: postgresql
  username: root
  password:
  database: example

When I run heroku run rake db:migrate, I see this error:

rake 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 "/var/run/postgresql/.s.PGSQL.5432"?

If I add this line to database.yml:

host: localhost

and run migration again, I see this error:

rake aborted! 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?

How to fix it ?

2
  • Can you print the output of heroku config --app your_app_name command? Commented Jul 26, 2017 at 20:58
  • LANG: en_US.UTF-8 RACK_ENV: production RAILS_ENV: production RAILS_LOG_TO_STDOUT: enabled RAILS_SERVE_STATIC_FILES: enabled SECRET_KEY_BASE: yyyyyyyyyyyyyyyyyyyyyyy KEY HERE Commented Jul 26, 2017 at 21:01

3 Answers 3

21

It seems like database is not provided for your app, you need to add one:

heroku addons:create heroku-postgresql

You can verify the database was added to your application by running:

heroku config --app your_app_name
Sign up to request clarification or add additional context in comments.

Comments

1

After delete old DB, you should execute this

heroku pg:promote HEROKU_POSTGRESQL_NEW_DATABASE

You can check your DB nickname by

heroku pg:info

1 Comment

Why should OP delete a database when the problem is to connect?
0

Follow some steps in serial order,

  1. $ heroku login

  2. In Gemfile, add the pg gem to your Rails project. Change:

    gem sqlite

    to

    gem 'sqlite3', group: :development
    gem 'pg', '0.18.1', group: :production

  3. In Gemfile, add the rails_12factor gem::

    gem 'rails_12factor', group: :production

  4. $ bundle install

  5. Ensure config/database.yml is using the postgresql adapter. Change:

    production:
    <<: *default
    database: db/production.sqlite3
    to

    production:
    <<: *default
    adapter: postgresql
    database: db/production.sqlite3

  6. $ git add .
    $ git commit -m "Heroku config"
  7. $ heroku create
  8. $ git push heroku master
  9. $ heroku run rake db:migrate
    I hope it worked.

SO FOLLOW STEP 5 correctly.

1 Comment

rails_12factor is no needed for Rails 5. Check documentation: github.com/heroku/rails_12factor

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.