python 两个数据库postgresql对比


Posted in Python onOctober 21, 2019

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

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

1. 数据库查询语句

2. python字典比较

import psycopg2
import sys


class PdbModel:
  def __init__(self, host, dbname, username='postgres', password='postgres', port='5432'):
    self.host = host
    self.dbname = dbname
    self.username = username
    self.password = password
    self.port = port

    self.conn = None
    self.cursor = None
    self.init_db()

  def init_db(self):
    try:
      self.conn = psycopg2.connect(database=self.dbname, user=self.username, password=self.password,
                     host=self.host,
                     port="5432")
      self.cursor = self.conn.cursor()

    except Exception, e:
      error_out_print("Error: connection to db %s : %s failed. check need" % (self.host, self.dbname))
      print e
      sys.exit(-1)

  def execute_sql(self, sql, is_exist=True):
    """
      execute sql and return rows
    :param sql:
    :return:
      results of execute sql
    """
    try:
      standout_print('command sql : %s' % sql)
      self.cursor.execute(sql)
      rows = self.cursor.fetchall()
      return rows

    except Exception, e:
      self.conn.rollback()
      error_out_print("Failed: failed execute sql [%s]" % sql)
      error_out_print(e)
      if is_exist:
        self.close()
        sys.exit(-1)
      else:
        return None

  def get_tables_size(self):
    """
    select table_schema || '.' || table_name as table_full_name , pg_size_pretty(pg_total_relation_size('"'||table_schema||'"."'||table_name||'"')) as size
from information_schema.tables
order by pg_total_relation_size('"'||table_schema||'"."'||table_name||'"') DESC

    :return:
    """
    standout_print("get the size of tables in db [%s]." % self.dbname)
    sql = """ 
    select table_schema || '.' || table_name as table_full_name , pg_size_pretty(pg_total_relation_size('"'||table_schema||'"."'||table_name||'"')) as size
from information_schema.tables
order by pg_total_relation_size('"'||table_schema||'"."'||table_name||'"') DESC
    """
    rows = self.execute_sql(sql)
    table_size_dic = {}
    for row in rows:
      table_name = row[0]
      table_size = row[1]
      table_size_dic[table_name] = table_size
    return table_size_dic


def standout_print(info):
  sys.stdout.write("Info: %s " % info)
  sys.stdout.flush()


def error_out_print(info):
  sys.stderr.write("Error: %s " % info)
  sys.stderr.flush()


if __name__ == '__main__':
  db1 = ''
  db2 = ''
  host = "172.16.101.92"
  db_model1 = PdbModel(host, db1)
  db_model2 = PdbModel(host, db2)
  table_size_dic1 = db_model1.get_tables_size()
  table_size_dic2 = db_model2.get_tables_size()
  import pprint

  # pprint.pprint(table_size_dic1)
  # pprint.pprint(table_size_dic2)
  print cmp(table_size_dic1, table_size_dic2)
  is_equal = cmp(table_size_dic1, table_size_dic2)
  different_table_size_dic = {}
  if is_equal == 0:
    print "these tables in two database are same."
  else:
    keys1 = table_size_dic1.keys()
    keys2 = table_size_dic2.keys()

    for key in keys1:
      value1 = table_size_dic1.get(key)
      value2 = table_size_dic2.get(key)
      if cmp(value1, value2) != 0:
        different_table_size_dic[key] = (value1,value2)

  pprint.pprint(different_table_size_dic)

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

Python 相关文章推荐
Python完全新手教程
Feb 08 Python
python实现从web抓取文档的方法
Sep 26 Python
使用Python的PIL模块来进行图片对比
Feb 18 Python
python抓取网页中链接的静态图片
Jan 29 Python
使用k8s部署Django项目的方法步骤
Jan 14 Python
PyQt QListWidget修改列表项item的行高方法
Jun 20 Python
python使用百度文字识别功能方法详解
Jul 23 Python
python3 使用traceback定位异常实例
Mar 09 Python
Python中Selenium模块的使用详解
Oct 09 Python
Python 列表反转显示的四种方法
Nov 16 Python
python3中for循环踩过的坑记录
Dec 14 Python
python实现简单反弹球游戏
Apr 12 Python
python多进程(加入进程池)操作常见案例
Oct 21 #Python
Python实现字符串中某个字母的替代功能
Oct 21 #Python
基于Python实现船舶的MMSI的获取(推荐)
Oct 21 #Python
基于Python解密仿射密码
Oct 21 #Python
python多继承(钻石继承)问题和解决方法简单示例
Oct 21 #Python
python超时重新请求解决方案
Oct 21 #Python
详解python中*号的用法
Oct 21 #Python
You might like
咖啡风味 世界咖啡主要分布分布 咖啡的生长要求
2021/03/06 新手入门
shell脚本作为保证PHP脚本不挂掉的守护进程实例分享
2013/07/15 PHP
php 获取SWF动画截图示例代码
2014/02/10 PHP
Thinkphp3.2.3整合phpqrcode生成带logo的二维码
2016/07/21 PHP
PHP实现的简单组词算法示例
2018/04/10 PHP
laravel unique验证、确认密码confirmed验证以及密码修改验证的方法
2019/10/16 PHP
PHP高并发和大流量解决方案整理
2019/12/24 PHP
jquery 简短右键菜单 多浏览器兼容
2010/01/01 Javascript
jQuery向上遍历DOM树之parents(),parent(),closest()之间的区别
2013/12/02 Javascript
js图片自动轮播代码分享(js图片轮播)
2014/05/06 Javascript
JS组件中bootstrap multiselect两大组件较量
2016/01/26 Javascript
JS模拟简易滚动条效果代码(附demo源码)
2016/04/05 Javascript
arcgis for js 修改infowindow样式的方法
2016/11/02 Javascript
Vue.js实现按钮的动态绑定效果及实现代码
2017/08/21 Javascript
js Element Traversal规范中的元素遍历方法
2018/04/19 Javascript
webpack4.x开发环境配置详解
2018/08/04 Javascript
微信实现自动跳转到用其他浏览器打开指定APP下载
2019/02/15 Javascript
vue-drawer-layout实现手势滑出菜单栏
2020/11/19 Vue.js
Python re模块介绍
2014/11/30 Python
详解使用python的logging模块在stdout输出的两种方法
2017/05/17 Python
python实现随机调用一个浏览器打开网页
2018/04/21 Python
详解python中递归函数
2019/04/16 Python
python通过paramiko复制远程文件及文件目录到本地
2019/04/30 Python
python集成开发环境配置(pycharm)
2020/02/14 Python
python2.7使用scapy发送syn实例
2020/05/05 Python
使用HTML5 Canvas API绘制弧线的教程
2016/03/22 HTML / CSS
银行会计财务工作个人的自我评价
2013/10/29 职场文书
如何写毕业求职自荐信
2013/11/06 职场文书
《美丽的田园》教学反思
2014/03/01 职场文书
史学专业毕业生求职信
2014/05/09 职场文书
群众路线教育实践活动批评与自我批评
2014/09/15 职场文书
蛋糕店创业计划书范文
2014/09/21 职场文书
成都人事代理协议书
2014/10/25 职场文书
2014年妇女工作总结
2014/12/06 职场文书
Golang 获取文件md5校验的方法以及效率对比
2021/05/08 Golang
Java中的Kotlin 内部类原理
2022/06/16 Java/Android