gpt4 book ai didi

python - 使用 python 模块子进程的 pg_dump 和 pg_restore 密码

转载 作者:行者123 更新时间:2023-12-03 03:02:09 26 4
gpt4 key购买 nike

问题:在 Python 脚本中使用 PSQL pg_dumppg_restore 并使用 subprocess 模块。

背景:我正在使用来自本地主机(即 Ubuntu 14.04.5 LTS)的以下 python 2.7 脚本来创建PSQL 服务器(即 PostgreSQL 9.4.11)中的表并将其恢复到较新版本的 PSQL 服务器中的远程主机(即 Ubuntu 16.04.2 LTS)(即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() 函数返回以下错误:

('', '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')*

我已经通过在 shell 中执行 pg_restore 命令来检查凭据和输出,并且还将凭据包含到 .pgpass 中(尽管不相关,因为我在 中传递密码) p.communicate())

有人有类似经历吗?我几乎被困住了!

问候,D.

最佳答案

对以下作品和所做的更改进行了评论。

我不确定为什么 pg_restore使用完整命令(即不在列表中拆分)并使用 shell=True 时会产生密码身份验证错误在Popen ,但是pg_dump另一方面,使用 shell=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()

关于python - 使用 python 模块子进程的 pg_dump 和 pg_restore 密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43380273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com