Heroku - 从Sqlite迁移到Postgres的麻烦

时间:2014-06-18 21:31:47

标签: ruby-on-rails postgresql heroku gem bundler

我使用内置数据库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

运行哪个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时,我收到以下错误消息。

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崩溃”。向前移动它会显示状态代码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,当localhost的公共端口是3000时。然后该消息显示了问题的痕迹,这似乎主要在connection_adapter / postgresql_adapter中。

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

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

说实话,我觉得我已经筋疲力尽了。我别无选择,只能在这里提出这个问题。非常感谢任何建议。

3 个答案:

答案 0 :(得分:1)

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

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

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

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

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

部署到Heroku时,您的应用程序不需要在开发环境中运行Postgresql,以便在Heroku上进行生产。

如果上述方法无效,该资源也可能会有所帮助。一切顺利!

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

答案 1 :(得分:0)

一种可能性。你有没有厌倦跑步" rake资产:预编译RAILS_ENV =生产 - 追踪"在开发环境中然后git add / commit在推送到Heroku之前? - 皮埃尔

答案 2 :(得分:0)

使用rails版本4.1.1上传到heroku时,您需要使用您提供的信息配置生产环境。 https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-ruby

Heroku替换了版本不是>的database.yml文件。如上所述,您必须在为您自动生成数据库时配置生产详细信息。

查看您的数据库详细信息:https://postgres.heroku.com/databases

   production: &production
   adapter: postgres://qrffxpnewoxxxxxxxxxxx
   host:  ec2-xxxxxxxxxxxxxxxxxxxxxxxxx
   database: db8qxxxxxxx
   port: 5432
   username: qrffxpxxxxxk
   password: G9CdRxxxxxxxxx