简体   繁体   中英

PG::ConnectionBad: could not connect to server: Connection refused (Ubuntu 16.04, Rails 5, Capistrano)

When deploying a Rails 5 app to Ubunbtu 16.04.3 (Digitalocean VPS), when running cap production deploy --trace I'm getting the following error:

.

Terminal

$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'

.

config/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"

.

config/deploy/production.rb

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

.

On the server, I've already made the symlinked files /home/deploy/my-app/shared/config/database.yml and /home/deploy/my-app/shared/config/secrets.yml and removed them from my repo. I also git ignored them as well.

.

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...

.

I also set up an Ubuntu system user named postgres and created a database user named deploy. I named the database correctly as indicated within the database.yml file. I've searched all over the place an cannot get this to work. Most of the answers are for debugging locally. This is for a deployment.

Try to connect to this server via psql, if you can't, the problem is in your postgresql server config. (Do this from the app's server)

psql -h 138.68.6.26 -U deploy my-app

Your Postgres server is configured to listen on the localhost interface only and hence not available from the outside.

To configure Postgres to allow external TCP connections, make these changes:

1) set listen_addresses to * in /etc/postgresql/<>/main/postgresql.conf

2) set the access permissions in /etc/postgresql/<>/main/pg_hba.conf to allow TCP connections from external IPs

Just make sure you got a firewall like UFW set up to prevent arbitrary hosts to connect to your database.

As it turns out, I had the wrong ip address in the database.yml file. I had it set to my host ip (138.68.6.26) when it should have been set to the local ip (127.0.0.1).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM