I'm trying to run Capistrano for a rails app with Postgres on Ubuntu 14, I ran into a password error during rake db:migrate-
DEBUG [2823f146] Command: cd /home/ben/apps/mll/releases/20160414014303 && ( export RBENV_ROOT="$HOME/.rbenv" RBENV_VERSION="2.0.0-p645" RAILS_ENV="production" ; $HOME/.rbenv/bin/rbenv exec bundle exec rake db:migrate )
DEBUG [2823f146] rake aborted!
PG::ConnectionBad: fe_sendauth: no password supplied
I tried every solution to similar posts but no luck. For kicks I also tried running just that command in the remote app dir and got the following:
PG::ConnectionBad: FATAL: password authentication failed for user "mll"
FATAL: password authentication failed for user "mll"
This is interesting because it's using my database name as my username. See database.yml below, so for the hell of it I added a mll role, and lo and behold it worked when just running rake db:migrate. I tried running Capistrano again though with this new role, and still no luck.
Is it a reasonable guess that the username is not being accessed/stored properly? Any way for me to test that? I manually ALTER ROLE ben WITH PASSWORD 'mypw'; for both my ben and mll roles, and nothing.
My database.yml:
defaults: &default
adapter: sqlite3
encoding: utf8
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/development.sqlite3_test
production:
<<: *default
host: localhost
adapter: postgresql
encoding: utf8
database: mll
pool: 5
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
\du:
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
ben | Superuser, Create role, Create DB | {}
mll | Superuser, Create role, Create DB | {}
postgres | Superuser, Create role, Create DB, Replication | {}
I read changing md5 to trust helped some people, I tried that, but I'm unsure how to restart, all commands I've seen haven't worked for me.
pg_hba.conf:
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
You should use "trust" under method for localhost in pg_hba.conf. Note that this means all connections from localhost will be able to login as any user, which is probably fine as long as you're using this for development.
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
After changing your pg_hba.conf you can restart postgres with pg_ctl reload
The best practice to resolve the issue is -
Add the below gem in your Gemfile under development group and bundle install.
gem 'capistrano-postgresql'
Add the below line in Capfile
require 'capistrano/postgresql'
Add below lines in config/deploy.rb or in config/deploy/*.rb
set :pg_password, ENV['DATABASE_PASSWORD']
set :pg_ask_for_password, true
Related
Changed the DB setup of Rails (config/database.yml) from:
development:
<<: *default
database: my_app_development
to:
development:
<<: *default
url: postgres://postgres#localhost:5432/my_app_development
Started getting the following error:
rails db:migrate:reset
=> rails aborted!
=> PG::ConnectionBad: fe_sendauth: no password supplied
OS: Ubuntu 18.04 LTS
The issue was that pg_hba.conf has different rules for this scenario. The following line might look like it enables all connections
# TYPE DATABASE USER ADDRESS METHOD
local all all peer (or trust)
But there can be other offending ones, in my case:
# TYPE DATABASE USER ADDRESS METHOD
host all all 127.0.0.1/32 md5
Changing this line from md5 to trust resolved the issue. Keep in mind that this is not safe.
I am using the Whenever gem for Rails to run a rake task checking the Postgres database for updates to entries. Running the rake file while SSH into the server works fine but with the cron job it does not run with the error message.
rake aborted!
PG::ConnectionBad: fe_sendauth: no password supplied
My setup is as follows:
database.yml
default: &default
adapter: postgresql
host: localhost
pool: 5
encoding: utf8
timeout: 5000
username: username
password: password
development:
<<: *default
database: development
password: password
test:
<<: *default
database: test
production:
<<: *default
database: production
password: password
script.rake
namespace :user do
desc "TODO"
task update_subscription: :environment do
puts "--- Starting user subscription check at #{DateTime.now} ---"
User.find_each do | user |
if user.refresh_date.past?
user.refresh_date = Date.today + 1.month
puts "User #{user.id} subscription updated"
end
end
end
end
schedule.rb
every 1.day, :at => '00:01 am' do
rake "user:update_subscription"
end
crontab -l
1 0 * * * /bin/bash -l -c 'cd /home/app/app && RAILS_ENV=production bundle exec rake user:update_subscription --silent >> log/cron_log.log 2>&1'
pg_hba.conf
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres peer
#host replication postgres 127.0.0.1/32 md5
#host replication postgres ::1/128 md5
output in log file
--- Starting user subscription check at 2018-11-23T10:20:05+00:00 ---
rake aborted!
PG::ConnectionBad: fe_sendauth: no password supplied
/home/app/app/lib/tasks/script.rake:5:in `block (2
levels) in <top (required)>'
/home/app/.rbenv/versions/2.3.8/bin/bundle:22:in `load'
/home/app/.rbenv/versions/2.3.8/bin/bundle:22:in `<main>'
Tasks: TOP => user:update_subscription
(See full trace by running task with --trace)
If I change md5 and peer to trust in pg_hba.conf everything will work but as far as I know this is not good for security in a production environment.
Go to database.yml file, replace:
host: localhost with host: ' '
or check once this links :
PG::ConnectionBad: fe_sendauth: no password supplied
Rails + Postgres fe_sendauth: no password supplied
Happy Coding ..!!!
After a lot more research I think I have found out why I was getting this problem. Cron jobs have limited environmental variables from the operating system. This meant the username, password, unix user etc. where not available and thus the error fe_sendauth: no password supplied was returned. Unfortunately I could not find a solution to this that worked in my situation but I changed from using Whenever to Crono which works great on just about the same code.
If anyone wants to suggest something that may work for the original question please be my guest. Where possible I will try them out incase one works.
To me, the solution was define the database password as a environment variable. Eg. in the /etc/environment file.
I'm having problems assessing a postgres database from straight ruby.
I've created a Postgres database using Rails
>rails new www --database=postgresql
using Rails 4.2.5 and Postgres is 9.4
It produces the following config/database.yml file.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: www_development
test:
<<: *default
database: www_test
production:
<<: *default
database: www_production
username: www
password: <%= ENV['WWW_DATABASE_PASSWORD'] %>
I can run rails server, db:drop, db:create and db:migrate fine.
I can also access the database fine with psql
>psql www_development
But when I run the following app.rb from a non Rails project directory, I get a fe_sendauth: no password supplied (PG::ConnectionBad) error message.
It's clearly not a Rails issue. I've either missed something in my ruby or Postgres need a tweek to handle some difference between Rails and pure Ruby [that I'm not aware off]. I've also included Postgres' pg_hba.conf file.
At wits end trying to figure this one out. Any help would be much appreciated.
app.rb
require 'yaml'
require 'active_record'
require 'pg'
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'localhost',
database: 'www_development',
)
/etc/postgresql/9.4/main/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
You don't specify neither username, no password in your ActiveRecord::Base.establish_connection(. I assume you want to use some SUPERUSER without password for www_development database - right?
as per documentation peer auth does
Obtain the client's operating system user name from the operating
system and check if it matches the requested database user name.
That is why if you can psql without password, you should be able run app.rb with same OS user and environment without password. If you can't, then app.rb tries to connect with different username or so...
Options:
put username: postgres to ActiveRecord::Base.establish_connection(
change local all all peer to local all all trust
With ENV variable as password it's very likely that the variable itself is not present. Confirm the presence with printenv. You need to relogin/reboot for the variable to be accessible after you've included it in /etc/environment file for example. If this works, it's probably better than changing pg_hba.conf.
development:
<<: *default
database: postgres
username: postgres
password: postgres
# must specify the right DB, superuser and superuser Password as per postgreSQL setup
This worked for me, the only changes I needed to make. (ok fine i re-installed pgsql with 12.1)
I had this same issue when setting up a Rails 6 application.
The issue was that when start the rails server using rails server, and try to view the application from a browser, I get the error below:
ActiveRecord::ConnectionNotEstablished
fe_sendauth: no password supplied
Here's how I solved it:
The issue was caused by me not specifying/supplying the database password to be used by the application, so when the application tries to connect to the database specified in the config/database.yml it runs into that error.
I solved it by specifying the database password for the application in the config/database.yml:
# The password associated with the postgres role (username).
password: my-password
Also, ensure that you've created the database for the application if you've not created it already using:
rails db:create
Afterwhich I restarted the rails server:
rails server
This time when I tried viewing the application from the browser, it worked fine.
That's all.
I hope this helps
I was trying to use peer authentication which shouldn't ask for the password.
For me the issue was that I was specifying localhost in the database.yml so that was forcing the database driver to try to make a different type of connection (e.g., tcp, named pipe, etc)
Removing the "host: 'localhost'" line from my config solved the problem for me per this other answer: ActiveRecord connection to a localhost postgresql database in trust mode
Note that in my case I'm using the 'peer' method and not the 'trust' method but the solution is the same
I have just created a new RoR project on a Godaddy VPS running Ubuntu. The app is set up to run Postgres, which is installed and running.
Wwhen I do the INITIAL rake, I get:
server$ bin/rake db:create db:migrate
FATAL: role "root" does not exist
Run `$ bin/rake db:create db:migrate` to create your database
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:898:in `rescue in connect'
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `connect'
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:568:in `initialize'
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `new'
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:435:in `new_connection'
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:445:in `checkout_new_connection'
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/........
Here is my database yml
development:
adapter: postgresql
encoding: utf8
database: project_development
pool: 5
username: philip
password: ###
test: &TEST
adapter: postgresql
encoding: utf8
database: project_test
pool: 5
username: philip
password: ### - not real
production:
adapter: postgresql
encoding: utf8
database: project_production
pool: 5
username: philip
password: ###
I have tried root and philip.
And I have created roles and users in PG for both.
In Ubuntu, PostgreSQL by default is configured to allow local connections over Unix socket only to authenticate with the same username as the connecting Unix user.
You are running your app as root in your OS. And PostgreSQL forces you to use root as a database user name. If you want it to use a different database account, you need to run your app as a different user.
Apparently, your PostgreSQL server doesn't have a root account.
Ubuntu wiki page about PostgreSQL setup
A dirty fix could be authentication over TCP/IP: set a host to 127.0.0.1 in your database.yml, and these restrictions won't have any effect.
Another way to circumvent it is to edit pg_hba.conf to not require "the same user" for local connections.
You need to create the roles and database by hand:
SSH into the vps and then ...
Switch into the default user:
sudo su – postgres
Once logged in as this user, you can create the new role:
createuser
Enter name of role to add: philip
Shall the new role be a superuser? (y/n) y
To outfit your user with a password, you can add the words –pwprompt to the createuser command:
createuser --pwprompt
Source: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-12-04
I started over by deleting all roles in PG. I recreated what I needed and it is all good
I'm setting up Postgresql with Rails on Ubuntu and I am getting the error: Rake Aborted! fe_sendauth: no password supplied when running rake commands.
I have the following in my database.yml file:
development:
adapter: postgresql
encoding: unicode
database: test_database
pool: 5
username: postgres
passsword: <password>
I have changed the method of connection in the pg_hba.conf file and restarted the postgresql server however the same error occurs.
# TYPE DATABASE USER ADDRESS METHOD
local all all md5
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
I know I can change the method to trust and run the rake commands but I need to use md5 since I want to use thinking sphinx and it doesn't support trust.
I can connect to the database through pgAdmin and in the console with my username and password: psql -U postgres -d test_database.
Am I missing something?
[EDIT]
Turns out I cannot speel. I changed 'Passsword' to 'Password' in my database.yml and it works now...
Fixed typo, when wanting to use user/password authentication in rails on ubuntu you must change the method access type in the pg_hba.conf of your postgresql installation directory to md5