rake db:create - Mysql2::Error: Unknown database - ruby-on-rails

I am trying to create a database using rake db:create task.
When I am running the command bundle exec rake db:create to create the database and load the schema, I get the following error:
rake aborted!
Mysql2::Error: Unknown database 'xxx_development'
My database.yml:
development:
adapter: mysql2
host: localhost
reconnect: true
username: user
password: password
pool: 50
database: xxx_development
I checked that mysql server is running and I am able to connect to it using the password/username I have in the database.yml
I also understand that I can go ahead and create the database in mysql and then run the bundle exec rake db:create but isn't the rake task db:create also creates a database in case it doesn't exists?
The rails version is 3.2.22.
and mysql2 version is 2.9.13.
Any pointers will be highly appreciated.

If you are using ohmyzsh with bundler plugin which makes rake run by default with bundle exec, you can use
unbundled_rake db:create

Ran into a similar issue on Rails 5 and the following steps helped me fix it:
Spring stop
./bin/rails db:create

I have two ideas.
Check the User permissions
When I run rake tasks, I don't use bundle exec (i.e just rake db:create)
Hope this is helpful

Try rake db:create RAILS_ENV=development hope this will work

For some reason rails db:create failed but my workaround was
to create dbs directly in mysql
sudo mysql
create database myapp_development;
create database myapp_test;
After that run as normal
rake db:migrate db:seed

Related

Can't connect my development environment to a newly imported postgres database

Someone sent me the sql files of a dump database. I imported it through the following command: psql my_dbname < infile.sql
I changed my database.yml file with the following:
development:
<<: *default
database: my_dbname
But when I run rails server, I don't have the expected data. Is there something I'm missing ? I think there are some stuff I need to do with roles but I'm not sure.
Thanks :)
Are you done with rake db:create and rake db:migrate before importing the .sql file if not then do the following
rake db:create
rake db:migrate
and then import .sql file
psql my_dbname < infile.sql
You can try:
rake db:create
rake db:migrate
psql -d my_dbname -a -f infile.sql

Rails - FATAL: database "myapp_development" does not exist

I am trying to set up my Env to use PostgreSQL with Rails and i followed the steps to install PostgreSQL from this Article
Am getting the following error
ATAL: database "myapp_development" does not exist Extracted source
(around line #661):
rescue ::PG::Error => error
if error.message.include?("does not exist")
raise ActiveRecord::NoDatabaseError.new(error.message, error)
else
raise
end
Did you run rake db:create and rake db:migrate before rails server?
UPDATE
Here's all the steps you should do:
cd /your/app/path
bundle install
bundle exec rake db:create
bundle exec rake db:migrate
bundle exec rails server
Run
rails db:create
before running
rails db:migrate
You can also create PostgreSQL database manually by using psql command prompt.
When in there, connect to your local server and write "create database myapp_development;" without the quotes
I was having a similar problem. I checked different websites and tried what they suggested but didn't work. Then I tried rake db:create:all and rake db:migrate it worked for me. Thank you!
You can fix it by:
bundle exec rake db:setup
In my case, the solution was to provide what the error said.."create the database that does not exist"....I spent many hours trying different commands or run db:migrate using bundle but none of that worked. I thought it should create the db on it's own but no...I eventually decided to create the db in postgres
sudo su - postgres
psql
CREATE DATABASE tableNameFromDatabase.yml WITH OWNER myusername;
after this rails db:migrate worked

Rake db:reset 'user does not have CONNECT privilege'

So I'm using Heroku Postgres in my Rails app, but I'm not hosting my app on Heroku itself. I used the Active Record connection details from Heroku in my database.yml, and it looks like this:
development:
adapter: postgresql
encoding: unicode
pool: 5
database: [database]
username: [username]
password: [password]
host: ec2-54-227-243-78.compute-1.amazonaws.com
port: 5432
However, now I'm trying to rake db:migrate my app so the database gets all set up with my models. Running that command doesn't do anything, so I tried rake db:reset and I get this:
Couldn't drop df2cokjfj0k4vu : #<PG::Error: FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
df2cokjfj0k4vu already exists
-- initialize_schema_migrations_table()
-> 1.3997s
-- assume_migrated_upto_version(20130924040351, ["/home/action/braindb/db/migrate"])
-> 0.0882s
Any idea what I'm doing wrong. I'm still pretty new to Rails so sometimes I forget how to get my Postgres database setup properly when migrating hosts.
Use heroku pg:reset DATABASE instead as noted in https://devcenter.heroku.com/articles/rake
You cannot drop databases in Heroku with rake db:reset because user has no privileges.
You can't drop your PG database on Heroku.
I recently had this problem and resolved it through the following steps.
heroku pg --help gets the name of commands for using postgres
heroku pg:reset DATABASE # resets the db
Answer the prompt to confirm
Like others I had a similar problem but running pg:reset did not help: that's where I ran into the user does not have CONNECT privilege issue.
1) heroku pg:reset you will be prompted to enter the project name to confirm.
2) From Heroku's docs run heroku rake db:schema:load
I had the same problem. I fixed it by running:
heroku pg:reset DATABASE
(note: must specify DATABASE as above)
If you got this because you were running rake db:create, instead, simply skip to the migration. For whatever reason, postgresql on heroku doesn't require the rake db:create step as may be required on local installations.
In other words, instead of
heroku run rake db:create db:migrate db:seed
run this instead
heroku run rake db:migrate db:seed
The solution to me was to just go straight to migration because the database is already created (don't go on "heroku run rails db:create")
heroku run rails db:migrate -a YOURAPPNAME
For one of my apps, upgrading to the first paid tier of a Heroku database seemed to work for me: https://devcenter.heroku.com/articles/upgrade-heroku-postgres-with-pgbackups#provision-new-plan
Reason was that 'heroku pg:info' revealed that I was over my row limit as shown here:
Rows: 12392/10000 (Write access revoked)

why 'rake test' is trying to connect to my development DB?

I have my config/database.yml like this:
development:
adapter: postgresql
database: psql_dev
username: postgres
min_messages: WARNING
test:
adapter: sqlite3
database: db/test.sqlite3
min_messages: WARNING
When I run rake test:units, it reports an error:
rake aborted!
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Why didn't it connect to my test DB(db/test.sqlite3).
and, If I run the test like this rake test RAILS_ENV=test, it works well.
Isn't RAILS_ENV=test the default setting for rake test?
I'm running rails 2.3.5 with ruby 1.8.7, and my $RAILS_ENV is not defined in my shell.
What's happening is that rake test depends on rake db:test:prepare which will attempt to load the current schema from the development database. That's how the test database gets updated when a migration is run on the development database
do you have a test:units rake task? Run:
rake test
does that work? Also can you paste the output of:
rake -T | grep tests

using mysql instead of sqlite in a rails app

I'm not sure how to find what I'm looking for here, but I've cloned a rails app from someone else and they were using sqlite, how do I switch the project over to mysql? There are no migrations but it has a schema. Thanks.
Have you changed the database.yml?
eg:
development:
adapter: mysql
encoding: utf8
database: <db name here>
pool: 5
username: root
password: <p/w>
socket: /var/run/mysqld/mysqld.sock
You don't need a migration. rake db:setup (or rake db:schema:load)
EDIT: this is assuming the schema is a schema.rb file. If it's an SQL file you'll have to convert that and then just run it using the mysql client.
hope this will helps you.
If you have some errors in the Database.yml file, you might have to install the mysql gem:
gem install mysql
Have rake create your database
rake db:create
rake db:schema:load
Use YamlDb to reload your data into MySql
rake db:load
You really should use db:schema:load instead of db:migrate for creating new databases

Resources