2012-07-29 72 views
2

如果我有一個DATABASE_URL,是否可以從本地連接到它?我使用下面的代碼:是否有可能從本地主機直接連接到Heroku數據庫?

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主機執行它。真的嗎?

回答

6

確實SSL是外部連接所必需的。底層的postgres適配器可以被迫使用參數爲sslmode=require的SSL。您可以從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) 
+0

即使將'sslmode'傳遞給連接,我仍然收到相同的錯誤消息。 – Geo 2012-08-05 12:43:53

+0

添加完整示例。 DM給我你的應用名稱或打開支持票,我可以進一步調查。你能夠從Heroku內連接到這個數據庫嗎?如果你嘗試使用全新的'dev'數據庫怎麼辦? – hgmnz 2012-08-05 17:36:49

+0

我的問題是我的heroku數據庫在端口5472上運行。我調整了代碼以使用一個端口,也許它會很好適應你的代碼片段?感謝您的大力支持! – Geo 2012-08-08 07:21:56

1

外部連接到postgresql服務器需要在Heroku上使用SSL。看看docs也是一樣。

+0

任何想法如何將它整合到'establish_connection'中? – Geo 2012-07-29 13:34:20

+0

似乎有一種'postgresql' gem的方法:http://fftools.blogspot.de/2012/03/ruby-and-postgresql-or-what-you-can-do.html 不要知道它是否與Heroku gem建議的'pg'一起工作。 – iltempo 2012-07-29 13:44:33

相關問題