简体   繁体   English

是否可以从localhost直接连接到Heroku数据库?

[英]Is it possible to directly connect to a Heroku database from localhost?

If I have a DATABASE_URL, is it possible to connect to it from localhost? 如果我有DATABASE_URL,是否可以从localhost连接到它? I'm using the following code: 我正在使用以下代码:

db = URI.parse(database_url)
connection = ActiveRecord::Base.establish_connection(
                                                     :adapter  => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
                                                     :host     => db.host,
                                                     :username => db.user,
                                                     :password => db.password,
                                                     :database => db.path[1..-1],
                                                     :encoding => 'utf8'
                                                     )

When running the code from my pc, I keep getting errors like: 从我的电脑运行代码时,我不断收到如下错误:

could not connect to server: Connection timed out
Is the server running on host some_host.amazonaws.com and accepting
    TCP/IP connections on port 5432?

I'm using the same code to share a database between two apps that are running on Heroku and it works. 我使用相同的代码在Heroku上运行的两个应用程序之间共享数据库并且它可以工作。 This leads me to believe that connecting to a Heroku database is restricted unless you perform it from a Heroku host. 这使我相信除非您从Heroku主机执行,否则连接到Heroku数据库是受限制的。 Is that true? 真的吗?

Indeed SSL is required for outside connections. 实际上,外部连接需要SSL。 The underlying postgres adapter can be forced to use SSL with the sslmode=require parameter. 可以强制底层postgres适配器使用SSL与sslmode=require参数。 You can pass arbitrary parameters down from the ActiveRecord connection options, like so: 您可以从ActiveRecord连接选项中传递任意参数,如下所示:

ActiveRecord::Base.establish_connection(
  adapter:  'postgresql', 
  host:     'host', 
  username: 'user', 
  password: 'pass', 
  database: 'database_name', 
  encoding: 'utf-8',
  port:     '5432', # could be a non-standard port
  sslmode:  'require' # force SSL
)

I've verified this locally, and here's a full session showing it work. 我在本地验证了这一点,这是一个完整的会话,显示它的工作原理。 Please make sure you're not mistyping anything: 请确保您没有错误输入任何内容:

heroku pg:credentials yellow -a my-app                                                               Connection info string:
   "dbname=ddbolrs4g89dsi host=ec2-23-21-91-108.compute-1.amazonaws.com port=5432 user=itkdrxzjmwcjtw password=wU-4tT3YbF8AZ3U5kwu-2KYPEX ssl
mode=require"
$ irb
> require 'active_record'
 => true
> ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'ddbolrs4g89dsi', host: 'ec2-23-21-91-108.compute-1.amazonaws.com', username: 'itkdrxzjmwcjtw', password: 'wU-4tT3YbF8AZ3U5kwu-2KYPEX', sslmodel: 'require')
 => #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x007f7f9ba6f0f0 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007f7f9ba6f078>, @spec=#<ActiveRecord::Base::ConnectionSpecification:0x007f7f9ba64150 @config={:adapter=>"postgresql", :database=>"ddbolrs4g89dsi", :host=>"ec2-23-21-91-108.compute-1.amazonaws.com", :username=>"itkdrxzjmwcjtw", :password=>"wU-4tT3YbF8AZ3U5kwu-2KYPEX", :sslmodel=>"require"}, @adapter_method="postgresql_connection">, @reserved_connections={}, @queue=#<MonitorMixin::ConditionVariable:0x007f7f9ba6f000 @monitor=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x007f7f9ba6f0f0 ...>, @cond=#<ConditionVariable:0x007f7f9ba6efd8 @waiters=[], @waiters_mutex=#<Mutex:0x007f7f9ba6ef88>>>, @timeout=5, @size=5, @connections=[], @automatic_reconnect=true>
> ActiveRecord::Base.connection.execute("SELECT version()").first
 => {"version"=>"PostgreSQL 9.1.4 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3, 64-bit"} 
> exit
$ # remove db, leaked creds ^
$ heroku addons:remove HEROKU_POSTGRESQL_YELLOW -a my-app --confirm my-app 
Removing HEROKU_POSTGRESQL_YELLOW on my-app... done, v490 (free)

Outside connection to the postgresql server require SSL on Heroku. 与postgresql服务器的外部连接需要Heroku上的SSL。 Have a look at the docs as well. 看看文档也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM