Friday, 28 December 2012

Python script to backup all database

list all the database from the  postgresql server, 
run the query in postgresql 

psql SELECT datname FROM pg_database;

it will list all the database from the postgres server. copy and paste it in one text file. and run the script.

import os

lines = [line.strip() for line in open('<<path to file>>')]
for i in lines:
    print os.system("pg_dump %s >
<<path to file>>%s.out" % (i,i))

it will dump all the database in particulate folder