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
Related
I am using rails 5.2.3
Here is my command line
RAILS_ENV=development bundle exec rake db:drop
I was expecting only one development db got dropped. But I got these two dbs got dropped. Something I have missed?
Dropped database 'db/development.sqlite3'
Dropped database 'db/test.sqlite3'
database.yml
# SQLite version 3.x
# 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
It's a known issue that when you run db tasks for development environment,they are also run for test envrionemnt.
You can check this issue on the following Rails github reop
https://github.com/rails/rails/issues/27299
As a workaround you can use this:
https://github.com/ioquatix/activerecord-migrations
it fixes some of these issues
Run: bin/rails db:environment:set RAILS_ENV=development
Then run bundle exec rake db:drop
It should only remove dev database ....
I'm new on RoR. I build a little app using ActiveAdmin and Devise and I wish to deploy it on Heroku.
When I had push my app on Heroku it run properly but the db seems to be empty ! In effect, my local login dont match when I try to log in my ActiveAdmin administration panel...
In addition, the others db of my app are totaly empty...
I guess that i had not fill the database.yml correctly but I dont find how I'm suppose to do it... :/
Database.yml :
# SQLite version 3.x
# 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
I would be grateful if you could help me or direct me to a solution!
Thank you for your attention ! ^^
production:
<<: *default
database: db/production.sqlite3
You can't use sqlite3 on heroku, because it is filesystem-based and on each restart a new dyno is created, with a completely new filesystem. The old dyno is killed and your sqlite3 data file with it.
Use a client-server DB on heroku, like Postgresql. This is well covered in heroku guides.
These steps is how to push the local database to heroku and to create database
heroku pg:backups:restore 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump' DATABASE_URL
Get database info
heroku pg
-> Gave me the name of the database
eg: HEROKU_POSTGRESQL_CYAN
Reset the database on heroku
heroku pg:reset HEROKU_POSTGRESQL_CYAN
Look for the name of the local database
Opened config/database.yml and found the database
name for the development environment.
eg: fashions_development => put the name that the terminal give it to you
Run push the local database to Heroku
Opened config/database.yml and found the database
heroku pg:push fashions_development HEROKU_POSTGRESQL_CYAN
So I've followed all of the guides I can find online for this issue, including similar stack posts, but things are not working.
I ran the command
heroku config:get DATABASE_URL
and got
postgres://<username>:<password>#<host_name>:<port>/<dbname>
(with all of the <> parts being my DB info of course) and ran
pg_dump --host=<host_name> --port=<port> --username=<username> --password <dbname> > output.sql
At this point I needed to run
psql -d <local_database_name> -f output.sql
but my rails app followed the Michael Hartl tutorial which uses sqlite3, so I had to switch things over. I tried this by changing my database.yml file to
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: postgresql
encoding: unicode
# adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development
# 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
# database: db/test.sqlite3
production:
<<: *default
database: db/production
# database: db/production.sqlite3
I also changed the Gemfile to use 'pg' instead of 'sqlite3' in development. At this point I ran bundle install and tried rails db:setup and rails db:migrate which did not create the databases (.. uh oh) and when I run the final step of
psql -d db/development -f db/output.sql
I get plenty of console output, but when I run a rails console sandbox, there is no user info from my Heroku database. Did I miss a step? What's going on? (I'm working on Ubuntu 14.04)
Edit:
First, I undid all of the local changes I made above. Then, I ended up following a combo of the above and the guide here http://manuelvanrijn.nl/blog/2012/01/18/convert-postgresql-to-sqlite/. I used heroku config:get DATABASE_URL to get all of the needed host, port, username, etc. info and then pulled the data with
pg_dump --host=<host_name> --port=<port> --username=<username> --password --data-only --inserts <dbname> > dump.sql
After that I followed the postgresql to sqlite database conversion (the "Longer story a.k.a please explain a little more." version) I linked above. I did all of the edits described in step 2 as well as having to remove a bunch of SELECT statements at the end of dump.sql. Another change I had to make was replacing all INSERT INTO instances with INSERT OR REPLACE INTO to satisfy some unique constraint errors in step 4. That did it!
I have a weird problem.
I have set my database.yml file to use sqlite3 for all three (production, test, dev) databases
I create a new rails project with all defaults.
I fire up rail server using WEBrick
I get "ActiveRecord::ConnectionNotEstablished" error
I try $rake db:create
I get the following error:
specified 'postgresql' for database adapter, but the gem is not loaded. Add gem 'pg' to your Gemfile.
I install pg and postgres server and I get
fe_sendauth: no password supplied Error on the webpage
I try $rake db:create again on the console and get
fe_sendauth: no password supplied (which I know is a postgres password error)
It seems that Rails is choosing a different database adapter than in my database.yml file.
I don't know where it could be. It even seems to be looking for a specific database that I used in some previous project. Therefore Rails must be looking at someother config file.
Can someone help.
Add sqlite3 gem to your Gemfile:
gem 'sqlite3'
and set sqlite3 as adapter in your database.yml:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
I created a new Ruby on Rails app (first app I'm working on in Rails 3), and for some reason, my app seems to be confused about what environment it should be running in (unless I'm just confused).
When I run rake db:create, it's creating both the "myapp_development" and "myapp_test" databases. When I subsequently run rake db:drop, it drops the development database, but not the test database.
What's going on?
Edit 1: Here's what my database.yml file looks like:
development:
adapter: mysql
database: myapp_development
username: root
password:
# 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: mysql
database: myapp_test
username: root
password:
production:
adapter: mysql
database: myapp_production
username: root
password:
Edit 2: Tried recreating a new app from scratch, and still have the same issue. Here's what I did.
1. Created a new rails app:
rails new myapp
2. Edited my Gemfile:
source 'http://rubygems.org'
gem 'rails', '3.0.6'
gem 'mysql', '2.8.1'
# Bundle the extra gems:
gem 'warden', '1.0.3'
gem 'devise', '1.2.1'
gem 'geokit'
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
group :development, :test do
gem 'ruby-debug'
end
3. Edited my database.yml:
development:
adapter: mysql
database: myapp_development
username: root
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:
adapter: mysql
database: myapp_test
username: root
password:
host: localhost
production:
adapter: mysql
database: myapp_production
username: root
password:
host: localhost
4. Running rake db:create at this point creates both myapp_development and myapp_test. If I proceed to run rake db:drop followed by rake db:create, I get a command line warning stating "myapp_test already exists" since the drop only removes myapp_development, but the create attempts to create both dev and test databases.
It would only create the development and test databases if you ran rake db:create:all, not rake db:create. The latter will only create one or the other, depending on the environment you're working in.
check your
config/database.yml
file in your app for the database configuration, you probably want test around though. but database.yml will let you access everything. It will use the development db by default though then you specify when to use production
Also a note, just running rake db:migrate will create the databases if they don't exist and migrate your models, you might want to use that instead