rails credentials not fetching - ruby-on-rails

I created rails config/master.key and config/credentials.yml.enc via rails credentials:edit command
on rails console i'm able to fetch values but when i run rake db:migrate, i get this error:
invalid port number: "Rails.application.credentials.development[:port]"
Couldn't create 'Rails.application.credentials.development[:database]' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: invalid port number: "Rails.application.credentials.development[:port]"
/home/ec2-user/.rvm/gems/ruby-2.6.1/gems/pg-1.1.4/lib/pg.rb:56:in `initialize'
/home/ec2-user/.rvm/gems/ruby-2.6.1/gems/pg-1.1.4/lib/pg.rb:56:in `new'
/home/ec2-user/.rvm/gems/ruby-2.6.1/gems/pg-1.1.4/lib/pg.rb:56:in `connect'
/home/ec2-user/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <main>'
Tasks: TOP => db:create
(See full trace by running task with --trace)
#config/environments/development.rb
config.require_master_key = true
#config/credentials.yml.enc
development:
database: xxxxxxxx
username: yyyyyyyy
password: zzzzzzzz
host: aaaaaaa.com
port: 1234
# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base: 1234567890
#config/database.yml
default: &default
adapter: postgresql
encoding: utf8
database: Rails.application.credentials.development[:database]
username: Rails.application.credentials.development[:username]
password: Rails.application.credentials.development[:password]
host: Rails.application.credentials.development[:host]
port: Rails.application.credentials.development[:port]
development:
<<: *default

It looks that problem in syntax
You need to use interpolation like this:
database: <%= Rails.application.credentials.development[:database] %>
username: <%= Rails.application.credentials.development[:username] %>
password: <%= Rails.application.credentials.development[:password] %>
host: <%= Rails.application.credentials.development[:host] %>
port: <%=Rails.application.credentials.development[:port] %>

Related

Unable to create Database in Rails using rake db:create

I created a new rails project and did few configuration changes in database.yml.
Then had to create the database using the command rake db:create inorder to continue developing the application but I get the error below.
Els-MacBook-Pro:eshop el$ rake db:create
warning ../../package.json: No license field
FATAL: role "eshop" does not exist
Couldn't create 'eshop_development' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: FATAL: role "eshop" does not exist
Tasks: TOP => db:create
(See full trace by running task with --trace)
Els-MacBook-Pro:eshop el$
I will also post the contents of my database.yml below:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
host: localhost
database: eshop_development
username: eshop
password: eshop
test:
<<: *default
host: localhost
database: eshop_test
username: eshop
password: eshop
production:
<<: *default
database: eshop_production
username: eshop
password: <%= ENV['ESHOP_DATABASE_PASSWORD'] %>
I will be grateful if someone can help me figure out why I cannot create a db. I have read all types of solutions and none seems to be working. Thanks.
In your database.yaml you need to add a port: 5432, and then Try rails db:create, as rake has been deprecated.
create a role in your postrgesql database named 'eshop'

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">]>

bundle exec rake db:setup, Rake tasks not supported by 'username' adapter

When I try to run bundle exec rake db:setup ,It gives me this error
and when I change the adapter to my username I got the same error
PG::InsufficientPrivilege: ERROR: permission denied to create database
: CREATE DATABASE "freelance_camp_documents_development" ENCODING = 'unicode'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"freelance_camp_documents_development"}
rake aborted!
ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: permission denied to create database
: CREATE DATABASE "freelance_camp_documents_development" ENCODING = 'unicode'
/usr/local/share/gems/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec'
/usr/local/share/gems/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `block in execute'
/usr/local/share/gems/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:566:in `block in log'
/usr/local/share/gems/gems/activesupport-
I can't put the whole error here
and here is the database.yml code
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: freelance_camp_documents_development
test:
<<: *default
database: freelance_camp_documents_test
production:
<<: *default
database: freelance_camp_documents_production
username: freelance_camp_documents
password: <%= ENV['FREELANCE_CAMP_DOCUMENTS_DATABASE_PASSWORD'] %>
PG::InsufficientPrivilege: ERROR: permission denied to create database
The problem here is you don't have sufficient privileges to user freelance_camp_documents on Postgres.
I will suggest to change permissions for user freelance_camp_documents from postgres or just change username to postgres
production:
<<: *default
database: freelance_camp_documents_production
username: postgres
password: <%= ENV['FREELANCE_CAMP_DOCUMENTS_DATABASE_PASSWORD'] %>
postgres user has permissions by default so you don't need to change anything

rake aborted:PG::ConnectionBad: fe_sendauth: no password supplied

I'm on Cloud9 Ubuntu Template and I installed postgres. I'm getting an error when I try to do a "rake db:migrate".
rake aborted:PG::ConnectionBad: fe_sendauth: no password supplied
Related settings in my database.yml file
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: <%= ENV['USERNAME'] %>
password: <%= ENV['PASSWORD'] %>
host: <%= ENV['IP'] %>
development:
<<: *default
database: app_development
It seems like Cloud9 issue. I refer https://community.c9.io/t/fe-sendauth-no-password-supplied-error-after-setting-up-postgrsql-on-rails/2206/2
Run below commands on your c9 terminal.
$ source ~/.profile
$ rake db:create
$ rake db:migrate
It works for me.

rake not seeing Rails.application.secrets

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 %>

Resources