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中字符串的处理技巧分享
Sep 17 Python
Python制作简易注册登录系统
Dec 15 Python
python实现字典(dict)和字符串(string)的相互转换方法
Mar 01 Python
python+matplotlib实现鼠标移动三角形高亮及索引显示
Jan 15 Python
python顺序的读取文件夹下名称有序的文件方法
Jul 11 Python
selenium+python截图不成功的解决方法
Jan 30 Python
python 获取毫秒数,计算调用时长的方法
Feb 20 Python
Django urls.py重构及参数传递详解
Jul 23 Python
Django使用 Bootstrap 样式修改书籍列表过程解析
Aug 09 Python
django中media媒体路径设置的步骤
Nov 15 Python
pytorch下大型数据集(大型图片)的导入方式
Jan 08 Python
Selenium环境变量配置(火狐浏览器)及验证实现
Dec 07 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
smarty中post用法实例
2014/11/28 PHP
PHP编程开发怎么提高编程效率 提高PHP编程技术
2015/11/09 PHP
Python中使用django form表单验证的方法
2017/01/16 PHP
php中请求url的五种方法总结
2017/07/13 PHP
CSS心形加载的动画源码的实现
2021/03/09 HTML / CSS
Array对象方法参考
2006/10/03 Javascript
Extjs列表详细信息窗口新建后自动加载解决方法
2010/04/02 Javascript
Javascript中Event属性搜集整理
2013/09/17 Javascript
js获取判断上传文件后缀名的示例代码
2014/02/19 Javascript
在JavaScript中使用timer示例
2014/05/08 Javascript
jquery中map函数与each函数的区别实例介绍
2014/06/23 Javascript
详解JS面向对象编程
2016/01/24 Javascript
jQuery xml字符串的解析、读取及查找方法
2016/03/01 Javascript
jQuery中on绑定事件后引发的事件冒泡问题如何解决
2016/05/25 Javascript
提高JavaScript执行效率的23个实用技巧
2017/03/01 Javascript
关于Vue背景图打包之后访问路径错误问题的解决
2017/11/03 Javascript
Vue与Node.js通过socket.io通信的示例代码
2018/07/25 Javascript
微信小程序如何获取手机验证码
2018/11/04 Javascript
python实现博客文章爬虫示例
2014/02/26 Python
跟老齐学Python之??碌某?? target=
2014/09/12 Python
在Django框架中运行Python应用全攻略
2015/07/17 Python
python中pandas.DataFrame的简单操作方法(创建、索引、增添与删除)
2017/03/12 Python
使用python实现名片管理系统
2020/06/18 Python
Python web框架(django,flask)实现mysql数据库读写分离的示例
2020/11/18 Python
css3翻牌翻数字的示例代码
2020/02/07 HTML / CSS
探究 canvas 绘图中撤销(undo)功能的实现方式详解
2018/05/17 HTML / CSS
美国孕妇装购物网站:Motherhood Maternity
2019/09/22 全球购物
教师新年寄语
2014/04/03 职场文书
经典的毕业生自荐信范文
2014/04/14 职场文书
中秋晚会策划方案
2014/06/12 职场文书
乡镇干部先进性教育活动个人整改措施
2014/09/16 职场文书
颐和园导游词
2015/01/30 职场文书
2015年女生节活动总结
2015/02/27 职场文书
物业保安辞职信
2015/05/12 职场文书
MybatisPlus EntityWrapper如何自定义SQL
2022/03/22 Java/Android
一起来看看Vue的核心原理剖析
2022/03/24 Vue.js