Migrating sqlite3 to postgres in rails app - ruby-on-rails

I have read several posts and watched the railscast on migrating from sqlite3 to pg and all of them seem to have conflicting settings and I have not seen anyone run into the error I'm getting. I have data locally that i do NOT care about. If it gets destroyed that's fine.
Here is what I have done.
Changed database.yml from this:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
To this:
development:
adapter: postgresql
database: app_name_database
pool: 5
timeout: 5000
One thing I did notice about this file and some of the examples is that I do not have encoding, username, or password. Is this necessary?
I updated my gem file from this:
group :production do
gem 'pg'
gem 'rails_12factor'
end
group :development do
gem 'sqlite3'
end
To this:
group :production do
gem 'pg'
gem 'rails_12factor'
end
group :development do
gem 'pg'
end
Following the instructions - the railscast says to rake db:create:all. However, when I run this I get the following error in console:
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
Any ideas on what I'm doing incorrectly? Are there rake tasks that need to be done before this?
Thanks

Have you started the Postgresql database ? This is how I start mine - pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start ?

Related

Gem::LoadError: when trying to deploy on Heroku - Rails 4

I am trying to deploy an app (just a simple app from a Rails tutorial) to heroku but it keeps giving me the same error message. I use the command:
git push heroku master
It starts well, and then suddenly appears this error:
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
Gem::LoadError: Specified 'sqlite3' for database adapter, but the gem is not loaded. Add `gem 'sqlite3'` to your Gemfile.
I did bundle install already, everything went smooth there.
Here is my Gemfile:
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
Maybe something I am missing something on databse.yml file?
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
By the way, I don't know if it helps, but I am using Rails 4.0.4, Ruby 2.1.1 and the version of SQLite which comes already installed on Mac, which is 3.7.13
Kirti is right in saying that Heroku does not support sqlite as adapter,
do the following
in the Gemfile:
group :production, :staging do
gem 'pg'
end
group :development, :test do
gem 'sqlite3'
end
in database.yml
production:
adapter: postgresql
database: name_of_your_db
pool: 5
timeout: 5000
SQLite is not intended as a production grade database. Instead Heroku provides production grade PostgreSQL databases as a service.
Read the details SQLite on Heroku

gem 'pg' can't be loaded only for rails application

I am using pg gem pg-0.17.1-x86-mingw32. It is working fine when i am connecting it from my ruby codes.But when i am trying to connect it via rails apps, it gives me an error::
"Specified 'postgresql' for database adapter, but the gem is not loaded. Add gem 'pg' to your Gemfile."
I am gettigng this error at the time of executing:: "rake db:migrate"
after changing the database.yml
My yml looks like::
development:
adapter: postgresql
database: postgres
host: localhost
user: postgres
password: postgre90
pool: 5
timeout: 5000
I am using::
Rails -4.0.4
Ruby -1.9.3
Please help.. I am a newbie in ruby on rails

Unable to connect to PostgreSQL when using SQLite?

The error I get is:
Could not load 'active_record/connection_adapters/postgresql_adapter'. Make sure that the adapter in config/database.yml is valid.
Here's my database.yml:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
user: test-app
encoding: unicode
And my Gemfile:
# Use sqlite3 as the database for Active Record
gem 'sqlite3', :group => :development
group :production do
# For Heroku
gem 'rails_12factor'
gem 'pg'
end
This identical setup works fine with another app, but for some reason refuses to here. Why is it even looking for PostgreSQL in development?
I figured out what the problem was.
I am using the gem dotenv to load my Heroku ENV vars from the .env file into Rails, one of which is a Heroku Postgres URL in the variable ENV['DATABASE_URL'] that was messing with Rails in development.
I removed it and Rails started working properly again.
In the database.yml file, the adapter in production should be 'postgresql' not 'pg'.

Deploying App to Heroku

When I want to deploy my app to heroku (using git push heroku master), it gives me an error and told me to install sqlite3 -v '1.3.6'. So after successfully installing that gem, I tried deploying it again to heroku and it still give me the same error!! However, I've already installed it. And now I can't even run my rails project locally (rails server). May I know what could be the cause of this?
Here's my content in database.yml file:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
Heroku does not work with SQLite3
Open your Gemfile and replace the line:
gem 'sqlite3'
for
group :production do
gem 'pg'
end
group :development, :test do
gem 'sqlite3'
end
I also suggest you to read the heroku instructions: https://devcenter.heroku.com/articles/rails3
Make your gemfile look like this
group :production do
gem 'pg'
end
group :development, :test do
gem 'sqlite3-ruby', :require => 'sqlite3'
end

Deploying RoR app to Heroku with SQLite 3 fails

I'm trying to deploy my first app to Heroku. I'm using SQLite as the database. As far as I know Heroku doesn't use SQLite - it switches to Postgres in the backend.
When I'm deploying I get the following error:
/usr/ruby1.8.7/lib/ruby/gems/1.8/gems/bundler-1.0.0/lib/bundler/runtime.rb:64:in
`require': no such file to load --
sqlite3 (LoadError)
My Gemfile (which is what I assume is causing this problem) looks as follows:
source 'http://rubygems.org'
gem 'rails', '3.0.0'
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
What am I doing wrong?
Heroku doesn't support SQLite databases. You need to use PostgreSQL on production, as I also explained in this post.
group :production do
gem "pg"
end
group :development, :test do
gem "sqlite3", "~> 1.3.0"
end
Actually, it's recommended to use in development/test an environment as close as possible to production. Therefore, I suggest you to switch all your environments to PostgreSQL.
# replace gem "sqlite3" with
gem "pg"
Simone Carletti is correct and so is Joost. You only need to group the sqlite3 gem or remove it entirely from your Gemfile. Heroku just needs to know that you don't want to use sqlite3 for production
So this:
...
group :development, :test do
gem "sqlite3-ruby", "~> 1.3.0", :require => "sqlite3"
end
...
Or this:
...
#No reference to sqlite3-ruby
...
If you remove the reference entirely you will probably mess up your local db though
After banging my head against this problem, I realized I was pushing the master branch of my repo to heroku, while I was making all of my postgres changes in my deploy-postgres branch of my repo!
I merged my deploy-postgres branch with my local master [git checkout master; git merge deploy-postgres] and then could run git push heroku master as per the heroku documentation.
I was stuck on this for hours looking at every answer here, but I couldn't get enough details to make it come together. This paged walked me through everything. http://railsapps.github.io/rails-heroku-tutorial.html
Good luck.
i was facing similar issue, but I realized that I was on a different branch - new_layout and was pushing master.
So I pushed my desired branch to heroku using following command and everything worked fine.
git push heroku new_layout:master
You can use clearDB addon
and gem 'mysql2' instead of gem 'sqlite3'
I'm using sqlite3 and deploy to Heroku no problem. Here is my database.yml
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000

Resources