我将 Rails 5 与 PostgreSQL 一起部署到 Heroku 中的免费应用程序。这是我在database.yml
的配置:
production:
adapter: postgresql
username: root
password:
database: example
当我运行heroku run rake db:migrate
时,我看到此错误:
耙子中止了! PG::连接错误:无法连接到服务器:没有这样的文件或目录 服务器是否在本地运行并接受 Unix 域套接字上的连接 "/var/run/postgresql/.s.PGSQL.5432"?
如果我将此行添加到database.yml
:
host: localhost
并再次运行迁移,我看到此错误:
耙子中止了! PG::连接错误:无法连接到服务器:连接被拒绝 服务器是否在主机"本地主机"(127.0.0.1( 上运行并接受 端口 5432 上的 TCP/IP 连接?
如何解决?
似乎没有为您的应用程序提供数据库,您需要添加一个:
heroku addons:create heroku-postgresql
您可以通过运行以下命令来验证数据库是否已添加到应用程序中:
heroku config --app your_app_name
删除旧数据库后,你应该执行这个
heroku pg:promote HEROKU_POSTGRESQL_NEW_DATABASE
您可以通过以下方式查看您的数据库昵称
heroku pg:info
按顺序执行一些步骤,
-
$ heroku login
-
在 Gemfile 中,将 pg gem 添加到您的 Rails 项目中。改变:
gem sqlite
到gem 'sqlite3', group: :development
gem 'pg', '0.18.1', group: :production
-
在 Gemfile 中,添加 rails_12factor gem:
gem 'rails_12factor', group: :production
-
$ bundle install
-
确保 config/database.yml 使用的是 postgresql 适配器。改变:
production:
<<: *default
database: db/production.sqlite3
production:
<<: *default
adapter: postgresql
database: db/production.sqlite3
-
$ git add .
$ git commit -m "Heroku config"
$ heroku create
$ git push heroku master
$ heroku run rake db:migrate
我希望它奏效了。
因此,请正确执行步骤5。