Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

350
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!