Skip to content

Instantly share code, notes, and snippets.

@iaingray
Created November 11, 2014 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iaingray/a584fd083b75cbb2a412 to your computer and use it in GitHub Desktop.
Save iaingray/a584fd083b75cbb2a412 to your computer and use it in GitHub Desktop.
tunneling postgress over SSH in Ruby
#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