I'm trying to automatically download pg_dump files and restore these. I'm having trouble executing the pg_restore
command. I'm currently using:
from subprocess import PIPE, Popen
import shlex
command = f'pg_restore ' \
f'-h {host} ' \
f'-d {database} ' \
f'-U {username} ' \
f'{file_path}'
command = shlex.split(command)
p = Popen(command, shell=False, stdin=PIPE, stdout=PIPE, stderr=PIPE)
p.communicate(str.encode(f'\r{password}\r'))
This however prompts me to fill in my password (when I fill it in, it works just fine).
What I've tried so far:
- set
shell = True
(in which case I get the error[Info] 2019-12-09 14:59:55 - [root] (b'', b'pg_restore: [archiver] input file does not appear to be a valid archive (too short?)\n')
) - pass the password using
p.stdin.write(f'{self.push_password}\n')
p.stdin.flush()
- Using
\n
instead of\r
- Only passing
\r
at the end of thep.communicate
I'm out of ideas and don't know what to do. Does anybody know how to automatically pass my password when calling pg_restore from python?