Created
December 9, 2016 16:47
-
-
Save anthonycarminati/24b0441e9185d52d42ac07e807b9c2e5 to your computer and use it in GitHub Desktop.
psycopg2_with_ssh_tunneling
This file contains hidden or 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
import psycopg2 | |
from sshtunnel import SSHTunnelForwarder | |
try: | |
with SSHTunnelForwarder( | |
('<server ip address>', 22), | |
#ssh_private_key="</path/to/private/ssh/key>", | |
### in my case, I used a password instead of a private key | |
ssh_username="<server username>", | |
ssh_password="<mypasswd>, | |
remote_bind_address=('localhost', 5432)) as server: | |
server.start() | |
print "server connected" | |
params = { | |
'database': '<dbname>', | |
'user': '<dbusername>', | |
'password': '<dbuserpass>', | |
'host': 'localhost', | |
'port': local_bind_port | |
} | |
conn = psycopg2.connect(**params) | |
curs = conn.cursor() | |
print "database connected" | |
except: | |
print "Connection Failed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment