gpt4 book ai didi

ruby-on-rails - Heroku - 从 Sqlite 迁移到 Postgres 时遇到问题

转载 作者:行者123 更新时间:2023-11-29 13:30:45 24 4
gpt4 key购买 nike

我用他们内置的数据库 SQLITE 3 构建了一个 Rails 应用程序。我已经尝试了一段时间将应用程序部署到 Heroku。我知道 Heroku 的首选数据库是 Postgres,所以我经历了将应用程序的数据库从 SQLITE 更改为 postgres 的麻烦。

这是我的 Gemfile,它指定我现在安装了“pg”而不是 sqlite。

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
ruby '2.1.0'
gem 'rails', '4.1.1'

group :development, :test do
gem 'pg', '0.17.1'
gem 'rspec-rails', '3.0.1'
end

group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end

gem 'sass-rails', '~> 4.0.2'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'sprockets-rails', '~> 2.1.3'
gem 'bootstrap-sass', '3.1.1.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.1.1'

group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end

# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

我最近安装了很多次 gem pg 以确保它已安装。运行 psql --version 给我以下信息

psql (PostgreSQL) 9.3.4

运行 which psql 显示目录

/usr/local/bin/psql

在从 SQLite 转换到 Postgres 的过程中,我尽可能地更新了我的 database.yml 文件。

# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: postgresql
encoding: unicode
database: games_development
pool: 5
timeout: 5000
host: localhost

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: postgresql
database: games_test
pool: 5
timeout: 5000

production:
adapter: postgresql
database: games_production
pool: 5
timeout: 5000

如您所见,我将适配器从 sqlite3 更改为 postgresql,如 Heroku Dev Center 网站上所示。我用以下格式命名我的数据库。三个环境的appname_environement

我什至为开发环境添加了 host:localhost。

这是我在运行 cat pg_hba.conf 时收到的输出

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication edited trust
#host replication edited 127.0.0.1/32 trust
#host replication edited ::1/128 trust

现在我想描述一下我的问题。我可以成功部署到 Heroku,但在运行 heroku open 时收到以下错误消息。

Application Error

An error occurred in the application and your page could not be served. Please try again in a few moments.

If you are the application owner, check your logs for details.

我的下一步是运行 heroku logs 命令。

2014-06-19T16:14:31.269890+00:00 app[web.1]: => Booting WEBrick
2014-06-19T16:14:31.269894+00:00 app[web.1]: => Rails 4.1.1 application starting in production on http://0.0.0.0:36743
2014-06-19T16:14:31.269896+00:00 app[web.1]: => Run `rails server -h` for more startup options
2014-06-19T16:14:31.269898+00:00 app[web.1]: => Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
2014-06-19T16:14:31.269899+00:00 app[web.1]: => Ctrl-C to shutdown server
2014-06-19T16:14:31.269901+00:00 app[web.1]: Exiting
2014-06-19T16:14:32.519139+00:00 heroku[web.1]: State changed from starting to crashed
2014-06-19T16:14:32.507573+00:00 heroku[web.1]: Process exited with status 1
2014-06-19T16:17:43.026380+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=lit-anchorage-9017.herokuapp.com request_id=d2e55388-dbe6-439e-bba5-2f3f18eb3744 fwd="24.14.65.222" dyno= connect= service= status=503 bytes=

根据我对上述输出的理解,它首先在生产开发中运行 WEBrick 服务器。我确实知道我的“pg”gem 在我的 Gemfile 中指示的开发和测试环境中。我认为这可能是个问题。输出显示服务器正在退出,状态为 1。最后一行是金色的,这向我表明存在问题。

阅读该错误消息时,我看到错误代码 H10 以及描述“APP crashed”。继续前进,它显示状态代码 503,表示服务器出现问题。

我尝试运行 rake db:rollback 命令来重置数据库。

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?
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" (fe80::1) and accepting
TCP/IP connections on port 5432?
/Users/edited/.rvm/gems/ruby-2.1.0/gems/activerecord- 4.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:881:in `initialize'
/Users/edited/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:881:in `new'
/Users/edited/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:881:in `connect'
/Users/edited/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:568:in `initialize'
/Users/edited/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `new'
/Users/edited/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'

我立即注意到的一件事是它指示连接错误,它无法连接到服务器。我还注意到它指示端口 5432,而本地主机的公共(public)端口是 3000。消息随后显示了问题的踪迹,这似乎主要在 connection_adapter/postgresql_adapter 中。

上面的错误消息也显示在 rake db:migrate、rails c-sandbox、rails c 命令和 rails s 上。

我承认,当我创建这个应用程序时,它是在 sqlite 中创建的,因此可能会导致一些问题。但是,由于我需要将应用程序推送到 Heroku,因此我不得不将数据库更改为 postgres。

老实说,我觉得我已经用尽了所有的选择。我别无选择,只能在这里问这个问题。非常感谢任何建议。

最佳答案

希望这能解决您遇到的问题..

我注意到您没有在您的 gemfile 中为生产环境指定 postgresql gem。您仅为开发和测试环境指定了它。

将以下内容添加到您的 gemfile 中:

group :production do
gem 'pg'
gem 'rails_12factor'
end

'rails_12factor' gem 支持静态 Assets 服务和 Heroku 中的日志记录等功能,因为它们已从 Rails 4 中删除。

部署到 Heroku 时,您的应用无需在开发环境中运行 Postgresql 即可在 Heroku 上的生产环境中运行。

如果上述方法不起作用,此资源也可能有所帮助。祝一切顺利!

https://devcenter.heroku.com/articles/getting-started-with-rails4

关于ruby-on-rails - Heroku - 从 Sqlite 迁移到 Postgres 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24295333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com