圆月山庄资源网 Design By www.vgjia.com

这篇文章主要介绍了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,数据库,postgresql,对比

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?