事实上,外部连接需要 SSL。可以通过sslmode=require
参数强制底层 postgres 适配器使用 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)