how to setup PostgreSQL username and password on digitalOcean - ruby-on-rails

Hello I am trying replace my app with the digital Ocean default App but when i try to run,
rails db:setup
i have this error
fe_sendauth: no password supplied
Couldn't create database for {"adapter"=>"postgresql",
"encoding"=>"unicode", "pool"=>5, "host"=>"localhost",
"username"=>"rails", "password"=>nil,
"database"=>"realestate_development"}
rails aborted!
PG::ConnectionBad: fe_sendauth: no password supplied
Here is my database.yml file
pool: 5
host: localhost
username: rails
password: <%= ENV['APP_DATABASE_PASSWORD'] %>
development:
<<: *default
database: realestate_development
test:
<<: *default
database: realestate_test
production:
<<: *default
database: realestate_production
username: rails
password: <%= ENV['APP_DATABASE_PASSWORD'] %>

If you follow the steps here, it will ask you for password for postgres user or you could create another user, "rails". Then you can update your database.yml with those credentials, especially the password.

The message is clear enough: fe_sendauth: no password supplied
Accessing to rails console in production and check out ENV['APP_DATABASE_PASSWORD'] value.

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

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.

rake db:create password authentication fails for user

While running the command rake db:create, the system throws postgresql authentication fails
FATAL: password authentication failed for user "postgres"
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "username"=>"postgres", "password"=>"password", "timeout"=>5000, "host"=>"localhost", "database"=>"rails-sample-guestbook-master_development"}
rake aborted!
database.yml file
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: supranimbus
password: 123456789
timeout: 5000
host: localhost
development:
<<: *default
database: rails-sample-guestbook-master_development
test:
<<: *default
database: rails-sample-guestbook-master_test
production:
<<: *default
database: rails-sample-guestbook-master_production
I guess the problem is setup password on postgresql if you don't remember the default password then go to pgAdmin click to open dashboard and then the left sidebar see the below image
Click the Properties... then will open a modal like see the below image
and click the Definition tab and see the Password field and set the new password, remember this password for postgres username.
That's is for password setup!
Now update your database.yml file like below
default: &default
adapter: postgresql
encoding: unicode
username: postgres
password: 123456 #=> which you set for postgres
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: project_development
Hope to help

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

Resources