Skip to content

Instantly share code, notes, and snippets.

@mhulse
Last active July 26, 2023 15:22
Show Gist options
  • Save mhulse/1954422 to your computer and use it in GitHub Desktop.
Save mhulse/1954422 to your computer and use it in GitHub Desktop.
SSH Tunneling: Map port from remote machine to your local machine so you can work from the remote DB (example using Django DB settings)...

SSH Tunneling

  1. In your local_settings.py file:

    DATABASES = {
    	'default': {
    		'ENGINE': 'django.db.backends.postgresql_psycopg2',
    		'NAME': 'django_%s' % (PROJECT_NAME,),
    		'USER': xxxx,
    		'PASSWORD': xxxx,
    		'HOST': 'localhost',
    		'PORT': '1234',
    	}
    }
    
  2. SSH to development server.

    To connect to the production server, from the command line, run:

    $ ssh -N -L 1234:localhost:5432 remote.com

    • 1234 = local port
    • 5432 = remote port

    Important: Don't close shell window.

    Note: After entering the above command, you won't get any feedback until you exit.

  3. You might need to $ touch your wsgi file or restart Apache (Django development server restarts automatically).

@mhulse
Copy link
Author

mhulse commented Mar 2, 2012

This above code shows you how to make a tunnel to a production server in order to make use of it's database contents.

  • 1234 = Local can be anything not being used.
  • 5432 = Specific to postgres
  • You'll want to google "ssh -N -L tunneling" for more info.

@banago
Copy link

banago commented Mar 2, 2012

What happens if you break the database in the development server? Will it break the live database too?

@mhulse
Copy link
Author

mhulse commented Mar 2, 2012

Good question. Well, you are connected to the live database, so if you do something that would break it, then it will break. :D

@banago
Copy link

banago commented Mar 2, 2012

Oh man! But it still a very good way for testing when you don't need the change the db.

@mhulse
Copy link
Author

mhulse commented Mar 2, 2012

Yah! It's a quick way to get live data to work with. If you get this to work with WP, you might fork this file and leave a comment here with a link to your version. :)

For future reference, here's the discussion that spawned this gist via wp-hackers listserv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment