【问题标题】:Error when run migrations on Heroku, PostgreSQL with Rails 5使用 Rails 5 在 Heroku、PostgreSQL 上运行迁移时出错
【发布时间】:2023-04-10 17:27:01
【问题描述】:

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

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

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

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

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

host: localhost

然后再次运行迁移,我看到了这个错误:

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

如何解决?

【问题讨论】:

  • 你能打印heroku config --app your_app_name命令的输出吗?
  • LANG: en_US.UTF-8 RACK_ENV: 生产 RAILS_ENV: 生产 RAILS_LOG_TO_STDOUT: 启用 RAILS_SERVE_STATIC_FILES: 启用 SECRET_KEY_BASE: yyyyyyyyyyyyyyyyyyyy KEY HERE

标签: ruby-on-rails ruby postgresql ruby-on-rails-4 heroku


【解决方案1】:

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

heroku addons:create heroku-postgresql

您可以通过运行来验证数据库是否已添加到您的应用程序中:

heroku config --app your_app_name

【讨论】:

    【解决方案2】:

    按顺序执行一些步骤,

    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 步。

    【讨论】:

    【解决方案3】:

    删除旧数据库后,您应该执行此操作

    heroku pg:promote HEROKU_POSTGRESQL_NEW_DATABASE
    

    您可以通过

    查看您的数据库昵称
    heroku pg:info
    

    【讨论】:

    • 连接问题时为什么要OP删除一个数据库?
    猜你喜欢
    • 1970-01-01
    • 2017-10-29
    • 2016-10-03
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    相关资源
    最近更新 更多