在Heroku上建立外部数据库连接 [英] Establish external database connection in production on Heroku

查看:164
本文介绍了在Heroku上建立外部数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立到Heroku上的辅助外部数据库的连接(这是在AWS上运行的PostgreSQL数据库)。我试图找到最简单和/或最好的方式来做到这一点。



我曾尝试使用Capistrano任务来复制 database.yml 文件部署到Heroku:

 在deploy:update_code,deploy:config_symlink之后

命名空间:deploy do

任务:config_symlink do
运行cp#{shared_pa​​th} /shared/config/database.yml#{release_path} / config / database .yml
end
end

我尝试通过Heroku建立连接config vars:

  class Pgdb< ActiveRecord :: Base.establish_connection(
:adapter => ENV ['PG_ADAPTER'],
:database => ENV ['PG_DB'],
:username => ENV [ 'PG_USER'],
:password => ENV ['PG_PW'],
:host => ENV ['PG_HOST']


self .abstract_class = true
self.table_name ='test'
$ b $ def self.getCardInfo(card_name)
get = connection.query(SELECT * FROM test)
get
end
end

我无法找到任何有意义的文档或者告诉我如何做到这一点,我不知道在上面的尝试中我是否接近或失败,我正在寻找解决上述问题或解决此问题的任何解决方案。

解决方案

您可以更改 heroku config的 database_url / code>,格式如下:

  postgres://< user>:< password> @ <服务器> ;:< port> /< db_name> 

例如:

  heroku config:set DATABASE_URL = postgres:// myusername:mypassword@111.111.111.11:5542 / myproject_production 

只要确保防火墙允许外部连接到RDS即可。


I am trying to establish a connection to a secondary, external database on Heroku (this is a PostgreSQL db running on AWS). I am trying to find the easiest and/or best way to do this.

I have tried using a Capistrano task to copy the database.yml file during deployment to Heroku:

after "deploy:update_code","deploy:config_symlink"

namespace :deploy do

  task :config_symlink do
    run "cp #{shared_path}/shared/config/database.yml #{release_path}/config/database.yml"
  end
end

I have tried establishing the connection via Heroku config vars:

class Pgdb < ActiveRecord::Base.establish_connection(
    :adapter => ENV['PG_ADAPTER'],
    :database => ENV['PG_DB'],
    :username => ENV['PG_USER'],
    :password => ENV['PG_PW'],
    :host => ENV['PG_HOST']
  )

  self.abstract_class = true
  self.table_name = 'test'

  def self.getCardInfo(card_name)
    get = connection.query("SELECT * FROM test)
    get
  end
end

I can't find any documentation that makes sense or tells me exactly how to do this. I do not know if I'm close or way off in the attempts above. I'm looking for any solution that fixes the above attempts or any other solution to this problem.

解决方案

You could change the database_url of the heroku config in the format shown below:

postgres://<user>:<password>@<server>:<port>/<db_name>

For example:

heroku config:set DATABASE_URL=postgres://myusername:mypassword@111.111.111.11:5542/myproject_production

Just make sure the firewall allows the external connection to RDS.

这篇关于在Heroku上建立外部数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆