Ankit-Kulkarni

Code snippet to find existence of a database in postgresql u

Apr 29th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.38 KB | None | 0 0
  1. db= "apdb"
  2. c1 = "psql -U postgres -lqt"
  3. c2 = "cut -d | -f 1"
  4. c3 = "grep -w " + db
  5.  
  6. ps1 = subprocess.Popen(c1.split(),stdout=subprocess.PIPE)
  7. ps2 = subprocess.Popen(c2.split(),stdin=ps1.stdout, stdout=subprocess.PIPE)
  8. ps3 = subprocess.Popen(c3.split(),stdin=ps2.stdout , stdout=subprocess.PIPE)
  9. result = ps3.communicate()[0]  # if empty , db not found
  10. result = result.strip()
Add Comment
Please, Sign In to add comment