[フレーム]
Last Updated: November 09, 2022
·
6.774K
· nickjacob

Using a Database through an SSH Tunnel in a Rails Environment

If you want to use your local machine to inspect a remote database over SSH, you can add it as an environment in rails to let you run your app locally off of the remote DB (and do rails console):

config/environments/remote.rb:

import 'net/ssh/gateway'
module RemoteConnectionManager
 SSH_USER = 'myuser'

 def self.port_through_tunnel(remote_host, port)
 return Net::SSH:Gateway.new(remote_host, SSH_USER)
 .open(port)
 end
end

database.yml:

production: &prod
 host: 'prod.mysite.com'
 port: 5432
 ...
remote:
 <<: *prod
 host: 127.0.0.1
 port: <%= RemoteConnectionManager.port_through_tunnel('prod.mysite.com', 5432) %>

3 Responses
Add your response

import 'net/ssh/gateway' should probably be require 'net/ssh'.. and net-ssh has to be in Gemfile..

But still I get this after running "rails console remote":

(erb):29:in <main>': Cannot loadRails.application.database_configuration`:
uninitialized constant RemoteConnectionManager (NameError)

over 1 year ago ·

Same problem. Needs to be

Net::SSH::Gateway
over 1 year ago ·

This used to work beautifully - until I upgrade to MacOS Ventura today. Has anyone got it working on there ?

over 1 year ago ·

AltStyle によって変換されたページ (->オリジナル) /