python 两个数据库postgresql对比
作者:百变小超 发布时间:2024-01-22 16:29:36
标签:python,数据库,postgresql,对比
这篇文章主要介绍了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)
来源:https://www.cnblogs.com/dasheng-maritime/p/7676557.html
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 几乎所有的Python 2程序都需要一些修改才能正常地运行在Python 3的环境下。为了简化这个转换过程,Python 3自带了一个叫做2
- 前端的小伙伴们在babel等的加持下,已经可以愉快的使用es6来写代码了。然后对于服务端的nodejs就有点坑爹了,虽然原生支持了es6,但
- 前端实现用ligerUI实现分页,感觉用框架确实简单,闲着无聊,模拟着liger的分页界面实现了一遍(只要是功能,样式什么无视)
- 实例1、取得MYSQL版本# -*- coding: UTF-8 -*-#安装MYSQL DB for pythonimport MySQL
- 我就废话不多说了,大家还是直接看代码吧~func main() { var a chan string a =mak
- 前言网上的做法基本都是下面的代码return HttpResponseForbidden()试了一下,效果一般,没有异常页面显示,最终显示的
- 前言fixture是在测试函数运行前后,由pytest执行的外壳函数。fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中
- 下面是完整代码://记录搜索引擎爬行记录 $searchbot = get_naps_bot(); if ($searchbot) { $t
- 百度AI接口的调用方法不必多介绍。官网地址人流量统计新建AipBodyAnalysisfrom aip import AipBodyAnal
- 一、问题开发中遇到将其它数据库数据插入到mysql数据库表中一直会报类似如下错误:Incorrect string value: '
- 背景最近在搭一个新项目的架子,在生产环境中,为了能实时的监控程序的运行状态,少不了逻辑执行时间长度的统计。时间统计这个功能实现的期望有下面几
- 0.目录1.前言2.安装python3.使用pip下载、安装包3.1 安装Scrapy3.2 安装PyQt3.3 同时安装多个包3.4 pi
- 在使用Ajax过程中,有时候总会遇到一些难题,浏览器兼容、编码、IE下的特殊处理等等,偶尔会搞的人头昏脑胀哭笑不得,这里列一些小贴士,或许有
- 先给大家讲解一下什么是kalikali是一种Linux系统,kali是专门用来渗透的,他是由back track系统演化而来的,后面结合了l
- 前言对于我这种小白来说,本地环境搭建常规的操作一向是直接去go官网下载go安装包,本机进行安装,然后配置相应的GOROOT和GOPATH,再
- testify在团队里推行单元测试的时候,有一个反对的意见是:写单元测试耗时太多。且不论这个意见对错,单元测试确实不应该太费时间。这时候,一
- 一 安装oracle数据库成功之后,会显示相关的数据库信息: 全局数据库名:oracle 系统标识符(SID):oracle 服务器参数文件
- 电脑配置:Windows10,64位操作系统一、Anaconda的介绍Anaconda指的是一个开源的Python发行版本,其包含了cond
- 自从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大开发工具,极小的学习曲线,让我对这个平台产生了浓厚的兴趣,在工作和学习中也
- 安装pdfminer 库windows 下安装pdfminer3kpip install pdfminer3kLiunx 下安装pdfmin