Postgres gem not loaded on initial Rails 4 app deployment - ruby-on-rails

I got my Rails 4 app running on my local machine and on heroku at the moment. Now I tried to set up an ubutunu 14 server. For deployment I am using capistrano and the connection between my local machine and the server is already set. Currently I am trying to do the initial deploy with this command:
cap production deploy:initial
I get following error at a certain point:
Running ~/.rvm/bin/rvm default do bundle exec rake assets:precompile on MYIP
DEBUG Command: cd /home/deploy/apps/savoir/releases/20160209113448 && ( export RAILS_ENV="production" ; ~/.rvm/bin/rvm default do bundle exec rake assets:precompile )
DEBUG rake aborted!
DEBUG Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
My gemfile looks like this:
source 'http://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.8'
# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0.1'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin]
gem 'bootstrap-sass', '~> 3.3.5.1'
gem 'autoprefixer-rails'
gem 'bootstrap-sass-extras'
gem 'bootstrap-daterangepicker-rails'
gem 'bootstrap-datepicker-rails'
gem 'simple_form'
gem 'nested_form'
gem 'ckeditor'
gem 'coffee-script-source', '1.8.0'
gem 'carrierwave', github:'carrierwaveuploader/carrierwave'
gem 'carrierwave-crop'
gem 'rmagick'
gem 'mini_magick'
gem 'momentjs-rails'
gem 'gon'
gem 'jquery-ui-rails'
gem 'jquery-fileupload-rails'
gem 'jquery-turbolinks'
gem 'jquery-validation-rails'
gem 'devise'
gem 'ransack'
gem 'will_paginate'
gem 'rails_12factor'
gem 'pg', '~> 0.18.3'
gem 'geocoder'
gem 'gmaps4rails'
gem 'underscore-rails'
group :development do
gem 'capistrano', require: false
gem 'capistrano-rvm', require: false
gem 'capistrano-rails', require: false
gem 'capistrano-bundler', require: false
gem 'capistrano3-puma', require: false
end
gem 'puma'
And my database.yml:
development:
adapter: postgresql
database: savoir_development
pool: 5
username: postgres
password: root
test:
adapter: postgresql
database: savoir_test
pool: 5
timeout: 5000
username: postgres
password: root
production:
adapter: postgresql
database: deploy
pool: 5
timeout: 5000
username: USER
password: PASSWORD
So as far as I can see it, the gemfile and database.yml should be fine. Can anyone find the mistake(s) I made? I found a few other threads that worked this topic but they didn't help in my case.

Ok, I found the solution.
Since I was using windows on my local machine, rails used this version of pg:
//Gemlock.lock
pg (0.18.4-x86-mingw32)
Changing it to
//Gemlock.lock
pg (0.18.4)
and pushing it to the git solved my problem.

Related

heroku rake db:migrate is not working in rails 4

When I do rake db:migrate, I get the error,below i have attached my databse.yml and my gem file, tried all the stuff on the internet but the error did'nt get resolved!
rake aborted!
LoadError: cannot load such file -- mysql2
development:
adapter: mysql2
encoding: utf8
database: demo_project_development
pool: 5
username: root
password: root
socket: /var/run/mysqld/mysqld.sock
host: localhost
test:
adapter: mysql2
database: demo_project_test
database: db/development.mysql2
username: root
password: root
pool: 5
timeout: 5000
production:
adapter: postgresql
database: demo_project_production
pool: 5
timeout: 5000
Gemfile
source 'https://rubygems.org'
gem 'rails', '4.2.6'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
group :production do
gem 'pg', '0.20'
gem 'rails_12factor'
end
group :test do
gem 'byebug'
gem 'mysql2', '>= 0.3.13', '< 0.5'
end
gem 'spring'
gem 'devise'
gem "cancan"
gem 'ckeditor', '4.1.3'
gem "nested_form"
gem "paperclip", "~> 5.0.0"
gem 'bootstrap-sass', '~> 3.3.6'
gem 'kaminari'
gem 'ratyrate'
gem 'thinking-sphinx', '~> 3.3.0'
gem 'delayed_job_active_record'
gem 'rails-api'
gem 'active_model_serializers', '~> 0.10.6'
group :development do
gem 'web-console', '~> 2.0'
gem 'mysql2', '>= 0.3.13', '< 0.5'
end
Below is my application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)
module DemoProject
class Application < Rails::Application
config.active_record.raise_in_transactional_callbacks = true
config.active_job.queue_adapter = :delayed_job
config.api_only = false
end
end
config/envoirment.rb
require File.expand_path('../application', __FILE__)
Rails.application.initialize!
boot.rb
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
I was able to reproduce your error in a dummy Rails 4.2.6 app with your Gemfile and database.yml. I couldn't even push the app to Heroku without raising LoadError: cannot load such file -- mysql2.
I fixed the problem by removing the mysql2 gem from the development and test groups and adding it to the default group. My guess is that running bundle install locally adds it as a dependency to Gemfile.lock which is checked into git and pushed to Heroku. Heroku doesn't install the gem and when the app tries to require it a LoadError is raised.
group :test do
gem 'byebug'
end
group :development do
gem 'web-console', '~> 2.0'
end
gem 'mysql2', '>= 0.3.13', '< 0.5'
A couple notes: if you want to add one gem to two or more specific groups, do not specify them separately. This just means there are two places where you're going to have to remember to change your version dependencies. So instead of this
group :test do
gem 'byebug'
gem 'another_gem'
end
group :development do
gem 'web-console', '~> 2.0'
gem 'another_gem'
end
do this
group :test do
gem 'byebug'
end
group :development do
gem 'web-console', '~> 2.0'
end
group :development, :test do
gem 'another_gem'
end
Also, you shouldn't use MySQL locally and PostgreSQL in production, just use Postgres for both. They are not fully compatible, so you are bound to run into confusing issues down the road. Your best solution to this problem would be to remove mysql2 altogether and configure your dev and test databases for Postgres.

PG::ConnectionBad: could not connect to server (heroku)

i can push to heroku just fine.. however when I run a rake i get this error:
PG::ConnectionBad: could not connect to server
I can run locally ok so not really sure why I am having trouble pushing to heroku - any help would be great
I suspect this is related to my database.yml file:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# 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:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
Also here is my gemfile:
source 'http://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.8'
# Use sqlite3 as the database for Active Record
group :development do
gem 'sqlite3'
gem 'capistrano'
end
group :production do
gem 'pg'
gem 'mysql2'
gem 'activerecord-mysql-adapter'
gem 'rails_12factor'
end
# Use SCSS for stylesheets
gem 'coffee-rails'
gem 'bootstrap-sass', '~> 3.3.3'
gem 'sass-rails', '>= 3.2'
gem 'autoprefixer-rails'
gem 'will_paginate', '~> 3.0.7'
gem 'will_paginate-bootstrap'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin]
You should not commit database.yml into heroku:
When Rails applications are deployed to Heroku a database.yml file is
automatically generated for your application that configures
ActiveRecord to use a PostgreSQL connection and to connect to the
database located at DATABASE_URL. This behavior is only needed up to
Rails 4.1. Any later version contains direct support for specifying a
connection URL and configuration in the database.yml so we do not have
to overwrite it.
Read heroku documentation about this.

ActiveRecord::StatementInvalid (SQLite3::ReadOnlyException: when using postgres

I am trying to deploy my first rails application on digital ocean in Ubuntu, I am unsure if I am doing this right. I initially had my production database in postgres and test and production in sqllite. In frustration, I changed all my databases to postgres my changing the database.yml. I am unsure if I did that correctly But I had this error: ActiveRecord::StatementInvalid (SQLite3::ReadOnlyException: . When I was in sqllite. I was not sure if I had to tell the server to switch to production mode or if it was configured to use sqllite.
What I want to do is use postgres instead of sqllite. I am using nginx and Unicorn. MY database is blank so I dont need to transfer anything. I have made the postgres database within postgres, I just need to point my app to that database. (I am unaware if I need to do something else
I have used this database.yml
development:
adapter: postgresql
encoding: unicode
host: localhost
database: blog_development
pool: 5
username: bob
password: password
test:
adapter: postgresql
encoding: unicode
database: blog_test
host: localhost
pool: 5
username: bob
password: password
production:
adapter: postgresql
encoding: unicode
host: localhost
database: blog_production
pool: 5
username: bob
password: password
Gem file:
source 'https://rubygems.org'
ruby '2.0.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.4'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'jquery-ui-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
gem 'bootstrap-sass' # for using bootstrap-rails"
gem 'faker'
gem 'will_paginate'
gem 'annotate', '~> 2.6.5'
gem 'font-awesome-rails' # for using font-awesome icons
gem 'redcarpet', '~> 2.1.1'
gem 'coderay', '~> 1.1.0' # For nice code snippets
gem 'devise'
gem 'sidekiq'
gem 'haml-rails'
group :development do
gem 'better_errors'
gem 'binding_of_caller'
gem 'meta_request'
gem 'guard-rspec'
end
group :test do
gem 'capybara'
gem 'factory_girl_rails', '4.2.0'
end
gem 'pg', '0.15.1'
group :development, :test do
gem 'rspec-rails'
# Use sqlite3 as the database for Active Record in testing
end
group :production do
gem 'rails_12factor', '0.0.2'
end
I have the rest of the code on my github: https://github.com/RubyQuarry/Bootstrap_blog
Run rake db:drop db:create db:drop to drop your old database(s) and recreate the new ones in Postgres.

Can't start rails server - sqlite3 adapter error

I'm trying to teach myself ruby on rails and I'm trying to create a simple application to start with. I'm following the tutorial at http://guides.rubyonrails.org/getting_started.html#creating-a-new-rails-project but am having difficulty starting the server. When I try start up the server and go to the localhost,
Could not load 'active_record/connection_adapters/sqlite3_adapter'. Make sure that the adapter in
config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or
'sqlite3' add the necessary adapter gem to the Gemfile.
I'm tried to look up a solution/reason why this happens on other questions but to no avail.
My Gemfile looks like this
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.6'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more:
https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin]
I'm using
Ruby 2.1.3
and when I type
gem list
I find I'm running
rails 4.1.6
sqlite3 (1.3.9 x86-mingw32)
Can anyone point me in the right direction?
Make sure your config/database.yml has an appropriate database driver set for current environment:
development:
adapter: sqlite3 # check if you have sqlite3 here
encoding: ...
database: ...
pool: ...
username: ...
password: ...
Also don't forget to set it for all other environments you want to use sqlite3 in.
I've never used sqlite but from the error I'm guessing you have a line in database.yml that says:
adapter: sqlite3_adapter
that should instead be:
adapter: sqlite3
Edit: ninja'd
You can put your sqlite3 gem to development in Gemfile
group :development do
gem 'sqlite3'
//*Your other gem to only development
end
wrong adapter in your database.yml file.
use this:
adapter: sqlite3

New Rails app Postgres won't install

I just started a Rails 4 app from scratch with the intention to use Postgres. I went and changed the database.yml file to the following:
development:
adapter: postgresql
encoding: unicode
database: personalapi_dev
pool: 5
username: nickoneill
password:
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: &test
adapter: postgresql
encoding: unicode
database: personalapi_test
pool: 5
username: nickoneill
password:
host: localhost
Yet when I run rake db:create it gives me the following error:
uninitialized constant ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::PG
/Users/nickoneill/.rvm/gems/ruby-2.0.0-p247#personalapi/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:562:in `active?'
/Users/nickoneill/.rvm/gems/ruby-2.0.0-p247#personalapi/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:360:in `verify!'
/Users/nickoneill/.rvm/gems/ruby-2.0.0-p247#personalapi/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:458:in `block in checkout_and_verify'
/Users/nickoneill/.rvm/gems/ruby-2.0.0-p247#personalapi/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:373:in `_run__1536634998624253346__checkout__callbacks'
/Users/nickoneill/.rvm/gems/ruby-2.0.0-p247#personalapi/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
I've tried everything from upgrading the Postgres gem, making sure Postgres is running, etc, yet not sure why it's not working. I have other rails apps (including Rails 4) on my computer that run just fine yet this new one won't work. Any ideas?
Update
Here's my gemfile:
source 'https://rubygems.org'
ruby "2.0.0"
# gem 'bootstrap-sass', '~> 2.3.2'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'
gem 'addressable'
#
# Error handling
#
gem "sentry-raven" #, :git => "https://github.com/getsentry/raven-ruby.git"
#
# Sidekiq gems
#
gem 'sidekiq' , '2.5.4'
gem 'redis' , '~> 3.0'
gem 'unicorn' , '~> 4.5'
# For sidekiq interface
#gem 'sinatra', require: false
gem 'slim'
gem 'sinatra', '>= 1.3.0', :require => nil
# json parsing
gem 'oj' , '2.0.1'
# templates
gem 'haml'
# file storage
# gem "paperclip", "~> 3.1"
# gem 'aws-sdk', '1.8.0'
# Use postgres as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
group :development , :test do
gem "better_errors"
gem 'fakeweb'
gem 'rspec-rails', '2.12.2'
gem 'shoulda-matchers'
gem 'guard-rspec'
end
group :test do
#gem "cucumber-rails", ">= 1.3.0"
gem 'database_cleaner'
gem 'factory_girl_rails' , '~> 4.0'
gem 'capybara'
gem 'guard-spork'
gem 'spork'
gem "launchy"
gem 'rb-fsevent', '~> 0.9.1'
gem 'faker'
gem 'annotate'
gem 'simplecov'
gem 'guard-rspec'
gem 'growl'
gem 'rb-fsevent', '~> 0.9.1'
end
From my experience updating the Gemfile to gem 'pg', '0.15.1' solved the problem.

Resources