【发布时间】:2021-05-03 06:26:27
【问题描述】:
我在 Linux 上恢复我的数据库模板,如下所示: Linux 终端:
su postgres
psql
create database test3;
\q
pg_restore -v -d test3 /opt/empty-db_template.backup
exit
systemctl restart postgresql-11.service
完成了!但是当我在 python 中执行 Shell 命令时,如图卡在psqlstep 中,我该如何解决?
import os
os.system('ls')
os.system('su postgres')
os.system('psql')
os.system('create database test3;')
os.system('\q')
os.system('pg_restore -v -d test3 /opt/empty-db_template.backup')
os.system('exit')
os.system('systemctl restart postgresql-11.service')
【问题讨论】:
-
你弄错了 os.system。它不会将您的字符串作为击键传递,而是将参数作为命令行执行。因此,例如, os.system('\q') 没有任何意义。您应该将所有操作作为一个命令行。所以你将有三个 os.system 调用(一个用于“ls”,第二个用于运行 psql,第三个用于重新启动 postgresql)