簡體   English   中英

是否可以從localhost直接連接到Heroku數據庫?

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

如果我有DATABASE_URL,是否可以從localhost連接到它? 我正在使用以下代碼:

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'
                                                     )

從我的電腦運行代碼時,我不斷收到如下錯誤:

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?

我使用相同的代碼在Heroku上運行的兩個應用程序之間共享數據庫並且它可以工作。 這使我相信除非您從Heroku主機執行,否則連接到Heroku數據庫是受限制的。 真的嗎?

實際上,外部連接需要SSL。 可以強制底層postgres適配器使用SSL與sslmode=require參數。 您可以從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
)

我在本地驗證了這一點,這是一個完整的會話,顯示它的工作原理。 請確保您沒有錯誤輸入任何內容:

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)

與postgresql服務器的外部連接需要Heroku上的SSL。 看看文檔也是如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM