-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrestore-dump.py
35 lines (29 loc) · 1.01 KB
/
restore-dump.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import subprocess
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
def restore_postgres_db(db_host, db, port, user, password, backup_file):
"""
Restore postgres db from a file.
"""
print(user,password,db_host,port, db)
process = subprocess.Popen(
['pg_restore',
'--no-owner',
'--dbname=postgresql://{}:{}@{}:{}/{}'.format(user,
password,
db_host,
port, db),
'-v',
backup_file],
stdout=subprocess.PIPE
)
output = process.communicate()[0]
if int(process.returncode) != 0:
print('Command failed. Return code : {}'.format(process.returncode))
return output
host = "localhost"
database_name = "test"
port="5432"
user="postgres"
password="postgres"
dest_file="dump.sql"
restore_postgres_db(host, database_name, port, user, password, dest_file)