Problem: Use the PSQL pg_dump
and pg_restore
in a Python script and using the subprocess
module.
Backgrou
You can use environment variables https://www.postgresql.org/docs/11/libpq-envars.html and "--no-password" option for pg_dump.
def dump_schema(host, dbname, user, password, **kwargs):
command = f'pg_dump --host={host} ' \
f'--dbname={dbname} ' \
f'--username={user} ' \
f'--no-password ' \
f'--format=c ' \
f'--file=/tmp/schema.dmp '
proc = Popen(command, shell=True, env={
'PGPASSWORD': password
})
proc.wait()