pgtool 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/python
  2. import os
  3. import sys
  4. # print 'Number of arguments:', len(sys.argv), 'arguments.'
  5. # print 'Argument List:', str(sys.argv)
  6. actions = [
  7. 'help',
  8. 'create_database',
  9. 'create_user',
  10. 'delete_database',
  11. 'delete_user',
  12. 'grant_all_privileges',
  13. 'test_connection'
  14. ]
  15. def usage():
  16. print('')
  17. print('USAGE: '+__file__+' ['+'|'.join(action for action in actions)+'] [parameters]')
  18. print('')
  19. print(' Role management')
  20. print('')
  21. print(' '+__file__+' create_user <user_name> <password>')
  22. print(' '+__file__+' delete_user <user_name>')
  23. print('')
  24. print(' Database management')
  25. print('')
  26. print(' '+__file__+' create_database <database_name>')
  27. print(' '+__file__+' delete_database <database_name> --force')
  28. print(' '+__file__+' grant_all_privileges <database_name> <user_name>')
  29. print('')
  30. print(' Other')
  31. print('')
  32. print(' '+__file__+' test_connection <database_name> <user_name> <password> [<hostname>]')
  33. print('')
  34. print('')
  35. print('NOTES:')
  36. print(' - this tool is intended to be used as \'postgres\' system user')
  37. print(' - default hostname is 127.0.0.1')
  38. print('')
  39. print('')
  40. def show_help_and_exit():
  41. usage()
  42. exit()
  43. def on_result_and_exit(error_code):
  44. if error_code==0:
  45. print('')
  46. print('')
  47. exit(0)
  48. print('ERRROR')
  49. print('')
  50. print('')
  51. exit(error_code)
  52. #######
  53. if len(sys.argv)<=1:
  54. show_help_and_exit()
  55. if sys.argv[1] not in actions:
  56. print('Unknown command %s'%sys.argv[1])
  57. show_help_and_exit()
  58. if sys.argv[1]=='help':
  59. show_help_and_exit()
  60. ########################################
  61. #
  62. # BELOW ARE STANDARD ACTIONS
  63. #
  64. ########################################
  65. print('')
  66. if sys.argv[1]=='create_user':
  67. if len(sys.argv)!=4:
  68. show_help_and_exit()
  69. user_name = sys.argv[2]
  70. user_pass = sys.argv[3]
  71. print('PG # CREATE USER')
  72. print('----------------')
  73. print('name: '+user_name)
  74. print('password: XXX')
  75. sql = '"CREATE USER %s WITH PASSWORD \'%s\';"' %(user_name, user_pass)
  76. result = os.system('echo %s|psql' % (sql))
  77. on_result_and_exit(result)
  78. if sys.argv[1]=='delete_user':
  79. if len(sys.argv)!=3:
  80. show_help_and_exit()
  81. user_name = sys.argv[2]
  82. print('PG # DELETE USER')
  83. print('------------------')
  84. print('name: '+user_name)
  85. print('')
  86. sql = '"DROP ROLE %s;"' %(user_name)
  87. result = os.system('echo %s|psql' % (sql))
  88. on_result_and_exit(result)
  89. if sys.argv[1]=='create_database':
  90. if len(sys.argv)!=3:
  91. show_help_and_exit()
  92. name = sys.argv[2]
  93. print('PG # CREATE DATABASE')
  94. print('--------------------')
  95. print('name: '+name)
  96. print('')
  97. sql = '"CREATE DATABASE %s;"' %(name)
  98. result = os.system('echo %s|psql' % (sql))
  99. on_result_and_exit(result)
  100. if sys.argv[1]=='delete_database':
  101. if len(sys.argv)!=4:
  102. show_help_and_exit()
  103. name = sys.argv[2]
  104. force = sys.argv[3]
  105. print('PG # DROP DATABASE')
  106. print('------------------')
  107. print('name: '+name)
  108. print('')
  109. if force!='--force':
  110. print('Add --force option in order to drop a database')
  111. on_result_and_exit(-1)
  112. sql = '"DROP DATABASE %s;"' %(name)
  113. result = os.system('echo %s|psql' % (sql))
  114. on_result_and_exit(result)
  115. if sys.argv[1]=='grant_all_privileges':
  116. if len(sys.argv)!=4:
  117. show_help_and_exit()
  118. database_name = sys.argv[2]
  119. user_name = sys.argv[3]
  120. print('PG # GRANT ALL PRIVILEGES ON DATABASE')
  121. print('-------------------------------------')
  122. print('database: '+database_name)
  123. print('user: '+user_name)
  124. print('')
  125. sql = '"GRANT ALL PRIVILEGES ON DATABASE %s to %s;"' % (database_name, user_name)
  126. result = os.system('echo %s|psql' % (sql))
  127. on_result_and_exit(result)
  128. if sys.argv[1]=='test_connection':
  129. if len(sys.argv)<5:
  130. show_help_and_exit()
  131. hostname = '127.0.0.1'
  132. database_name = sys.argv[2]
  133. user_name = sys.argv[3]
  134. password = sys.argv[4]
  135. if len(sys.argv)>=6:
  136. hostname = sys.argv[5]
  137. print('PG # CONNECT TO DATABASE')
  138. print('------------------------')
  139. print('server: '+hostname)
  140. print('database: '+database_name)
  141. print('username: '+user_name)
  142. print('')
  143. result = os.system('export PGPASSWORD=%s; echo "SELECT NOW();"|psql -h %s -U %s %s' % (password, hostname, user_name, database_name))
  144. on_result_and_exit(result)