Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to remote PostgreSQL database with SQLAlchemy using SSH tunneling with public key and passphrase, all that from a Windows machine

So, I am using Windows to connect to remote Postgres database with a tool like Navicat. I am trying to achieve the same thing using Python (2.7) and SQLAlchemy (0.9) but with no success.

So, my Navicat setup looks like this:

enter image description here

enter image description here

I tried to set up tunnels in PuTTY, connect to server, leave connection opened and experimenting with IPs and ports but with no success at all. I'm not sure if all that's enough since I have this public key and passphrase, so I wonder is it possible to connect to this database from Windows machine using Python and SQLAlchemy (and this beautiful PuTTY app also, of course) in any way?
Thanks!

like image 546
errata Avatar asked Aug 07 '14 16:08

errata


1 Answers

You can pass the server and port to your create_engine() statement, ala:

create_engine('postgres://username:password@server:port/database')

http://docs.sqlalchemy.org/en/rel_0_9/dialects/postgresql.html

So assuming you've FIRST established an SSH tunnel to the remote host, and port forwarded local port 1111 to the remote server's port 5432 (which is the default postgres port), your connect string would be something like:

create_engine('postgres://username:password@localhost:1111/database')
like image 162
user590028 Avatar answered Sep 24 '22 17:09

user590028