#!/usr/bin/env pythonpython
import os,sys from datetimesql
import * arg = sys.argv try: db = arg[1] except: print('ERROR: Please enter DB name!!!') exit() date_start = date(2016, 12, 31) sql_get_ptables = "select schemaname||'.'||tablename from pg_partitions where partitiontype='range' group by schemaname,tablename" ptables_obj = os.popen('psql -h 127.0.0.1 -p 5432 -t -d %s -c "%s"'%(db,sql_get_ptables)) ptables = ptables_obj.readlines() ptables.pop() print(ptables) for i in ptables: j = i.strip().lstrip().rstrip('\n') date_start = date(2016,12,31) for k in range(1,732): date_end = date_start+timedelta(1) str_start = date_start.strftime('%Y-%m-%d') str_end = date_end.strftime('%Y-%m-%d') sql = "alter table %s add partition start(date '%s') inclusive end(date '%s') exclusive"%(j,str_start,str_end) print(sql) os.system('psql -h 127.0.0.1 -p 5432 -t -d %s -c "%s"'%(db,sql)) date_start = date_endip