Connecto to redshift via ssh tunnel using python

Viewed 15

hope you guys can help me.

I'm trying to connect to a redshift database using python. I have all the credentials needed to connect, because I've stabelished a connection via DBeaver and can query the database.

This is how I'm trying to connect using python:

First I create a tunnel to a EC2 machine:

def create_redshift_tunnel(ec2_machine="ec2-machine-url",
                    ssh_username="ec2-user",
                    ssh_pkey="ssh_pkey_location",
                    database_url="redshift_database_url"):
    
    
    
    global redshift_tunnel
    redshift_tunnel = SSHTunnelForwarder(
        (ec2_machine),
        ssh_username=ssh_username,
        ssh_pkey=ssh_pkey,
        remote_bind_address=(database_url, 5439)
    )
    
    redshift_tunnel.start()

After creating the tunnel I use the redshift_connector to try and connect.

Once the tunnel is stabilished this is what I'm trying:

conn = redshift_connector.connect(
     host="127.0.0.1",
     database="db_name",
     user="db_user",
     password="password",
     port=5439
  )

When I try this, I get the connection refused error. I read somewhere that I might need a ssl certificate? But I'm not sure...

I've tried changing the host to the same address as 'redshift_database_url' but when I try it like that I get a timeout. I really don't know that to do, any suggestions? Thanks!

0 Answers
Related