teaching myself rails deploying first app to heroku -failure to upload - sqlite3 and postgre glitch

房东的猫 提交于 2019-12-10 11:27:38

问题


Im teaching myself rails. Im a programmer but not a web programmer. Im going through Michael Hartl's book here

It mentions that its a good idea to start deploying your app right from the beginning. I agree. So I got an account on Heroku and went through ther setup etc. Then created my 1st app, git and the works. Then followed all the instructions on Heroku's site. Finally I came to pushing my app up to Heroku using this command:

git push heroku master

The book says that there could be problems at this stage because the app on my machine is using sqlite3 while Heroku wants postgresql. The book suggests changing a line in the gem file of my app from

gem 'sqlite3'

to

gem 'sqlite3-ruby', :group => :development

I tried that and then ran bundle install and then tried to push the app. No luck I get this message on my console:

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.

! ! Failed to install gems via Bundler. ! ! Heroku push rejected, failed to compile Ruby/rails app

So I tried what the Heroku website suggests here

to change my gemfile

from this:

gem 'sqlite3'

to this:

gem 'pg'

so I tried this approach and then ran 'bundle install'. But that didn't work either. I get this message on my console:

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.

Tried to run this command:

gem install pg

that failed with the following error message:

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

Can someone please help me with this. ive hit a roadblock. Not sure what to do next.Thanks


回答1:


The right configuration in your case has already been posted in other answers and I attach it here for your convenience.

group :production do
  gem 'pg'
end

group :development, :test do
  gem 'sqlite3'
end

However, keep in mind that when you run bundle install, bundler will try to install all gems for all environments. Later, it will load only the gems for the specified environment at runtime.

This is the reason why on your local machine it is trying to install pg as well.

You have two possibilities to solve the compilation error:

  1. Install libpq, the library required by the pg gem to compile. You don't need to install the full PostgreSQL client or stack, libpq is enough to pass compilation. On MacOSX you might want to install it using homebrew.

  2. Tell bundler to skip unnecessary groups when running install.

    bundle install --without production
    

As a side note, please keep in mind that SQLite and PostgreSQL are two different databases. It's always a good idea to use a development environment as similar as possible to the production one. My personal suggestion is to install PostgreSQL on your local machine and use it both on development and production.

Again, homebrew is your friend.

$ brew install postgresql



回答2:


You'll want to install Postgres locally. It appears that you're on a Mac, so the easiest way would be to use the installer here:

http://www.postgresql.org/download/macosx/

Then stick with this in your Gemfile:

gem 'pg'



回答3:


So this is what worked: 1) Install postgresql on the machine

2) For permanent fix - Go to the .bash_rc file and add the following: export PATH = "/Library/PostgreSQL/9.1/bin/":$PATH OR just to install the missing files and run the bundle command one time type this in the root of the app: PATH=$PATH:/Library/PostgreSQL/9.1/bin/ bundle install or PATH=$PATH:/Library/PostgreSQL/9.1/bin/ gem install pg

3) Now the pg gem is installed

This post was helpful: http://excid3.com/blog/installing-postgresql-and-pg-gem-on-mac-osx/




回答4:


Try

group :production do
  gem 'pg'
end

group :development do
  gem 'sqlite3'
end



回答5:


I think the reason for the error that the pg is not installed on your system, so you can not install the gem.

Although heroku not advised to use a different database, I have worked like this:

group :test, :development do
  gem 'sqlite3'
end

group :production do
  gem 'pg'
end


来源:https://stackoverflow.com/questions/10321189/teaching-myself-rails-deploying-first-app-to-heroku-failure-to-upload-sqlite3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!