Created
November 11, 2014 00:38
-
-
Save iaingray/a584fd083b75cbb2a412 to your computer and use it in GitHub Desktop.
tunneling postgress over SSH in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#tunnel pg through ssh | |
#pg test | |
require 'pg' | |
require 'net/ssh/gateway' | |
gate = Net::SSH::Gateway.new( | |
'BASTION_HOST_ADDR', | |
'BASTION_USER', | |
{port: 22, key_data: ['PRIVATE_KEY( as string)'], keys_only: true} | |
) | |
conn_details = { | |
:dbname => 'DATABASE_NAME', | |
:user => 'DB_USER', | |
:password => 'DB_PASS', | |
:host => '127.0.0.1', #localhost on bastion | |
:port => gate.open('DB_HOST', 5439) #accessible from bastion host | |
} | |
conn = PG.connect(conn_details) | |
puts conn.exec('SELECT * FROM public.test').values.to_s; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment