python 兩個數(shù)據(jù)庫postgresql對比
這篇文章主要介紹了python 兩個數(shù)據(jù)庫postgresql對比,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
比較兩個postgresql數(shù)據(jù)庫,原理 比較數(shù)據(jù)庫中各表的大小
1. 數(shù)據(jù)庫查詢語句
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)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
對Python生成漢字字庫文字,以及轉(zhuǎn)換為文字圖片的實例詳解
今天小編就為大家分享一篇對Python生成漢字字庫文字,以及轉(zhuǎn)換為文字圖片的實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
python實現(xiàn)同一局域網(wǎng)下傳輸圖片
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)同一局域網(wǎng)下傳輸圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03
python爬蟲之利用Selenium+Requests爬取拉勾網(wǎng)
這篇文章主要介紹了python爬蟲之利用Selenium+Requests爬取拉勾網(wǎng),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python爬蟲的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04
使用Python編寫一個最基礎(chǔ)的代碼解釋器的要點解析
Python、Ruby等語言代碼就是在解釋器程序中一行行被解釋為機器碼同步執(zhí)行的,而如果使用Python編寫解釋器的話則可以把目標(biāo)代碼解釋為Python代碼再進(jìn)行解釋執(zhí)行,這里我們就來看一下使用Python編寫一個最基礎(chǔ)的代碼解釋器的要點解析:2016-07-07
python機器學(xué)習(xí)使數(shù)據(jù)更鮮活的可視化工具Pandas_Alive
今天我分享大家一款非常棒的動畫可視化工具:Pandas_Alive,它以?matplotlib?繪圖為后端,不僅可以創(chuàng)建出令人驚嘆的動畫可視化,而且使用方法非常簡單。本文詳情如下2021-11-11
配置 Pycharm 默認(rèn) Test runner 的圖文教程
今天小編就為大家分享一篇配置 Pycharm 默認(rèn) Test runner 的圖文教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11

