在 Heroku 上从 Mysql 切换到 postgres 后如何连接到 pg; PG连接被拒绝

How do I conneect to pg after switch from Mysql to postgres on Heroku; PG Connection refused

我将 rails 4.2 应用程序从 mysql 切换到 Heroku 上的 postgres。切换后在本地一切正常,并且在 postgre 上运行完美,但是当我尝试在 Heroku 上进行迁移时收到以下消息:

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

我的 Heroku 环境变量是:

 HEROKU_POSTGRESQL_BLACK_URL:     postgres://bbczqezwwklzqp:1jBcxlGoibmUuriNufTTx2fFOk@ec2-54-163-248-144.compute-1.amazonaws.com:5432/dcit0akvuomjtr
 LANG:                           en_US.UTF-8
 NEW_RELIC_AGGRESSIVE_KEEPALIVE: 1
 NEW_RELIC_LICENSE_KEY:          aaca6674945ff7f7f542e6db625cb7a509ad7746
 NEW_RELIC_LOG:                  stdout
 PAPERTRAIL_API_TOKEN:           Ckbl776aK5kMKkml7K41
 RACK_ENV:                       staging
 RAILS_ENV:                      staging
 SECRET_KEY_BASE:                3d21b4717dc1755ea7c924b6ad6e3d8770b0099b4d87892dbebd92b6f7b1c287831fd564115de2650bdf7339c472e7dd83a4b56ab6567863d9885f2097709065

我的 database.yml 是:

 development: &default
   adapter: postgresql
   database: lawgix_development
   encoding: utf8
   host: localhost
   min_messages: warning
   pool: 2
   timeout: 5000

 test:
   <<: *default
   database: lawgix_test

 staging:
   <<: *default
   database: lawgix_staging

 production:
   <<: *default
   database: lawgix

看来您需要在 Heroku 中配置 DATABASE_URL env 变量。否则,Rails 将退回到您的 database.yml 并尝试使用 localhost

中不存在的数据库

你可以试试这个命令:

 heroku pg:promote HEROKU_POSTGRESQL_BLACK_URL

或者只是定义它来模仿 HEROKU_POSTGRESQL_BLACK_URL 你自己。