rake not seeing Rails.application.secrets - ruby-on-rails

In short:
seems that rake does not have access to Rails.application.secrets in config/database.yml file
what is the purpose of config/secrets.yml then?
In long:
When I run
RAILS_ENV=production rake db:migrate
I get the error Mysql2::Error: Access denied for user 'root'#'localhost' (using password: NO), though I specified appropriate values in config/database.yml and the user connecting should not be 'root'. This is an excerpt from respective config files:
# config/database.yml
production:
<<: *default
adapter: mysql2
host: localhost
database: <%= Rails.application.secrets[:database][:name] %>
username: <%= Rails.application.secrets[:database][:username] %>
password: <%= Rails.application.secrets[:database][:password] %>
# config/secrets.yml
production:
secret_key_base: very-long-blah-blah-blah
database:
name: app_db_name
username: app_db_user
password: app_db_password
Seems that rake has no access to Rails.application.secrets. Running migration succeeds when I explicitly put necessary values in database.yml, for example, as follows:
production:
<<: *default
adapter: mysql2
host: localhost
database: <%= Rails.application.secrets[:database][:name] || 'app_db_name' %>
username: <%= Rails.application.secrets[:database][:username] || 'app_db_user' %>
password: <%= Rails.application.secrets[:database][:password] || 'app_db_password' %>
The above proves that Rails.application.secrets[:database][:name] resolves to nothing.
How to have access to Rails.application.secrets in rake? Would this be the correct solution?
I know that I can use ENV[VARNAME] to fill in secret sections of config/database.yml. But what the the purpose of config/secrets.yml file then?
Moreover, I am using Passenger, which means that variables in .bashrc will probably not be accessible to the web server (I had this issue with secret_key_base). Therefore I try to avoid using environment variable. Just do not want to have all my secrets spilled all over the server.
rails-4.2.2, Ubuntu LTS 14.04

I haven't seen such nested content for the secrets.yml like you have, also the release notes doesn't have such kind. You should be just fine with the below code
# config/secrets.yml
production:
secret_key_base: very-long-blah-blah-blah
name: app_db_name
username: app_db_user
password: app_db_password
And in the database.yml
# config/database.yml
production:
<<: *default
adapter: mysql2
host: localhost
database: <%= Rails.application.secrets.name %>
username: <%= Rails.application.secrets.username %>
password: <%= Rails.application.secrets.password %>

Related

Can't run rails db:create for postgres RDS on AWS

I created an postgres RDS instance,** which I can connect to from my EC2 instance and my local shell**.
But for some reason when I run rails db:create I get the following error:
FATAL: password authentication failed for user "my_user"
Any pointers?
I can connect to the RDS instance from EC2 instance and local machine.
I expect rails to be able to run migrations as well, but it throws an error.
db.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: use
password: gresgres
development:
<<: *default
database: myapp_dev
test:
<<: *default
database: myapp_test
production:
<<: *default
database: <%= ENV['PG_RDS_DBNAME'] %>
username: <%= ENV['PG_RDS_USERNAME'] %>
password: <%= ENV['PG_RDS_PASSWORD'] %>
host: <%= ENV['PG_RDS_ENDPOINT'] %>
port: <%= ENV['PG_RDS_PORT'] %>

Rails, datatabse creation issue

I am trying to create database using default user 'postgres'
but while executing rails db:create not seleting role name postgres, it's selecting my systme name 'mysystem'.
below is my database.yml file code
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: user_development
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
test:
<<: *default
database: user_test
production:
<<: *default
host: <%= ENV['DATABASE_HOST_URL'] %>
database: user_production
username: <%= ENV['USER_DATABASE_USERNAME'] %>
password: <%= ENV['USER_DATABASE_PASSWORD'] %>
Error while executing rails db:create
rails aborted!
ActiveRecord::ConnectionNotEstablished: FATAL: role "mysystem" does not exist
Caused by:
PG::ConnectionBad: FATAL: role "mysystem" does not exist
There is some helpful information here:
https://guides.rubyonrails.org/v4.2.7/configuring.html#configuring-a-database
sections 3.12 and 3.13
I think your issue might be the host: setting. This should either
be /tmp for a unix domain socket, or an ip address / hostname, if used.
Settings I used in testing:
postgresql 12.3 debian unix.
Using this pg_hba.conf:
local all all ident
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
host all all 192.168.50.0/24 md5
You might want a hostssl entry for data being sent between machines.
# using the same "user" database name you have.
rails new user -d postgresql
#setting the password for testing.
psql
alter role user1 password 'testpass';
cd user/config
emacs database.yml
#example using ident authentication - being logged in as the same unix user.
production:
<<: *default
host: /tmp
database: user_production
username: user1
password: nil
# using a host ip address
production:
<<: *default
host: 192.168.50.6
database: user_production
username: user1
password: testpass
# using a host ip address with env vars
#export USER_DATABASE_USERNAME="user1"
#export USER_DATABASE_PASSWORD="testpass"
production:
<<: *default
host: 192.168.50.6
database: user_production
username: <%= ENV['USER_DATABASE_USERNAME'] %>
password: <%= ENV['USER_DATABASE_PASSWORD'] %>
# hardcoding everything in the url for illustration:
production:
<<: *default
url: postgres://user1:testpass#192.168.50.6/user_production
# best practice according to doc. DATABASE_URL has precedence over database.yaml when set
# we are just indicating we know that here.
# export DATABASE_URL="postgres://user1:testpass#192.168.50.6/user_production"
production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
user1#debian10 /home/user1/rails/user > RAILS_ENV=production bin/rails db:create
Created database 'user_production'
user1#debian10 /home/user1/rails/user > RAILS_ENV=production bin/rails db:drop
Dropped database 'user_production'
#Seeing what is being used:
RAILS_ENV=production bin/rails runner 'p ActiveRecord::Base.configurations'
#<ActiveRecord::DatabaseConfigurations:0x0000564e40093fa0 #configurations=[#<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e40092f60 #env_name="default", #name="primary", #configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5}>, #<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e400928f8 #env_name="development", #name="primary", #configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"user_development"}>, #<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e400921a0 #env_name="test", #name="primary", #configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"user_test"}>, #<ActiveRecord::DatabaseConfigurations::UrlConfig:0x0000564e40091b60 #env_name="production", #name="primary", #configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :username=>"user1", :password=>"testpass", :database=>"user_production", :host=>"192.168.50.6"}, #url="postgres://user1:testpass#192.168.50.6/user_production">]>

receiving "PG::ConnectionBad fe_sendauth: no password supplied" error when storing passwords in ENV variables in rails

I recently switched db's from sqlite3 to PG. My username and password for pg were hard-coded, and although it was working, this was not safe practice. SO I stored my password in an ENV variable in a .yml file, and I reference that variable in my database.yml file, but when I run the server, it gives me the error "PG::ConnectionBad
fe_sendauth: no password supplied"
the following is my pg_keys.yml file
PG_PASSWORD: "***********"
And the following is my database.yml file:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: stockapp_development
username: postgres
password: <%= ENV['PG_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:
<<: *default
database: stockapp_test
host: localhost
username: postgres
password: <%= ENV['PG_PASSWORD'] %>
production:
<<: *default
database: stockapp_production
username: stockapp
password: <%= ENV['STOCKAPP_DATABASE_PASSWORD'] %>
Why is it saying that no password is supplied? Does my database.yml not see the password in the other file? Do I need to export the password?
It is because ENV variables come from https://github.com/bkeepers/dotenv.
Install this gem by adding it to your Gemfile, then run bundle and set the variable PG_PASSWORD=*********** in .env in your root. Then it should work fine.

Rails Postgres config host mistaken as database name

have been pulling my hair due this issue :
When I run my rails application on my mac it seems the host is mistaken as database name and it only happens on development environment.
This is my database.yml :
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: <%= ENV['DATABASE_USERNAME'] || 'admin' %>
password: <%= ENV['DATABASE_PASSWORD'] || 'password'%>
host: <%= ENV['DATABASE_URL'] || 'localhost'%>
development:
<<: *default
database: cid_dev
test:
<<: *default
database: cid_test
production:
<<: *default
database: cid_api
Then when I run :
$ bundle exec rake db:create
It returns me this :
Database 'localhost' already exists
anyone has any idea what happen in my local environment ?
FYI, I tried rbenv and rvm both has same issue.
Thank you.
I found the answer myself.
Rails using DATABASE_URL environment variable as a place to put connection string. So when I put database host inside env variable DATABASE_URL it will use it as default
https://github.com/rails/rails/blob/fb764ba63e53b728873075a0d207b993409798a2/railties/lib/rails/application/configuration.rb#L88-L102
So to fix it what I need is rename DATABASE_URL to DATABASE_HOST which is the correct one anyway. Thank you

rails SQlite 3: Cant open the connection

I have existing rails project with PostgreSQL DB. When I am running the same in my local machine I am receiving SQLite exception.
But my database.yml configured with postgres adapter only.
Here is the exception:
SQLite3::CantOpenException: unable to open database file
database.yml
postgres: &postgres
adapter: postgresql
encoding: unicode
host: localhost
pool: 5
username: postgres
password: postgres
min_messages: warning
development:
<<: *postgres
database: dev_development
username: <%= ENV['PG_USER'] || 'postgres' %>
password: <%= ENV['PG_PASSWORD'] || 'postgres' %>
test:
<<: *postgres
database: dev_test
username: <%= ENV['PG_USER'] || 'postgres' %>
password: <%= ENV['PG_PASSWORD'] %>
Perhaps you have an environment variable set (DATABASE_URL) that is overriding your database.yml file? Look at this for more info: http://edgeguides.rubyonrails.org/configuring.html#configuring-a-database
If that's not it, is it possible you recently changed your database.yml file and haven't restarted the server?

Resources