Heroku rake 中止(PG::ConnectionBad:无法连接到服务器:没有这样的文件或目录)
Heroku rake aborted (PG::ConnectionBad: could not connect to server: No such file or directory)
我正在使用 Ruby Telegram Bot boilerplate 开发 Telegram 机器人。
它是在 Ruby(没有 Rails)上使用 Active Record 实现的,并且在本地开发时运行良好。
我决定将其部署在 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
.
这就是 Rakefile
和 config/database.yml
在 Heroku 上的样子:
$ 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
这是我配置的构建包:
$ 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 迁移数据库?
您的数据库不会与您的代码位于同一台服务器上,您也无法自行创建。相反,Heroku 会告诉您如何通过 the DATABASE_URL
environment variable.
进行连接
您引用的代码出现在 load its database information from a file without looking at the environment 中。您必须修改它以在设置该变量时使用 ENV['DATABASE_URL']
。
我正在使用 Ruby Telegram Bot boilerplate 开发 Telegram 机器人。
它是在 Ruby(没有 Rails)上使用 Active Record 实现的,并且在本地开发时运行良好。
我决定将其部署在 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
.
这就是 Rakefile
和 config/database.yml
在 Heroku 上的样子:
$ 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
这是我配置的构建包:
$ 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 迁移数据库?
您的数据库不会与您的代码位于同一台服务器上,您也无法自行创建。相反,Heroku 会告诉您如何通过 the DATABASE_URL
environment variable.
您引用的代码出现在 load its database information from a file without looking at the environment 中。您必须修改它以在设置该变量时使用 ENV['DATABASE_URL']
。