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中的jquery PyQuery库使用小结
May 13 Python
python创建列表并给列表赋初始值的方法
Jul 28 Python
Python实现获取命令行输出结果的方法
Jun 10 Python
python爬虫爬取某站上海租房图片
Feb 04 Python
Django自定义manage命令实例代码
Feb 11 Python
python处理两种分隔符的数据集方法
Dec 12 Python
python实现坦克大战游戏 附详细注释
Mar 27 Python
django中forms组件的使用与注意
Jul 08 Python
pyenv与virtualenv安装实现python多版本多项目管理
Aug 17 Python
python使用itchat模块给心爱的人每天发天气预报
Nov 25 Python
python实现飞机大战游戏(pygame版)
Oct 26 Python
Python装饰器结合递归原理解析
Jul 02 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
一步一步学习PHP(8) php 数组
2010/03/05 PHP
解析php中const与define的应用区别
2013/06/18 PHP
php实现文件下载功能的几个代码分享
2014/05/10 PHP
详解PHP原生DOM对象操作XML的方法
2016/10/17 PHP
ThinkPHP框架表单验证操作方法
2017/07/19 PHP
laravel 5.5 关闭token的3种实现方式
2019/10/24 PHP
JQuery打造PHP的AJAX表单提交实例
2009/11/03 Javascript
你需要知道的10个最佳javascript开发实践小结
2012/04/15 Javascript
基于jQuery通过jQuery.form.js插件使用ajax提交form表单
2015/08/17 Javascript
js格式化时间的方法
2015/12/18 Javascript
基于Node.js的JavaScript项目构建工具gulp的使用教程
2016/05/20 Javascript
JavaScript实现广告弹窗效果
2016/08/09 Javascript
Js获取当前日期时间及格式化代码
2016/09/17 Javascript
JQuery异步提交表单与文件上传功能示例
2017/01/12 Javascript
Mongoose经常返回e11000 error的原因分析
2017/03/29 Javascript
Vue 2.0学习笔记之使用$refs访问Vue中的DOM
2017/12/19 Javascript
Vue组件通信之Bus的具体使用
2017/12/28 Javascript
微信小程序如何实现精确的日期时间选择器
2020/01/21 Javascript
[06:16]第十四期-国士无双绝地翻盘之撼地神牛
2014/06/24 DOTA
Python生成随机数的方法
2014/01/14 Python
在Python 3中实现类型检查器的简单方法
2015/07/03 Python
Python网络爬虫与信息提取(实例讲解)
2017/08/29 Python
pandas 空的dataframe 插入列名的示例
2018/10/30 Python
利用Tensorboard绘制网络识别准确率和loss曲线实例
2020/02/15 Python
Python内建序列通用操作6种实现方法
2020/03/26 Python
如何在scrapy中捕获并处理各种异常
2020/09/28 Python
十佳家长事迹材料
2014/08/26 职场文书
2014年外贸业务员工作总结
2014/12/11 职场文书
大学辅导员述职报告
2015/01/10 职场文书
杭州西湖英语导游词
2015/02/03 职场文书
幼儿园小朋友毕业感言
2015/07/30 职场文书
《西门豹》教学反思
2016/02/23 职场文书
selenium.webdriver中add_argument方法常用参数表
2021/04/08 Python
在K8s上部署Redis集群的方法步骤
2021/04/27 Redis
浅谈redis整数集为什么不能降级
2021/07/25 Redis
Python使用pyecharts控件绘制图表
2022/06/05 Python