2017-04-12 10 views
0

문제 : 사용 PSQL pg_dumppg_restore 파이썬 스크립트와 subprocess 모듈을 사용.pg_dump의 및 파이썬 모듈 서브 프로세스를 사용하여 pg_restore에 암호

배경 : 나는 로컬 호스트에서 다음 python 2.7 스크립트를 사용하고 (즉 Ubuntu 14.04.5 LTS을)를 PSQL 서버에있는 테이블의 백업을 만들 수 있습니다 (즉, PostgreSQL 9.4.11)와 원격 호스트로 (예 : Ubuntu 16.04.2 LTS을)를 복원에서 최신 버전의 PSQL 서버 (예 : PostgreSQL 9.6.2).

#!/usr/bin/python 

from subprocess import PIPE,Popen 

def dump_table(host_name,database_name,user_name,database_password,table_name): 

    command = 'pg_dump -h {0} -d {1} -U {2} -p 5432 -t public.{3} -Fc -f /tmp/table.dmp'\ 
    .format(host_name,database_name,user_name,table_name) 

    p = Popen(command,shell=True,stdin=PIPE) 

    return p.communicate('{}\n'.format(database_password)) 

def restore_table(host_name,database_name,user_name,database_password): 

    command = 'pg_restore -h {0} -d {1} -U {2} < /tmp/table.dmp'\ 
    .format(host_name,database_name,user_name) 

    p = Popen(command,shell=True,stdin=PIPE) 

    return p.communicate('{}\n'.format(database_password)) 

def main(): 
    dump_table('localhost','testdb','user_name','passwd','test_tbl') 
    restore_table('remotehost','new_db','user_name','passwd') 

if __name__ == "__main__": 
    main() 

dump_table() 함수가 성공적으로 완료되고 /tmp/table.sql 파일을 생성하지만 restore_table() 함수는 다음과 같은 에러 리턴 위와 I 순차 함수를 사용하는 경우 : I가 실행하여 인증 정보를 & 출력 체크인

('', 'Password: \npg_restore: [archiver (db)] connection to database "database_name" failed: FATAL: password authentication failed for user "username"\nFATAL: password authentication failed for user "username"\n')*

을 셸의 pg_restore에 대한 명령과 .pgpass에 대한 자격 증명도 포함되어 있습니다 (암호는 p.communicate()에 전달하므로 관련이 없음)

누구나 비슷한 경험을 했습니까? 나는 꽤 붙어있다!

감사합니다, D.

답변

1

다음 작품과 주석의 변경.

나는 (즉,이 목록에 분할되지 않음) 전체 명령을 사용하고 Popenshell=True를 사용할 때 pg_restore 암호 인증 오류가 있음을 생산하는 이유는 그래도 잘 모르겠지만, 다른 한편으로 pg_dumpshell=True & 전체 명령을 사용하여 작동 . <은 무엇인가해야합니까?

#!/usr/bin/python 

from subprocess import PIPE,Popen 
import shlex 

def dump_table(host_name,database_name,user_name,database_password,table_name): 

    command = 'pg_dump -h {0} -d {1} -U {2} -p 5432 -t public.{3} -Fc -f /tmp/table.dmp'\ 
    .format(host_name,database_name,user_name,table_name) 

    p = Popen(command,shell=True,stdin=PIPE,stdout=PIPE,stderr=PIPE) 

    return p.communicate('{}\n'.format(database_password)) 

def restore_table(host_name,database_name,user_name,database_password): 

    #Remove the '<' from the pg_restore command. 
    command = 'pg_restore -h {0} -d {1} -U {2} /tmp/table.dmp'\ 
       .format(host_name,database_name,user_name) 

    #Use shlex to use a list of parameters in Popen instead of using the 
    #command as is. 
    command = shlex.split(command) 

    #Let the shell out of this (i.e. shell=False) 
    p = Popen(command,shell=False,stdin=PIPE,stdout=PIPE,stderr=PIPE) 

    return p.communicate('{}\n'.format(database_password)) 

def main(): 
    dump_table('localhost','testdb','user_name','passwd','test_tbl') 
    restore_table('localhost','testdb','user_name','passwd') 

if __name__ == "__main__": 
    main()