How to Connect to PostgreSQL Through Jump Server and SSH Tunnel?

===========
Python file

from robot.api import logger
import paramiko

def create_ssh_tunnel_and_connect_to_db(ssh_host, ssh_port, ssh_user, ssh_private_key, jump_host, jump_port, jump_user, jump_private_key, db_host, db_port, db_name, db_user, db_password=‘’):
jump_ssh = None
try:
# Establish SSH connection to the jump server
jump_ssh = paramiko.SSHClient()
jump_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
jump_ssh.connect(hostname=jump_host, port=jump_port, username=jump_user, key_filename=jump_private_key, timeout=10)

    # Create local SSH tunnel through the jump server to the database
    jump_transport = jump_ssh.get_transport()
    local_port = jump_transport.request_port_forward('', db_port)  # Empty string for localhost
    
    # Connect to the database through the SSH tunnel
    db_connection = f"dbname={db_name} user={db_user} password={db_password} host=localhost port={local_port}"
    logger.info(f"Connecting to the database: {db_connection}")
    
    # Start a paramiko SSH session
    ssh = jump_ssh.invoke_shell()
    ssh.send(f"ssh -L {local_port}:{db_host}:{db_port} -N {ssh_user}@{ssh_host}\n")
    
    return db_connection

except Exception as e:
    logger.error(f"Error establishing SSH tunnel and connecting to the database: {e}")
    raise
finally:
    if jump_ssh:
        jump_ssh.close()

===========
Robot file

*** Variables ***

${SSH_HOST} 123.26.34.123
${SSH_PORT} 22
${SSH_USER} nishan_1234
${SSH_PRIVATE_KEY} /Users/app-automation/TestData/test.pem

${JUMP_HOST} 123.26.34.123
${JUMP_PORT} 22
${JUMP_USER} nishan_1234
${JUMP_PRIVATE_KEY} /Users/app-automation/TestData/test.pem

${DB_HOST} abc.com
${DB_PORT} 5432
${DB_NAME} nishan
${DB_USER} postgres
${DB_PASSWORD} hgsshhshshsnzj

*** Test Cases ***

Connect to PostgreSQL Through Jump Server
${db_connection} = Create SSH Tunnel And Connect To DB ${SSH_HOST} ${SSH_PORT} ${SSH_USER} ${SSH_PRIVATE_KEY} ${JUMP_HOST} ${JUMP_PORT} ${JUMP_USER} ${JUMP_PRIVATE_KEY} ${DB_HOST} ${DB_PORT} ${DB_NAME} ${DB_USER} ${DB_PASSWORD}
Connect To Database psycopg2 connection=${db_connection}
# Perform database operations…
@{result} Query SELECT * FROM your_table
Log Many @{result}
Disconnect From Database

I’m getting below error:

Connect to PostgreSQL Through Jump Server | FAIL |
ValueError: Required ‘dbUsername’ parameter was not provided in keyword arguments.

Can someone help me to resolve this issue?

Hi
I’m not sure what you are trying to do.
Have you tried this lib :

I want to connect my remote database and get a value. Database is PostgreSQL and there are 2 additional steps to connect to the db; SSH tunnel and jump server as below. I need to know how to connect to the DB.

Hi,
I have never done that before, but I found some informations on the Internet :slight_smile:
Have you seen the SSH Librairy ?
http://robotframework.org/SSHLibrary/SSHLibrary.html
Also, people on seem to have the issue, you might wanna take a look :
https://groups.google.com/g/robotframework-users/c/GCTIG2LUdXA

Hope you find what you are looking for!

Best,
TextSolver34761