PG :: ConnectionBad:无法连接到服务器:连接被拒绝(Ubuntu 16.04,Rails 5,Capistrano)

时间:2017-11-07 09:38:14

标签: ruby-on-rails postgresql capistrano ubuntu-16.04 digital-ocean

将Rails 5应用程序部署到Ubunbtu 16.04.3(Digitalocean VPS)时,运行cap production deploy --trace时出现以下错误:

终端

$HOME/.rbenv/bin/rbenv exec bundle exec rake db:migrate
rake aborted!

PG::ConnectionBad: could not connect to server: Connection refused
  Is the server running on host "138.68.6.26" and accepting
  TCP/IP connections on port 5432?

/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/pg-0.21.0/lib/pg.rb:56:in `initialize'
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/pg-0.21.0/lib/pg.rb:56:in `new'
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/pg-0.21.0/lib/pg.rb:56:in `connect'
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:695:in...
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:220:in…
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in …
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/activerecord-
...
/home/deploy/.rbenv/versions/2.4.1/bin/bundle:22:in `load'
/home/deploy/.rbenv/versions/2.4.1/bin/bundle:22:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
cap aborted!

的Gemfile

group :development do
  gem 'capistrano', '~> 3.7', '>= 3.7.1'
  gem 'capistrano-rails', '~> 1.2'
  gem 'capistrano-passenger', '~> 0.2.0'
  gem 'capistrano-rbenv', '~> 2.1'
  gem 'capistrano-bundler'
end

Capfile

require 'capistrano/rbenv'
set :rbenv_type, :user
set :rbenv_ruby, '2.4.1'

require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/passenger'

配置/ deploy.rb

set :application, "my-app"
set :repo_url, "git@github.com:username/my-app.git"

set :deploy_to, '/home/deploy/my-app'

append :linked_files, "config/database.yml", "config/secrets.yml"
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "vendor/bundle", "public/system", "public/uploads"

配置/部署/ production.rb

server '138.68.6.26', user: 'deploy', roles: %w{app db web}

在服务器上,我已经创建了符号链接文件/home/deploy/my-app/shared/config/database.yml/home/deploy/my-app/shared/config/secrets.yml,并将其从我的回购中删除。我也git也忽略了它们。

的database.yml

production:
  adapter: postgresql
  host: 138.68.6.26
  database: my-app
  username: deploy
  password: password
  encoding: unicode
  pool: 5

secrets.yml

production:
  secret_key_base: 9b865db3b261f66576ef6b2a...

我还设置了一个名为postgres的Ubuntu系统用户,并创建了一个名为deploy的数据库用户。我按照database.yml文件中的指示正确命名了数据库。我搜遍了整个地方,无法让它发挥作用。大多数答案都是在本地进行调试。这是为了部署。

3 个答案:

答案 0 :(得分:0)

尝试通过psql连接到此服务器,如果不能,问题出在postgresql服务器配置中。 (从应用程序的服务器执行此操作)

  

psql -h 138.68.6.26 -U部署my-app

答案 1 :(得分:0)

您的Postgres服务器配置为仅侦听localhost接口,因此无法从外部获取。

要配置Postgres以允许外部TCP连接,请进行以下更改:

1)在/etc/postgresql/<>/main/postgresql.conf中将listen_addresses设置为*

2)在/etc/postgresql/<>/main/pg_hba.conf中设置访问权限以允许来自外部IP的TCP连接

请确保您设置了防火墙,例如UFW,以防止任意主机连接到您的数据库。

答案 2 :(得分:0)

事实证明,我在database.yml文件中输入了错误的IP地址。我把它设置为我的主机IP(138.68.6.26),它应该设置为本地IP(127.0.0.1)。