使用Rails 5在Heroku,PostgreSQL上运行迁移时出错

我使用PostgreSQL将Rails 5部署到Heroku中的免费应用程序。 这是我在database.yml配置:

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

当我运行heroku run rake db:migrate ,我看到这个错误:

耙子流产了! PG :: ConnectionBad:无法连接到服务器:没有这样的文件或目录服务器是否在本地运行并接受Unix域套接字“/var/run/postgresql/.s.PGSQL.5432”上的连接?

如果我将此行添加到database.yml

 host: localhost 

并再次运行迁移,我看到这个错误:

耙子流产了! PG :: ConnectionBad:无法连接到服务器:连接被拒绝服务器是否在主机“localhost”(127.0.0.1)上运行并接受端口5432上的TCP / IP连接?

怎么解决?

似乎没有为您的应用提供数据库,您需要添加一个:

 heroku addons:create heroku-postgresql 

您可以通过运行以下命令validation数据库已添加到您的应用程序

 heroku config --app your_app_name 

按顺序执行一些步骤,

  1. $ heroku login

  2. 在Gemfile中,将pg gem添加到Rails项目中。 更改:

    gem sqlite

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

  3. 在Gemfile中,添加rails_12factor gem ::

    gem 'rails_12factor', group: :production

  4. $ bundle install

  5. 确保config / database.yml正在使用postgresql适配器。 更改:

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

    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
    我希望它有效。

所以正确地遵循第5步。