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 ....
Related
I looked for hours on the web but none of my attempts to adapt my database.yml work out. I just started with Rails and the exercise is to use sqlite3 for development, and postgreSQL for production. I am using rvm 2.4.1 and rails 5.3.1.
I added this to my gemfile:
group :production do
gem 'pg'
end
And this is my 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
So that does not work out and I get the following error when I try to deploy to heroku:
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).
I am assuming I need to somehow let the production part lead to postgreSQL but the manuals on the heruko website and other posts on stackOverflow and elsewhere are not helping me. How can I get it work? I can add :default to the groups, gem 'pg' is in within my Gemfile but then other things like scaffolding produce errors later on. I would really be thankful to get specific help on this as a spend about 12 hours on this single issue already..
first, since you informed in your production using postgresql then you must change setting in database.yml (see production part)
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:
adapter: postgresql
encoding: unicode
database: project_name_production
pool: 5
host: localhost
username: your_username
password: your_database_password
and then you can follow this rake command in heroku
second about your gem file, if the problem still persist then you can put out from production environtment and push to heroku, and then do bundle install to install the adapter
GEMFILE
gem 'pg'
here is good for your to read to solve your problem link information for heroku with postgresql
I'm trying to create the database for my Rails app in production mode using Postgresql.
When I run the command:
$ RAILS_ENV=production rake db:create
I get the following error:
FATAL: database "postgres" does not exist
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "host"=>"localhost", "database"=>"provenword", "pool"=>5, "username"=>"postgres", "password"=>"password"}
I can create a database for older Rails apps that I have. However, only this one gives me this error. How can I get this to work so I can create the database and run the application?
Gem file:
..
gem 'pg'
..
Rails Version - 4.2.6
database YML
#
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
# default: &default
# user: postgres
# password: password
# 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:
adapter: postgresql
encoding: unicode
host: localhost
database: provenword
pool: 5
username: postgres
password: password
Postgresql is installed on my server.
Thanks to #Dharam.
It's work for me as well:
I beleive postgresql expects a database with the same name as the user
name, in your case postgres, be present. First setup a database with
the name postgres then you can run RAILS_ENV=production rake db:create
I have been been repeatedly getting this error despite trying to change many things.
I viewed other SO pages and changed indentation, names, etc. Really not sure what's going on. Any insight would be greatly appreciated.
# 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: lightpath_development
# 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: lightpath_test
production:
<<: *default
database: lightpath_production
Try switching back to the default 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: sqlite3
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
You're likely getting that error because you're trying to use Postgres database without properly configuring Postgres on your development machine/server.
For details on how to switch over to postgres you should watch this RailsCast episode.
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
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