简体   繁体   English

在 Win10 上从 Python 备份 Postgres

[英]Backup Postgres from Python on Win10

I'm trying to backup Postgres from Python on Win10.我正在尝试从 Win10 上的 Python 备份 Postgres。

I'm working on Anaconda python 3.8, Win10 machine with Postgres12 local.我正在使用 Anaconda python 3.8,Win10 机器与 Postgres12 本地。 On path environment variable I have postgres (lib and bin), no anaconda, and python 3.8 (no the anaconda one).在路径环境变量上,我有 postgres(lib 和 bin),没有 anaconda 和 python 3.8(没有 anaconda 之一)。

I'm able to correctly backup the database using on window's command shell:我可以使用窗口命令 shell 正确备份数据库:

pg_dump --dbname=postgresql://postgres:password@127.0.0.1:5432/test > C:\backup\dumpfile3.dump

but when I run it on anaconda:但是当我在 anaconda 上运行它时:

os.system("pg_dump --dbname=postgresql://postgres:password@127.0.0.1:5432/test > C:\backup\dumpfile3.dump"  ) 

I get as output 1 , witch is error code.我得到 output 1 ,女巫是错误代码。 It creates the file, but it's empty.它创建文件,但它是空的。

Using:使用:

import subprocess
stk= 'pg_dump --dbname=postgresql://postgres:password@127.0.0.1:5432/test > C:\backup\dumpfile3.dump'

try:
    subprocess.check_output(stk, shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
    raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

I get:我得到:

RuntimeError: command 'pg_dump --dbname=postgresql://postgres:password@127.0.0.1:5432/test > C:\backup\dumpfile3.dump' return with error (code 1): b"'pg_dump' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n"

If I use: subprocess.run or subprocess.call I doesn't produce error, but the created file it's empty.如果我使用: subprocess.runsubprocess.call我不会产生错误,但创建的文件是空的。

It seems that neither os.system or subprocess on the anaconda interpreter, got access to the environment variables on the command shell.似乎os.system解释器上的 os.system 或subprocess进程都无法访问命令 shell 上的环境变量。 How is this possible?这怎么可能? and how I can overcome it?.以及如何克服它? It is different user invoking the shell?调用 shell 是不同的用户吗?

Thanks in advance.提前致谢。

The Computer was restarted, and it solves the issue... .电脑重启了,问题就解决了。。。。 There was no change in the paths, I believe that from the moment things (python,postgres, ...) were installed, the machine hasn"t been restarted.路径没有变化,我相信从安装东西(python,postgres,...)的那一刻起,机器还没有重新启动。

import os
os.system("pg_dump --dbname=postgresql://postgres:password@127.0.0.1:5432/test > C:\backup\dumpfile3.dump" )

worked,, and工作,和

import subprocess
subprocess.call(r"C:\some\path\backup.bat")

also worked.. Inside backup:bat is:也有效..里面的备份:bat是:

pg_dump pg_dump --dbname=postgresql://postgres:password@127.0.0.1:5432/test > C:\backup\dumpfile3.dump

I imagine that the issue was that the anaconda interpreter need a system restart to get access to the environment variables (where the postgres variable was), witch make very little sense as return with error (code 1): b"'pg_dump' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n" seen like a console message.我想问题在于 anaconda 解释器需要重新启动系统才能访问环境变量(postgres 变量所在的位置),因为return with error (code 1): b"'pg_dump' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n"看起来像控制台消息。

If anyone have a better explanation, is welcome.如果有人有更好的解释,欢迎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM