title: Heroku rake aborted (PG::ConnectionBad: could not connect to server: No such file or directory)
body: <p>I am developing Telegram bot using <a href="https://github.com/MaximAbramchuck/ruby-telegram-bot-starter-kit" rel="nofollow noreferrer">Ruby Telegram Bot boilerplate</a>.</p>
  
  <p>It is implemented on Ruby (without Rails) using Active Record and works fine locally on development.</p>
  
  <p>I decided to deploy it on Heroku. I ran</p>
  
  <pre><code>$ heroku buildpacks:set heroku/ruby
  </code></pre>
  
  <p>After pushing to Heroku I need ran <code>db:migrate</code>, which gave the following output:</p>
  
  <pre><code>$ 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 &lt;top (required)&gt;'
  /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 `&lt;top (required)&gt;'
  /app/vendor/bundle/ruby/2.5.0/bin/rake:23:in `load'
  /app/vendor/bundle/ruby/2.5.0/bin/rake:23:in `&lt;top (required)&gt;'
  /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 &lt;top (required)&gt;'
  /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 `&lt;top (required)&gt;'
  /app/vendor/bundle/ruby/2.5.0/bin/bundle:23:in `load'
  /app/vendor/bundle/ruby/2.5.0/bin/bundle:23:in `&lt;main&gt;'
  Tasks: TOP =&gt; db:migrate
  </code></pre>
  
  <p>The same problem happens when I run <code>db:create</code>.</p>
  
  <p>This is what <code>Rakefile</code> and <code>config/database.yml</code> look like on Heroku:</p>
  
  <pre><code>$ 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'=&gt; 'postgres',
                                                  'schema_search_path'=&gt; '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'=&gt; 'postgres',
                                                  'schema_search_path'=&gt; '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
  </code></pre>
  
  <p>Here are my configured buildpacks:</p>
  
  <pre><code>$ heroku buildpacks
  === telegram-bot-app Buildpack URL
  heroku/ruby
  </code></pre>
  
  <p>Here are my addons:</p>
  
  <pre><code>$ 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.
  </code></pre>
  
  <p>That's my <code>Gemfile</code>:</p>
  
  <pre><code>source 'https://rubygems.org'
  
  ruby '2.5.1'
  
  gem 'telegram-bot-ruby'
  gem 'activerecord', '5.1.6.2'
  gem 'i18n'
  gem 'pg', '~&gt; 0.18'
  gem 'rake', '12.3.1'
  gem 'dotenv'
  </code></pre>
  
  <p>What could be the problem and how to get Heroku to migrate the database?</p>