I experimented a few weeks ago by adding a second database to my database.yml. While everything worked, I didn't like the approach and removed all the models etc of that second DB. The primary database was postgresql and the second was sqlite3. The db location was the absolute path to the sqlite3 db in database.yml.
Every once in a while I will dump my production db on the server and pull it into my development db. I'd do this with:
rake db:drop
rake db:create
psql -d {database} < {output from pg_dump}
The first time I tried this procedure after my experiment, I got a rake error:
xuserAir:db xuser$ rake db:create
(in /Users/xuser/Work/v/vfw-post)
/Users/xuser/vfw/gnucash/export/vfwexport.gnucash already exists
vfw-post_test already exists
rake aborted!
Gem::LoadError: Specified 'sqlite3' for database adapter, but the gem is not loaded. Add `gem 'sqlite3'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
Gem::LoadError: sqlite3 is not part of the bundle. Add it to Gemfile.
There is no reference anywhere in my application root to sqlite3, the database path or any of the file names or directory. rake db seems to be picking up the old information from someplace by I have no idea where. I may have had db/development.db set up as a symbolic like to the export.db, but that is also gone.
Also rake db:drop deleted the sqlite3 database that was outside the app root.
I striped everything in my database.yml file:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: vfw-post_development
test:
<<: *default
database: vfw-post_test
production:
<<: *default
database: vfw-post_production
username: vfw
# password: <%= ENV['VFW-POST_DATABASE_PASSWORD'] %>
And still get the same error.
Stuck
Rails 4.2
I found out where it was coming from.
Somewhere in my experiment with two database I set up
DATABASE_URL=sqlite3:/Users/xuser/vfw/gnucash/export/vfwexport.gnucash
I think to help when I deployed. I didn't remove that and it goes to the DATABASE_URL first.
Related
As the title says, I clones a rails API. I tried to follow the steps in this article from point 2 onwards https://dev.to/w3ndo/a-checklist-for-setting-up-a-cloned-rails-application-locally-5468 but I keep getting the same error from db:setup onwards.
Please help!
I have tried googling the answer and phoning a friend.
I have tried rails db:setup, rails db:seed, rails db:create, rails db:migrate.
Update: So I found I was getting this error because the db owner was listed as the original owner in the repo but when I typed psql in terminal and located the db, the owner was listed as me.
I was able to change this using PGadmin 4 and type in the original owner as the db owner.
You want to initialize the postgres db, which doesn't quite come for free. I recommend using sqlite3 until you need a production db. If the clone calls for PG, then:
(user) $ sudo su - postgres
(postgres) $ createuser --interactive
(local) $ sudo systemctl restart postgresql
(local) $ bundle exec rails db:create:all
$ pg_isready tells you at a glance if posgtres server/cluster is online.
$ pg_isready
/tmp:5432 - accepting connections
If it gets frustrating, change the config/database.yml to the default version, remove pg gem if possible, add sqlite3. Then simple rake db:migrate after creating or adding an [environment].sqlite3 file to db/
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 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
Update
Thank you for the help. I was able to find out as my partner had created the backend, the issue was arising because the owners of the database didn't match when I cloned his repo.
So I found I was getting this error because the db owner was listed as the original owner in the repo but when I typed psql in terminal and located the db, the owner was listed as me.
I was able to change this using PGadmin 4 and type in the original owner as the db owner.
I am upgrading my rails app from Sqlite3 to Postgresql. I am using Ansible to setup my server and Capistrano to deploy. I was able to successfully run ansible and now I'm trying to deploy my branch to an existing server that had Sqlite3 installed on it.
On the capistrano step:
deploy:assets:precompile
I get this error
$HOME/.rbenv/bin/rbenv exec bundle exec rake assets:precompile
01 rake aborted!
01 Gem::LoadError: Specified 'sqlite3' for database adapter, but the gem is not loaded. Add `gem 'sqlite3'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
But I don't understand. I have Postgresql set up in my branch I'm trying to deploy.
Here is my database yml file.
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: {appname}_development
test:
<<: *default
database: {appname}_test
staging:
<<: *default
database: {appname}_staging
username: <%= Rails.application.secrets[:db_username] %>
password: <%= Rails.application.secrets[:db_password] %>
Obviously changed {appname} with my actual application's name. I didn't include a production key cause this app does not have a production website only a staging server. It is more of a playground I use for rails.
Does anyone have any experience with fixing this issue? I'm really not sure if there is an issue with the Capfile or if I'm missing something. Thanks (and let me know if you need more of my code)
Worth noting this app is still running Rails 5.1 (I'm going to update this next I just wanted to get it on a Postgres db first)
EDIT: OK I am onto something. i discovered that this capistrano step:
04 ln -s /var/www/{appname}/shared/config/database.yml /var/www/{appname}/releases/20190923224949/config/database.yml
Is not updating my database yml to be the updated postgres db. I'm guessing I'm missing something somewhere. :cry:
I GOT IT. I'M A GENIUS.
I had to rereun the webserver playbook to get my database.yml file onto the server.
I am trying to deploy to Heroku but can't because the default sqlite3 server is still in place.
Detected sqlite3 gem which is not supported on Heroku.
https://devcenter.heroku.com/articles/sqlite3
In another tutorial with Rails 3.2.13 I was able to use sqlite3 as the dev db and Postgres as the production db. The Gemfile looks different in Rails 4 but I have modified it to have this:
group :development do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
end
group :production do
gem 'pg'
end
I then changed my database.yml file so that the production section looked like this:
production:
adapter: postgresql
database: my_production_database
pool: 5
timeout: 5000
I then ran bundle install and rake db:create and rake db:migrate but am still unable to push to Heroku. So I tried rake db:drop as well as rake db:create and rake db:migrate but am still getting the same error message.
Am I missing something? What else do I need to do to make sure I'm getting Postgres as my production database and am able to use Heroku?
Don't do it. You are just going to run into problems down the road. Use the same database in production and development. There are a lot of resources available in documenting the switch from a sqlite to postgres database.
Take the time and switch.
Have a look at this Rails Cast.
http://railscasts.com/episodes/342-migrating-to-postgresql?view=asciicast
Try using this for your production DB
production:
adapter: postgresql
host: localhost
encoding: unicode
database: my_production_database
pool: 5
username:
password:
You can leave username and password blank
I've created my stack on heroku and everything has been deployed but when I try to actually visit it via URL it just defaults to the 500.html error page. The app ran fine on my localhost, but I developed it in sqlite3. I've since changed my Gemfile to look like the following and ran bundle install.
#gem 'sqlite3'
gem 'thin'
group :production do
gem 'pg'
end
group :development, :test do
gem 'sqlite3'
end
When you visit the url it should be pointing to my login page.
This is what my database.yml file looks like...does this have anything to do with my problem.
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
Thanks for any advice
Use heroku logs to see the log file showing the 500 error's cause. If you haven't run your migrations that might be the cause. Be sure to run:
heroku run rake db:migrate
before you use your app.
Your database.yml file points to SQLite still. You'll need to change that to point to the PostgreSQL DB on heroku.
I can see from the comment that you are using sqlite for production into heroku. Which is not possible since heroku doesnt support sqlite. You can either use pg or mysql2 for your production environment. You can check here for solution :
How to configure database.yml for deployment to Heroku
And once you are done with it run these commands
heroku run rake db:reset
heroku run rake db:drop db:create db:seed
heroku run rake db:migrate
I typed cd generate and then rake db:migrate, but the CMD shows that rake aborted could not open database you can refer to chap2 of the book [Ruby on Rails] OReilly Head First Rails Jan A learner's companion to Ruby on Rails 2009
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development: adapter: sqlite3 database: db/development.sqlite3 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 timeout: 5000
production: adapter: sqlite3 database: db/production.sqlite3 timeout: 5000
Sounds like your database is not properly configured. Make sure that config/database.yml matches the settings for your computer, and that you can log into the database on the host and port specified with the username and password specified.
It might help to see config/database.yml and the details of your database setup.
If your config/database.yml and adapter are configured correctly, try running rake db:create:all before running rake db:migrate