Rails 每当 rake 中止时! PG::ConnectionBad:fe_sendauth:未提供密码
Rails Whenever rake aborted! PG::ConnectionBad: fe_sendauth: no password supplied
我正在使用 Whenever gem for Rails to 运行 rake 任务检查 Postgres 数据库的条目更新。 运行 通过 SSH 连接到服务器时 rake 文件工作正常,但对于 cron 作业它没有 运行 并显示错误消息。
rake aborted!
PG::ConnectionBad: fe_sendauth: no password supplied
我的设置如下:
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
在日志文件中输出
--- 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)
如果我更改 md5 和 peer to trust in pg_hba.conf 一切都会正常工作,但据我所知,这对生产环境中的安全性不利。
转到 database.yml 文件,替换:
host: localhost with host: ' '
或检查一次此链接:
PG::ConnectionBad: fe_sendauth: no password supplied
Rails + Postgres fe_sendauth: no password supplied
快乐编码..!!!
经过更多的研究,我想我已经找到了我遇到这个问题的原因。 Cron 作业具有来自操作系统的有限环境变量。这意味着用户名、密码、unix 用户等不可用,因此返回错误 fe_sendauth: no password supplied
。不幸的是,我找不到适合我的情况的解决方案,但我从使用 Whenever 更改为 Crono,它在几乎相同的代码上工作得很好。
如果有人想提出可能对原始问题有用的建议,请成为我的客人。如果可能的话,我会尝试一下,以防万一。
对我来说,解决方案是将数据库密码定义为环境变量。例如。在 /etc/environment 文件中。
我正在使用 Whenever gem for Rails to 运行 rake 任务检查 Postgres 数据库的条目更新。 运行 通过 SSH 连接到服务器时 rake 文件工作正常,但对于 cron 作业它没有 运行 并显示错误消息。
rake aborted!
PG::ConnectionBad: fe_sendauth: no password supplied
我的设置如下:
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
在日志文件中输出
--- 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)
如果我更改 md5 和 peer to trust in pg_hba.conf 一切都会正常工作,但据我所知,这对生产环境中的安全性不利。
转到 database.yml 文件,替换:
host: localhost with host: ' '
或检查一次此链接:
PG::ConnectionBad: fe_sendauth: no password supplied
Rails + Postgres fe_sendauth: no password supplied
快乐编码..!!!
经过更多的研究,我想我已经找到了我遇到这个问题的原因。 Cron 作业具有来自操作系统的有限环境变量。这意味着用户名、密码、unix 用户等不可用,因此返回错误 fe_sendauth: no password supplied
。不幸的是,我找不到适合我的情况的解决方案,但我从使用 Whenever 更改为 Crono,它在几乎相同的代码上工作得很好。
如果有人想提出可能对原始问题有用的建议,请成为我的客人。如果可能的话,我会尝试一下,以防万一。
对我来说,解决方案是将数据库密码定义为环境变量。例如。在 /etc/environment 文件中。