教我自己将第一个应用程序部署到heroku -failure上传 - sqlite3和postgregitch

ban*_*ing 2 ruby-on-rails heroku railstutorial.org gemfile

我教自己铁路.我是程序员但不是网络程序员.我在这里浏览Michael Hartl的书

它提到从一开始就开始部署应用程序是一个好主意.我同意.所以我在Heroku上获得了一个帐户并经历了设置等.然后创建了我的第一个应用程序,git和工程.然后按照Heroku网站上的所有说明进行操作.最后,我使用此命令将我的应用程序推送到Heroku:

git push heroku master
Run Code Online (Sandbox Code Playgroud)

这本书说在这个阶段可能会出现问题,因为我的机器上的应用程序使用的是sqlite3而Heroku需要postgresql.这本书建议改变我的应用程序的gem文件中的一行

gem 'sqlite3'
Run Code Online (Sandbox Code Playgroud)

gem 'sqlite3-ruby', :group => :development
Run Code Online (Sandbox Code Playgroud)

我尝试了然后运行bundle install然后尝试推送应用程序.没有运气我在我的控制台上收到此消息:

An error occured while installing sqlite3 (1.3.6), and Bundler cannot continue.
   Make sure that `gem install sqlite3 -v '1.3.6'` succeeds before bundling.
Run Code Online (Sandbox Code Playgroud)

!!无法通过Bundler安装gem.!!Heroku推送拒绝,无法编译Ruby/rails应用程序

所以我尝试了Heroku网站在这里建议的内容

改变我的宝石文件

由此:

gem 'sqlite3'
Run Code Online (Sandbox Code Playgroud)

对此:

gem 'pg'
Run Code Online (Sandbox Code Playgroud)

所以我尝试了这种方法然后运行'捆绑安装'.但这也不起作用.我在控制台上收到此消息:

Installing pg (0.13.2) with native extensions 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /Users/AM/.rvm/rubies/ruby-1.9.2-p290/bin/ruby extconf.rb 
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
    --with-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/Users/AM/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
    --with-pg
    --without-pg
    --with-pg-dir
    --without-pg-dir
    --with-pg-include
    --without-pg-include=${pg-dir}/include
    --with-pg-lib
    --without-pg-lib=${pg-dir}/lib
    --with-pg-config
    --without-pg-config
    --with-pg_config
    --without-pg_config


Gem files will remain installed in /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.13.2     for inspection.
Results logged to /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.13.2/ext/gem_make.out
An error occured while installing pg (0.13.2), and Bundler cannot continue.
Make sure that `gem install pg -v '0.13.2'` succeeds before bundling.
Run Code Online (Sandbox Code Playgroud)

试图运行此命令:

gem install pg
Run Code Online (Sandbox Code Playgroud)

失败,出现以下错误消息:

Building native extensions.  This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

/Users/AM/.rvm/rubies/ruby-1.9.2-p290/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
--with-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/AM/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
--with-pg
--without-pg
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config


Gem files will remain installed in /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.13.2     for inspection.
Results logged to /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.13.2/ext/gem_make.out
Run Code Online (Sandbox Code Playgroud)

有人可以帮我这个.我遇到了障碍.不知道下一步该做什么.谢谢

Sim*_*tti 5

您的案例中的正确配置已经发布在其他答案中,为方便起见,我将其附在此处.

group :production do
  gem 'pg'
end

group :development, :test do
  gem 'sqlite3'
end
Run Code Online (Sandbox Code Playgroud)

但是,请记住,当您运行时bundle install,bundler将尝试为所有环境安装所有gem.稍后,它将在运行时仅加载指定环境的gem.

这就是为什么在本地计算机上也尝试安装的原因pg.

您有两种方法可以解决编译错误:

  1. 安装libpq,pggem编译所需的库.您不需要安装完整的PostgreSQL客户端或堆栈,libpq足以通过编译.在MacOSX上,您可能希望使用自制软件安装它.

  2. 在运行安装时告诉bundler跳过不必要的组.

    bundle install --without production
    
    Run Code Online (Sandbox Code Playgroud)

请注意,请注意SQLite和PostgreSQL是两个不同的数据库.使用尽可能与生产环境相似的开发环境始终是个好主意.我个人的建议是在本地机器上安装PostgreSQL,并在开发和生产中使用它.

再一次,自制软件是你的朋友.

$ brew install postgresql
Run Code Online (Sandbox Code Playgroud)