문제 : 사용 PSQL pg_dump
및 pg_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.