Heroku耙终止(PG :: ConnectionBad:无法连接到服务器:没有这样的文件或目录)

时间:2019-03-22 15:28:28

标签: ruby heroku activerecord rake telegram-bot

我正在使用Ruby Telegram Bot boilerplate开发Telegram机器人。

它是使用Active Record在Ruby(无Rails)上实现的,并且在开发中可以在本地正常工作。

我决定将其部署在Heroku上。我跑了

$ heroku buildpacks:set heroku/ruby

推送到Heroku之后,我需要运行db:migrate,它给出了以下输出:

$ heroku run rake db:migrate --trace
Running rake db:migrate --trace on telegram-bot-app... up, run.2290 (Free)
** Invoke db:migrate (first_time)
** Execute db:migrate
rake aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
/app/vendor/bundle/ruby/2.5.0/gems/pg-0.18.4/lib/pg.rb:45:in `initialize'
/app/vendor/bundle/ruby/2.5.0/gems/pg-0.18.4/lib/pg.rb:45:in `new'
/app/vendor/bundle/ruby/2.5.0/gems/pg-0.18.4/lib/pg.rb:45:in `connect'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/connection_adapters/postgresql_adapter.rb:697:in `connect'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/connection_adapters/postgresql_adapter.rb:221:in `initialize'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in `new'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in `postgresql_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:759:in `new_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:803:in `checkout_new_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:782:in `try_to_checkout_new_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:743:in `acquire_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:500:in `checkout'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `retrieve_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/connection_handling.rb:116:in `retrieve_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/connection_handling.rb:88:in `connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/schema_migration.rb:20:in `table_exists?'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/schema_migration.rb:24:in `create_table'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/migration.rb:1125:in `initialize'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/migration.rb:1007:in `new'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/migration.rb:1007:in `up'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6.2/lib/active_record/migration.rb:985:in `migrate'
/app/Rakefile:14:in `block (2 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/task.rb:271:in `block in execute'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/task.rb:271:in `each'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/task.rb:271:in `execute'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/task.rb:213:in `block in invoke_with_call_chain'
/app/vendor/ruby-2.5.1/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/task.rb:193:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/task.rb:182:in `invoke'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/application.rb:160:in `invoke_task'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/application.rb:116:in `each'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/application.rb:116:in `block in top_level'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/application.rb:125:in `run_with_threads'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/application.rb:110:in `top_level'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/application.rb:83:in `block in run'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/application.rb:186:in `standard_exception_handling'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/lib/rake/application.rb:80:in `run'
/app/vendor/bundle/ruby/2.5.0/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
/app/vendor/bundle/ruby/2.5.0/bin/rake:23:in `load'
/app/vendor/bundle/ruby/2.5.0/bin/rake:23:in `<top (required)>'
/app/vendor/bundle/ruby/2.5.0/gems/bundler-1.15.2/lib/bundler/cli/exec.rb:74:in `load'
/app/vendor/bundle/ruby/2.5.0/gems/bundler-1.15.2/lib/bundler/cli/exec.rb:74:in `kernel_load'
/app/vendor/bundle/ruby/2.5.0/gems/bundler-1.15.2/lib/bundler/cli/exec.rb:27:in `run'
/app/vendor/bundle/ruby/2.5.0/gems/bundler-1.15.2/lib/bundler/cli.rb:360:in `exec'
/app/vendor/bundle/ruby/2.5.0/gems/bundler-1.15.2/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/app/vendor/bundle/ruby/2.5.0/gems/bundler-1.15.2/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/app/vendor/bundle/ruby/2.5.0/gems/bundler-1.15.2/lib/bundler/vendor/thor/lib/thor.rb:369:in `dispatch'
/app/vendor/bundle/ruby/2.5.0/gems/bundler-1.15.2/lib/bundler/cli.rb:20:in `dispatch'
/app/vendor/bundle/ruby/2.5.0/gems/bundler-1.15.2/lib/bundler/vendor/thor/lib/thor/base.rb:444:in `start'
/app/vendor/bundle/ruby/2.5.0/gems/bundler-1.15.2/lib/bundler/cli.rb:10:in `start'
/app/vendor/bundle/ruby/2.5.0/gems/bundler-1.15.2/exe/bundle:30:in `block in <top (required)>'
/app/vendor/bundle/ruby/2.5.0/gems/bundler-1.15.2/lib/bundler/friendly_errors.rb:121:in `with_friendly_errors'
/app/vendor/bundle/ruby/2.5.0/gems/bundler-1.15.2/exe/bundle:22:in `<top (required)>'
/app/vendor/bundle/ruby/2.5.0/bin/bundle:23:in `load'
/app/vendor/bundle/ruby/2.5.0/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate

运行db:create时也会发生相同的问题。

这是Heroku上Rakefileconfig/database.yml的样子:

$ heroku run bash
Running bash on telegram-bot-app... up, run.3429 (Free)
~ $ cat Rakefile
require 'rubygems'
require 'bundler/setup'

require 'pg'
require 'active_record'
require 'yaml'

namespace :db do

  desc 'Migrate the database'
  task :migrate do
    connection_details = YAML::load(File.open('config/database.yml'))
    ActiveRecord::Base.establish_connection(connection_details)
    ActiveRecord::Migrator.migrate('db/migrate/')
  end

  desc 'Create the database'
  task :create do
    connection_details = YAML::load(File.open('config/database.yml'))
    admin_connection = connection_details.merge({'database'=> 'postgres',
                                                'schema_search_path'=> 'public'})
    ActiveRecord::Base.establish_connection(admin_connection)
    ActiveRecord::Base.connection.create_database(connection_details.fetch('database'))
  end

  desc 'Drop the database'
  task :drop do
    connection_details = YAML::load(File.open('config/database.yml'))
    admin_connection = connection_details.merge({'database'=> 'postgres',
                                                'schema_search_path'=> 'public'})
    ActiveRecord::Base.establish_connection(admin_connection)
    ActiveRecord::Base.connection.drop_database(connection_details.fetch('database'))
  end
end
~ $ cat config/database.yml
adapter: postgresql
database: bot
encoding: unicode
pool: 5
timeout: 5000

这是我配置的buildpack:

$ heroku buildpacks
=== telegram-bot-app Buildpack URL
heroku/ruby

这是我的插件:

$ heroku addons

Add-on                                          Plan       Price  State  
──────────────────────────────────────────────  ─────────  ─────  ───────
heroku-postgresql (postgresql-contoured-96514)  hobby-dev  free   created
 └─ as DATABASE

The table above shows add-ons and the attachments to the current app (telegram-bot-app) or other apps.

那是我的Gemfile

source 'https://rubygems.org'

ruby '2.5.1'

gem 'telegram-bot-ruby'
gem 'activerecord', '5.1.6.2'
gem 'i18n'
gem 'pg', '~> 0.18'
gem 'rake', '12.3.1'
gem 'dotenv'

可能是问题所在,以及如何使Heroku迁移数据库?

1 个答案:

答案 0 :(得分:0)

您的数据库将与您的代码不在同一服务器上,并且您不能自己创建它。相反,Heroku会告诉您如何通过the DATABASE_URL environment variable进行连接。

您引用的代码显示在load its database information from a file without looking at the environment上。设置该变量后,您必须对其进行修改以使用ENV['DATABASE_URL']