0

I had a program which makes PostgreSQL backups, and gives the user the chance to zip these backups:

if bkp_type == 'gz':
   command = 'pg_dumpall -U {} -h {} -p {} | gzip > {}'.format(user, server, port, file)
elif bkp_type == 'bz2':
   command = 'pg_dumpall -U {} -h {} -p {} | bzip2 > {}'.format(user, server, port, file)
elif bkp_type == 'zip':
   command = 'pg_dumpall -U {} -h {} -p {} | zip > {}'.format(user, server, port, file)
else:
   command = 'pg_dumpall -U {} -h {} -p {} > {}'.format(user, server, port, file)

result = subprocess.call(command, shell=True)

Now, I am using pg_restore to restore them, but I am not able to do it if the file is compressed. Is there any way to do it directly with pg_restore (like in pg_dump or pg_dumpall) or do I have to check the kind of compressing used and unzip them with Python?

3
  • I you can make a program to pipe a dump file into a zip program, why not simply make a program that unzips a file and pipes it into pg_restore?
    – Patrick
    Commented May 9, 2014 at 11:06
  • I tried with an example of a zip file: unzip | pg_restore -U my_user -h my_server -p my_port -d path new_dbname, gives me the error pg_restore: [archiver] input file does not appear to be a valid archive
    – forvas
    Commented May 9, 2014 at 11:19
  • Can you unzip to a regular file and update your post to show the start of the dump file? You may have issues with PG versions or file formats, hard to say without any evidence.
    – Patrick
    Commented May 9, 2014 at 11:30

1 Answer 1

1

Finally I managed this solution for restoring the backups generated by pg_dump (not pg_dumpall yet):

if ext == 'gz':
   command = 'gunzip -c {} -k | pg_restore -U {} -h {} -p {}' \
             '-d {}'.format(file, user, server, port, new_dbname)
elif ext == 'bz2':
   command = 'bunzip2 -c {} -k | pg_restore -U {} -h {} -p {}' \
             '-d {}'.format(file, user, server, port, new_dbname)
elif ext == 'zip':
   command = 'unzip -p {} | pg_restore -U {} -h {} -p {} ' \
             '-d {}'.format(file, user, server, port, new_dbname)
else:
   command = 'pg_restore -U {} -h {} -p {} -d {} {}'.format(user,
                server, port, new_dbname, file)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.