Switching Rails app from SQLite to Postgres for local development - ruby-on-rails

I have a little Rails app that is deployed on Heroku. They require a Postgres database, which was not a problem to switch from SQLite3 on their side, but now I can't develop locally without pushing to Heroku every time I want to see changes. I downloaded and installed all of the Postgres assets including the little MenuBar app that keeps a server running and have been all over documentation. Any help?

try this
write in your gemfile
group :development do
gem "sqlite3"
end
group :production do
gem 'pg'
end
when you are working on development mode then it used sqlite3
and while you are working on production mode then it used pg

Related

Deploy MySQL database on Heroku with Ruby on Rails

I am trying to deploy a Rails project on Heroku. My Rails application uses mysql2.
I have tried using the taps gem but its not working. I get the following error when I run the command taps server mysql://root#localhost/heroku_ex tempuser tempass:
Failed to connect to database: Sequel::AdapterNotFound -> LoadError: cannot load such file -- mysql
Is there any way to deploy my application on Heroku? I would prefer to only use free add-ons if possible.
There are a few moving parts here.
First of all, Heroku doesn't support MySQL by default. If you want to use MySQL instead of PostgreSQL you will have to provision an add-on that provides it. There are currently at least two add-ons that provide MySQL or MariaDB¹ support with a free tier².
Next, Heroku doesn't run database servers on localhost. How can you handle different database configurations between your development machine and your Heroku server?
One strategy that is endorsed by Heroku is to store configuration in the environment. Following this model lets you alter your application's configuration by modifying environment variables instead of editing files. Luckily, Rails appears to override config/database.yml with configuration from the DATABASE_URL variable by default, so this approach should be a good fit.
Very often database add-ons will automatically set an environment variable for you. For example, the JawsDB Maria add-on sets JAWSDB_MARIA_URL when it is provisioned. This isn't the variable that Rails looks for, so you'll either have to tell Rails to look for JAWSDB_MARIA_URL instead of DATABASE_URL or manually set DATABASE_URL to contain the same URL that JawsDB provides in JAWSDB_MARIA_URL.
¹MariaDB is a fork of MySQL that aims to be fully-compatible.
²Note that the free tier may be quite limited, e.g. by only providing 5MB of storage. You may have to upgrade to a paid database tier as you continue to develop your application.
I have solved this by adding the pg gem in production and mysql2 in development environment.
group :development, :test do
gem 'mysql2'
end
group :production do
gem 'listen', '~> 3.0.5'
gem 'pg'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end

How to run ruby on rails apps with different versions of rails?

I have about 3 apps on a Windows Server 2012 and all of them are different versions of rails. So far, I can run locally the app with the latest version of rails, but not the other two, which were deployed from Heroku. I wanted to make changes locally before I push them to Heroku but no luck. Is there any reference to that or is this even possible? thanks a lot!
Your problem is that you are trying to use sqlite with Heroku. Heroku requires using 'postgres' as the database
change the gemfile to the following
group :development do
gem 'sqlite3'
end
group :production do
gem 'pg'
end
And then change your database.yml file to this
adapter: postgresql

Rails: steps to take before deploying rails app (e.g. changing database to postgres, etc)?

Hi I am about to deploy a rails app (onto heroku) I made and wanted to know what the procedure is. I know you have to change the database by changing gem 'sqlite3' to gem 'pg' but after I did that and went on my localhost:3000 it gives me the error:
ActiveRecord::ConnectionNotEstablished
What else am I missing?
You don't need to change gem. Database used in application on heroku default is postgres. Keep gem 'sqlite3' and add this in your Gemfile:
group :production do
gem "pg", "0.14.0"
end

Ruby Gemfile & database adapters

I am developing on an open source rails application and as a rails 3 project, it includes a Gemfile. The application is also dependent on a database adapter for active record. I can specify a specific adapter (like gem 'sqlite3') within the Gemfile but this is not what I want to do. I advertise the app as beeing compatible with all the adapters active record is compatible with. Is there any way to specify that the app depends on a single database adapter (out of a selection) but that one is sufficient? Something like
any_of do
gem 'sqlite'
gem 'mysql'
etc.
end
Thanks for any help or suggestions how to approach the problem differently.
I just recently installed something that generated an error, until I realized how they handled this.
group :postgresql do
gem 'pg'
end
group :mysql do
gem 'mysql2'
end
and then when you run bundler:
bundle install --without postgresql

why Heroku does not download database updates with 'Heroku db: pull'?

I have run 'heroku db:push' to download heroku db updates, it show me error:
taps load error: no such file to load --sqlite3
you may need to install or update the taps gem to use db commands.
on most systems this will be: sudo gem install taps
I have installed heroku 2.25.0, taps 0.3.24 and I changed the Gemfile replacing gem sqlite3 with:
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg'
end
I think it's a problem of compatibility database between sqlite e postgresql.
is it possible? Or what can be ?
First, make sure you've got your gems all up to date by running bundle install. It won't hurt if everything's fine so you may as well make sure.
After that, you should be able to use db:pull to pull a copy to your local machine using the snippet that follows:
bundle exec heroku db:pull sqlite://local.db --app my_app
If this doesn't work for some reason, you can just change your Gemfile while pulling the database down -- and then you can change it back if you want to.
group :development, :test do
gem 'sqlite3'
# add this next line and run 'bundle install' until you get db:pull to work
gem 'pg'
end
group :production do
gem 'pg'
end
(I believe this will work, though I personally haven't tried it. Please respond if there's an issue.)
Finally, there are some issues pulling and pushing databases if you're running ruby 1.9.3 on your local machine -- but it's only if you're doing a db:push. If you have problems with that here's a link to the solution.
When you run the heroku command, it runs locally on your development machine. So the development group of your Gemfile is used:
group :development, :test do
gem 'sqlite3'
end
Since it contains sqlite3, you need the sqlite3 gem to be installed on your development machine. Have you run bundle install to install this gem locally? And you may need to run the heroku command within a bundle exec call to make sure the gems are loaded:
bundle exec heroku db:pull.
Finally, if you want to download Heroku DB, you should use db:pull, not db:push.

Resources