rails is not using the configuration set on database.yaml - ruby-on-rails

This is my database.yml
production:
adapter: mysql2
encoding: utf8
database: testing
pool: 5
username: ubuntu
host: 127.0.0.1
When I run rake db:setup I got this:
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "database"=>"testing", "pool"=>5, "username"=>"ubuntu", "host"=>"127.0.0.1"}
-- create_table("documents", {:force=>:cascade})
rake aborted!
Mysql2::Error: Access denied for user ''#'localhost' to database 'testing'
Why it's not using my username and host? instead is showing '' # 'localhost'

I think, database.yml config for production environment, but you run it on development environment. You can use 'RAILS_ENV=development rake db:setup' or 'RAILS_ENV=test rake db:setup'
One more thing, database should be created by mysql user with root privileges

Related

rake assets:precompile RAILS_ENV=production Error

I want to deploy a spree app to heroku and in order to do that I need to precompile my assets locally
I did
heroku addons:create heroku-postgresql
then I added config/application.rb
config.assets.initialize_on_precompile = false
my database.yaml file is
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
host: localhost
database: anzels_development
username: anzels
password: 1234
test:
<<: *default
host: localhost
database: anzels_test
username: anzels
password: 1234
production:
adapter: postgresql
encoding: unicode
database: anzels_production
pool: 5
password:
and whenever I run
sumeet#sumi-pc:~/anzels$ rake assets:precompile RAILS_ENV=production
I get an error
rake aborted!
ActiveRecord::NoDatabaseError: FATAL: role "sumeet" does not exist
/home/sumeet/anzels/config/environment.rb:5:in `<top (required)>'
PG::ConnectionBad: FATAL: role "sumeet" does not exist
/home/sumeet/anzels/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => environment
(See full trace by running task with --trace)
config/enviormenr.rb
# Load the Rails application.
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
please help
In local computer you are trying to run
rake assets:precompile RAILS_ENV=production
but this requires your local compute to have config/database.yml with the config mentioned by #thieu-nguyen
so add the following in
username: $PRODUCTION_DB_USER
password: $PRODUCTION_DB_PASS
under production in config/database.yml
then add the environment for you local computer as
PRODUCTION_DB_USER=anzels
PRODUCTION_DB_PASS=1234
and for heroku as
PRODUCTION_DB_USER=user
PRODUCTION_DB_PASS="" (empty)
ANOTHER EASIER WAY IS
username: anzels
password: 1234
to production in config/database.yml **JUST BEFORE assests precompilation **
then run command
git checkout config/database.yml
JUST BEFORE GIT COMMIT command
the idea is to not to commit the username and password but temporarily edit it for assests precompilation purpose only
Please add username and password to production config in database.yml or you need to create new role for postgres with name "sumeet".
production:
adapter: postgresql
encoding: unicode
database: anzels_production
pool: 5
username: anzels
password: 1234

Problems with heroku rake db:reset

I have a problem, I pushed my app to github and I made the deploy with heroku, everything was ok, but when I run heroku run --app myapp rake db:reset (to populate the database on heroku) I found the following messages filled with errors (I show only the part that I think is important because the message was too long)
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
.
.
.
/app/vendor/bundle/ruby/2.0.0/bin/rake:23:in `load'
/app/vendor/bundle/ruby/2.0.0/bin/rake:23:in `<main>'
Couldn't drop d5q6ajst4p4d97
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
.
.
.
/app/vendor/bundle/ruby/2.0.0/bin/rake:23:in `load'
/app/vendor/bundle/ruby/2.0.0/bin/rake:23:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"utf8", "database"=> [DATABASE], "pool"=>5, "username"=> [USERNAME], "password"=> [PASSWORD], "port"=>5432, "host"=> [HOST]}
-- enable_extension("plpgsql")
-> 0.0370s
-- create_table("heros", {:force=>:cascade})
-> 0.0307s
-- initialize_schema_migrations_table()
-> 0.0579s
Note: I'm hiding the username, password, etc...
Everything works fine in the deployed app, but I find ugly to see so many mistakes when making a rake db: reset in heroku, so after seeing this I searched how to solve this problem and found some posts that preach the same solution, so I followed the recommendations and run the command heroku pg:reset DATABASE_URL --app myapp, it ended succesfully, but the same error message appears when I tried again to run rake db:reset, What could be happening?
This is my database.yml
development:
adapter: postgresql
encoding: utf8
database: project_development
pool: 5
username:
password:
test:
adapter: postgresql
encoding: utf8
database: project_test
pool: 5
username:
password:
production:
adapter: postgresql
encoding: utf8
database: project_production
pool: 5
username:
password:
Heroku, being a shared service is not allowing users the permissions to drop nor create databases - hence the error when running rake db:reset
Hence heroku pg:reset is present as a command to perform the reset of your database.
See https://devcenter.heroku.com/articles/rake
I got the same error I have fixed it by
heroku pg:reset DATABASE_URL
heroku rake db:migrate
As per you error
Also your database.yml point postgres user to connect database in heroku
it should not be postgres as a user in production mode.
so
Active Record 4.1+ Escape Valve
If you need need to connect to a different database than the
ENV['DATABASE_URL'] Rails 4.1+ supports a URL key in the database.yml
file that will take precedence.
production:
url: <%= ENV["SOME_OTHER_URL"] %>

Setting up PostgresQL on Heroku Rails 4

its been a while since i have used postgresql and deployed an app to Heroku, think I have made an error somewhere with my setup.
I have created my app on Heroku, there is a Hobby Dev database setup (I ran Heroku run rake db:setup) which set up my database, but I'm wondering in my database.yml file do i have an error
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
production:
<<: *default
database: my_app_production
username: my_app
password: <%= ENV['MY_APP_DATABASE_PASSWORD'] %>
When I run heroku run rake db:create i get
FATAL: permission denied for database
DETAIL: User does not have CONNECT privilege.
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"databaseName", "username"=>"userName", "password"=>"password", "port"=>5432, "host"=>"hostNamehere"}
I have set the database password using heroku config:set
What have i missed here?
Thanks
Heroku ignores your database.yml configuration, they generate one when you deploy your application and they also take care of database creation.
All you need to do is heroku run rake db:migrate and maybe a heroku run rake db:seed in case you need to seed your database.

bundle exec rake db:migrate hangs on Amazon EC2 and RDS

I have an EC2 instance (m3.medium) that runs my Rails application and connected to an RDS instance for the MySQL database.
When running 'bundle exec rake db:migrate', the process hangs indefinitely without outputting any error. Any ideas on how to debug this?
Edit the database.yml as:-
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: database_name
pool: 5
username: username
password: password
host: abcdef.amazonaws.com
Run the migration as:-
bundle exec rake RAILS_ENV=production db:migrate

ActiveRecord::AdapterNotSpecified: 'postgresql' database is not configured

I trying to figure out how fix the active record problem so I can push to heroku.
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
ActiveRecord::AdapterNotSpecified: 'postgresql' database is not configured. Available: ["development", "adapter", "encoding",
"database", "host", "pool", "username", "password", "production"]
development:
adapter: postgresql
encoding: unicode
database: sample_app_development
host: localhost
pool: 5
username: sample_app
password:
test:
adapter: postgresql
encoding: unicode
database: sample_app_test
host: localhost
pool: 5
username: sample_app
password:
production:
adapter: postgresql
encoding: unicode
database: sample_app_production
host: localhost
pool: 5
username: sample_app
password:
There is a space before development in the db config. I believe To fix it, simply remove space from the first line and follow the indentation.
I think you need to write this command on terminal to create you database:
rake db:create
Then run rake db:migrate to run your migrations
Remove spaces on 1st line " development:"
Make sure of the database name e.g. "sample_app_development" or just "sample_app"
gem install pg
rake db:create
rake db:migrate
First install postgresql
gem install pg
Then create project using following command:
rails new myapp -d postgresql

Resources