I am trying to create an automatic pg dump backup script for PostgreSQL DB. The script ask for a DB password. I am trying to put the password using stdin. But, it's not working. 'popen.stdin.write("rootpasswordhere123#!\n")', this line not inserting the password into the script terminal.
import gzip
import subprocess
with gzip.open('backup.gz', 'wb') as f:
popen = subprocess.Popen(['pg_dump', '--no-owner', '--no-acl', '-U', 'postgres', '-d', 'database_name', '-h', '178.111.11.111'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
time.sleep(5)
popen.stdin.write("rootVidhyaDhan123#!\n") # code not working
for stdout_line in iter(popen.stdout.readline, ""):
f.write(stdout_line.encode('utf-8'))
popen.stdin.close()
popen.stdout.close()
popen.wait()