pg_dump & pg_restore password using python module subprocess

后端 未结 3 2076
梦毁少年i
梦毁少年i 2021-02-10 23:45

Problem: Use the PSQL pg_dump and pg_restore in a Python script and using the subprocess module.

Backgrou

3条回答
  •  广开言路
    2021-02-11 00:11

    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()
    

提交回复
热议问题