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列表操作实例
Jan 14 Python
Python多进程同步简单实现代码
Apr 27 Python
Python简单实现控制电脑的方法
Jan 22 Python
python中的内置函数max()和min()及mas()函数的高级用法
Mar 29 Python
python DataFrame 修改列的顺序实例
Apr 10 Python
python线程池threadpool使用篇
Apr 27 Python
cmd运行python文件时对结果进行保存的方法
May 16 Python
django 删除数据库表后重新同步的方法
May 27 Python
python 不以科学计数法输出的方法
Jul 16 Python
Python3.4学习笔记之类型判断,异常处理,终止程序操作小结
Mar 01 Python
Python命名空间namespace及作用域原理解析
Jun 05 Python
python 实现Harris角点检测算法
Dec 11 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
ThinkPHP中获取指定日期后工作日的具体日期方法
2018/10/14 PHP
Laravel框架集成UEditor编辑器的方法图文与实例详解
2019/04/17 PHP
javascript window.confirm确认 取消对话框实现代码小结
2012/10/21 Javascript
Jquery validation remote 验证的缓存问题解决方法
2014/03/25 Javascript
如何让你的Lightbox支持滚轮缩放及Base64图片
2014/12/04 Javascript
JavaScript驾驭网页-DOM
2016/03/24 Javascript
JS简单验证上传文件类型的方法
2017/04/17 Javascript
JavaScript门面模式详解
2017/10/19 Javascript
axios发送post请求springMVC接收不到参数的解决方法
2018/03/05 Javascript
JS实现盒子跟着鼠标移动及键盘方向键控制盒子移动效果示例
2019/01/29 Javascript
原生JS实现多条件筛选
2020/08/19 Javascript
vue操作dom元素的3种方法示例
2020/09/20 Javascript
python分割和拼接字符串
2013/11/01 Python
Python自动扫雷实现方法
2015/07/25 Python
Python合并字典键值并去除重复元素的实例
2016/12/18 Python
Python 加密的实例详解
2017/10/09 Python
使用Python进行AES加密和解密的示例代码
2018/02/02 Python
Python中的函数作用域
2018/05/07 Python
TensorFlow实现卷积神经网络
2018/05/24 Python
Empty test suite.(PyCharm程序运行错误的解决方法)
2018/11/30 Python
python随机在一张图像上截取任意大小图片的方法
2019/01/24 Python
Django Rest framework解析器和渲染器详解
2019/07/25 Python
关于pandas的离散化,面元划分详解
2019/11/22 Python
卸载tensorflow-cpu重装tensorflow-gpu操作
2020/06/23 Python
3分钟看懂Python后端必须知道的Django的信号机制
2020/07/26 Python
Python包资源下载路径报404解决方案
2020/11/05 Python
CSS3实现王者荣耀匹配人员加载页面的方法
2019/04/16 HTML / CSS
美国波西米亚风格服装品牌:Show Me Your Mumu
2018/01/05 全球购物
TCP协议通讯的过程和步骤是什么
2015/10/18 面试题
接口的多继承会带来哪些问题
2015/08/17 面试题
中学生学习生活的自我评价
2013/10/26 职场文书
马云北大演讲完整版:真心话,什么才是阿里的核心竞争力?
2014/04/04 职场文书
经费申请报告
2015/05/15 职场文书
2015年党小组工作总结
2015/05/26 职场文书
Python进行区间取值案例讲解
2021/08/02 Python
win11如何查看端口是否被占用? Win11查看端口是否占用的技巧
2022/04/05 数码科技