db:migrate (PostgreSQL) 在使用 Capistrano 进行 Rails 部署时失败

db:migrate (PostgreSQL) fails on Rails deployment with Capistrano

我对部署 Rails 完全是菜鸟,对 PostgreSQL 更是新手。我是 运行 CentOS 6.6 服务器,我的部署任务一直失败并出现以下 db:migrate 错误:

cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deploy@xxx.xxx.xxx.xxx: rake exit status: 1
rake stdout: /var/www/rally_app/shared/bundle/ruby/2.2.0/gems/fog-1.23.0/lib/fog/rackspace/mock_data.rb:42: warning: duplicated key at line 80 ignored: "name"
rake aborted!
PG::ConnectionBad: FATAL:  no pg_hba.conf entry for host "::1", user "me", database "pg_sampleapp", SSL off
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:436:in `new_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:446:in `checkout_new_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:565:in `retrieve_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:87:in `connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:913:in `initialize'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:820:in `new'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:820:in `up'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:798:in `migrate'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
rake stderr: Nothing written
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/sshkit-1.7.1/lib/sshkit/runners/parallel.rb:16:in `rescue in block (2 levels) in execute'
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/sshkit-1.7.1/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
SSHKit::Command::Failed: rake exit status: 1
rake stdout: /var/www/rally_app/shared/bundle/ruby/2.2.0/gems/fog-1.23.0/lib/fog/rackspace/mock_data.rb:42: warning: duplicated key at line 80 ignored: "name"
rake aborted!
PG::ConnectionBad: FATAL:  no pg_hba.conf entry for host "::1", user "me", database "pg_sampleapp", SSL off
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:436:in `new_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:446:in `checkout_new_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:565:in `retrieve_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:87:in `connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:913:in `initialize'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:820:in `new'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:820:in `up'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:798:in `migrate'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
rake stderr: Nothing written
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/sshkit-1.7.1/lib/sshkit/command.rb:95:in `exit_status='
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/sshkit-1.7.1/lib/sshkit/backends/netssh.rb:179:in `block in _execute'
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/sshkit-1.7.1/lib/sshkit/backends/netssh.rb:133:in `tap'
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/sshkit-1.7.1/lib/sshkit/backends/netssh.rb:133:in `_execute'
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/sshkit-1.7.1/lib/sshkit/backends/netssh.rb:66:in `execute'
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/capistrano-rails-1.1.3/lib/capistrano/tasks/migrations.rake:16:in `block (5 levels) in <top (required)>'
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/sshkit-1.7.1/lib/sshkit/backends/abstract.rb:85:in `with'
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/capistrano-rails-1.1.3/lib/capistrano/tasks/migrations.rake:15:in `block (4 levels) in <top (required)>'
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/sshkit-1.7.1/lib/sshkit/backends/abstract.rb:77:in `within'
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/capistrano-rails-1.1.3/lib/capistrano/tasks/migrations.rake:14:in `block (3 levels) in <top (required)>'
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/sshkit-1.7.1/lib/sshkit/backends/netssh.rb:54:in `instance_exec'
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/sshkit-1.7.1/lib/sshkit/backends/netssh.rb:54:in `run'
/home/me/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/sshkit-1.7.1/lib/sshkit/runners/parallel.rb:13:in `block (2 levels) in execute'
Tasks: TOP => deploy:migrate
The deploy has failed with an error: Exception while executing as deploy@xxx.xxx.xxx.xxx: rake exit status: 1
rake stdout: /var/www/rally_app/shared/bundle/ruby/2.2.0/gems/fog-1.23.0/lib/fog/rackspace/mock_data.rb:42: warning: duplicated key at line 80 ignored: "name"
rake aborted!
PG::ConnectionBad: FATAL:  no pg_hba.conf entry for host "::1", user "me", database "pg_sampleapp", SSL off
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:436:in `new_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:446:in `checkout_new_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:565:in `retrieve_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:87:in `connection'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:913:in `initialize'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:820:in `new'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:820:in `up'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:798:in `migrate'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/var/www/rally_app/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
rake stderr: Nothing written
** Invoke deploy:failed (first_time)
** Execute deploy:failed
[me@localhost rally_app]$ 

感谢您的帮助。

亚历克西斯

您的用户 "me" 没有权限连接到您的数据库。权限在 Postgres 的 pg_hba.conf 中定义。我没有使用过 CentOS,但据我所知,您应该可以在 /var/lib/pgsql/<your Postgres version>/data.

找到该文件

在文本编辑器中打开配置文件(您可能需要 运行 使用 sudo 才能获得编辑权限),然后滚动到末尾。

添加这一行:

host    pg_sampleapp             me             ::1/128                 password

在提供正确的密码后,me 授予您的用户 pg_sampleapp 访问数据库的权限——但仅限于本地主机,这意味着从您的服务器本身内部,而不是从外部连接到它时.

您可以通过将 pg_sampleappme 替换为 all 来扩大权限范围,这意味着本地所有 Postgres 用户都可以连接到所有数据库。如果你想要额外的方便又不担心安全问题,你也可以把password替换成trust,意思是你可以直接连接而不需要提供任何密码:

host    all             all             ::1/128                 trust

添加权限后,您需要通过service postgresql-9.4 restart重新加载Postgres(如果您安装了Postgres 9.4,否则相应地更改版本)。

您可以尝试使用 psql pg_sampleapp me 登录数据库来测试它是否正常工作。如果是,使用 CTRL+D 退出 psql。

有关详细信息,请参阅 Postgres documentation on pg_hba.conf — 它非常易读且非常详细。