I want to create a backup of my postgres database via SSH connecting to a Windows Server 2019. I use the Paramiko Python library in order to do this, but unfortunately the sql-file does not contain any data (file size is 0 and files cannot not deleted as they are still opened in cmd). Thus, I suspect the execution of my command hasn't finished ... This is my function:
def ssh_server(server, username, password, pg_pass, ps_user, database):
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=server, username=username, password=password)
stdin_2, stdout_2, stderr_2 = client.exec_command("SET PGPASSWORD=secret_pw")
stdin, stdout, stderr = client.exec_command(
"pg_dump -U {} {} > kp_{}.sql\n".format(
ps_user, database, ts_str), get_pty=True)
client.close()
BTW: Executing the commands in PuTTY yields the desired output. Does anyone have an idea how to fix this issue? Thanks for your help!