经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » PostgreSQL » 查看文章
python 两个数据库postgresql对比
来源:jb51  时间:2019/10/21 12:59:32  对本文有异议

这篇文章主要介绍了python 两个数据库postgresql对比,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

比较两个postgresql数据库,原理 比较数据库中各表的大小

1. 数据库查询语句

2. python字典比较

  1. import psycopg2
  2. import sys
  3.  
  4.  
  5. class PdbModel:
  6. def __init__(self, host, dbname, username='postgres', password='postgres', port='5432'):
  7. self.host = host
  8. self.dbname = dbname
  9. self.username = username
  10. self.password = password
  11. self.port = port
  12.  
  13. self.conn = None
  14. self.cursor = None
  15. self.init_db()
  16.  
  17. def init_db(self):
  18. try:
  19. self.conn = psycopg2.connect(database=self.dbname, user=self.username, password=self.password,
  20. host=self.host,
  21. port="5432")
  22. self.cursor = self.conn.cursor()
  23.  
  24. except Exception, e:
  25. error_out_print("Error: connection to db %s : %s failed. check need" % (self.host, self.dbname))
  26. print e
  27. sys.exit(-1)
  28.  
  29. def execute_sql(self, sql, is_exist=True):
  30. """
  31. execute sql and return rows
  32. :param sql:
  33. :return:
  34. results of execute sql
  35. """
  36. try:
  37. standout_print('command sql : %s' % sql)
  38. self.cursor.execute(sql)
  39. rows = self.cursor.fetchall()
  40. return rows
  41.  
  42. except Exception, e:
  43. self.conn.rollback()
  44. error_out_print("Failed: failed execute sql [%s]" % sql)
  45. error_out_print(e)
  46. if is_exist:
  47. self.close()
  48. sys.exit(-1)
  49. else:
  50. return None
  51.  
  52. def get_tables_size(self):
  53. """
  54. select table_schema || '.' || table_name as table_full_name , pg_size_pretty(pg_total_relation_size('"'||table_schema||'"."'||table_name||'"')) as size
  55. from information_schema.tables
  56. order by pg_total_relation_size('"'||table_schema||'"."'||table_name||'"') DESC
  57.  
  58. :return:
  59. """
  60. standout_print("get the size of tables in db [%s]." % self.dbname)
  61. sql = """
  62. select table_schema || '.' || table_name as table_full_name , pg_size_pretty(pg_total_relation_size('"'||table_schema||'"."'||table_name||'"')) as size
  63. from information_schema.tables
  64. order by pg_total_relation_size('"'||table_schema||'"."'||table_name||'"') DESC
  65. """
  66. rows = self.execute_sql(sql)
  67. table_size_dic = {}
  68. for row in rows:
  69. table_name = row[0]
  70. table_size = row[1]
  71. table_size_dic[table_name] = table_size
  72. return table_size_dic
  73.  
  74.  
  75. def standout_print(info):
  76. sys.stdout.write("Info: %s " % info)
  77. sys.stdout.flush()
  78.  
  79.  
  80. def error_out_print(info):
  81. sys.stderr.write("Error: %s " % info)
  82. sys.stderr.flush()
  83.  
  84.  
  85. if __name__ == '__main__':
  86. db1 = ''
  87. db2 = ''
  88. host = "172.16.101.92"
  89. db_model1 = PdbModel(host, db1)
  90. db_model2 = PdbModel(host, db2)
  91. table_size_dic1 = db_model1.get_tables_size()
  92. table_size_dic2 = db_model2.get_tables_size()
  93. import pprint
  94.  
  95. # pprint.pprint(table_size_dic1)
  96. # pprint.pprint(table_size_dic2)
  97. print cmp(table_size_dic1, table_size_dic2)
  98. is_equal = cmp(table_size_dic1, table_size_dic2)
  99. different_table_size_dic = {}
  100. if is_equal == 0:
  101. print "these tables in two database are same."
  102. else:
  103. keys1 = table_size_dic1.keys()
  104. keys2 = table_size_dic2.keys()
  105.  
  106. for key in keys1:
  107. value1 = table_size_dic1.get(key)
  108. value2 = table_size_dic2.get(key)
  109. if cmp(value1, value2) != 0:
  110. different_table_size_dic[key] = (value1,value2)
  111.  
  112. pprint.pprint(different_table_size_dic)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号