Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

347
Visualizações
Backup Postgres from Python on Win10

I'm trying to backup Postgres from Python on Win10.

I'm working on Anaconda python 3.8, Win10 machine with Postgres12 local. On path environment variable I have postgres (lib and bin), no anaconda, and python 3.8 (no the anaconda one).

I'm able to correctly backup the database using on window's command shell:

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

but when I run it on 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. 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.

It seems that neither os.system or subprocess on the anaconda interpreter, got access to the environment variables on the command shell. How is this possible? and how I can overcome it?. It is different user invoking the shell?

Thanks in advance.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

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:

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.

If anyone have a better explanation, is welcome.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda