postgres数据库重建流程

修改系统时间 分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC)

使用root用户查看系统时间 #date
查看硬件时间# hwclock --show
更新系统年月日# timedatectl set-time 2017-06-13
更新系统时分秒# timedatectl set-time 10:25:17
将硬件时钟调整为与系统时钟一致 timedatectl set-local-rtc 1
或者# hwclock --systohc --localtime
将日期写入CMOS clock –w
修改时区:(将Asia/shanghai-上海时区写入当前时区)#cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
提示是否覆盖,输入Y回车,

新建数据库修正编码方式(主库和备库同时需要)

CREATE DATABASE fintest
WITH
OWNER = postgres
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.utf8'
LC_CTYPE = 'en_US.utf8'
TABLESPACE = pg_default
TEMPLATE = template0
CONNECTION LIMIT = -1;

从源数据库导出测试数据

pg_dump -h 127.0.0.1 -t dt_user -t dt_dep -t dt_dev -t st_device -t fin_crowd_detail -t fin_crowd_master -t fin_rule_acdep -t fin_rule_master -t fin_device -t fin_card -t fin_account -t fin_sub_account -t fin_device_control -t fin_device_flow -t fin_trad_type -t fin_day_total -t fin_meal_total -t fin_merchant_account -t fin_tt_trad_type -t fin_tt_event_lend -a fintest --inserts >/var/lib/pgsql/fin_pg_20180206.sql

或者
pg_dump -h 127.0.0.1 -t dt_user -t dt_dep -t dt_dev -t st_device -t fin_crowd_detail -t fin_crowd_master -t fin_rule_acdep -t fin_rule_master -t fin_device -t fin_card -t fin_account -t fin_sub_account -t fin_device_control -t fin_device_flow -t fin_trad_type -t fin_day_total -t fin_meal_total -t fin_merchant_account -t fin_tt_trad_type -t fin_tt_event_lend -a fintest >/var/lib/pgsql/fin_pg_20180206.sql

pg_dump -h 127.0.0.1 -t app_account_role -t app_account_info -a postgres >/var/lib/pg_account_app_20180207.sql
pg_dump -h 127.0.0.1 -t basic_account_info -a postgres >/var/lib/pg_account_basic_20180207.sql
pg_dump -h localhost acc_data >/var/lib/account_all_20180207.sql

导入到目标数据库(主库和备库同时需要)

导入基础数据库(主库和备库同时需要)

psql -h localhost -d fintest -U postgres -f /var/fin_pg20180201.sql

psql -h localhost -d acc_data -U postgres -f /var/lib/pg_account_app_20180207.sql
psql -h localhost -d acc_data -U postgres -f /var/lib/pg_account_basic_20180207.sql

测试数据导入表统计(主库和备库同时需要)

drop table if exists insert_table;
create table insert_table (id serial,tablename varchar(100),flg int default 0);
insert into insert_table(tablename)
values ('dt_user'),('dt_dep'),('dt_dev'),('st_device'),('fin_crowd_detail'),('fin_crowd_master'),('fin_rule_acdep'),('fin_rule_master'),('fin_device'),('fin_card'),('fin_account'),('fin_sub_account'),('fin_device_control'),('fin_device_flow'),('fin_day_total'),('fin_meal_total'),('fin_merchant_account'),
('fin_tt_trad_type'), ('fin_tt_event_lend');

测试数据导入错误删除数据(主库和备库同时需要)

CREATE OR REPLACE FUNCTION public.cursor_demo(
)
RETURNS refcursor
LANGUAGE 'plpgsql'
COST 100.0
AS $function$
declare
unbound_refcursor refcursor;
t_accid varchar(100);
sql text;
begin
open unbound_refcursor for execute 'select tablename from insert_table';
loop
fetch unbound_refcursor into t_accid;
if found then
sql := 'truncate table ' || t_accid;
EXECUTE sql;
raise notice '%',sql;
else
exit;
end if;
end loop;
close unbound_refcursor;
raise notice 'the end of msg...';
return unbound_refcursor;
exception when others then
raise exception 'error-----(%)',sqlerrm;
end;
$function$;
ALTER FUNCTION public.cursor_demo()
OWNER TO postgres;

select cursor_demo();

导入测试数据库(主库和备库同时需要)

psql -h localhost -d fintest -U postgres -f /var/fin_Pg_20180206.sql

导入数据fin_bill_date(主库和备库同时需要)

insert into fin_bill_date(id,current_day,next_day,off_set)
values (1,'2017-12-11','2017-12-12',120);

重置序列值(主库和备库同时需要)

select max(xh) from dt_card;
alter sequence dt_card_xh_seq restart 12345 ;

-- dt_card_xh_seq;
dt_dev_xh_seq;
fin_crowd_master_crowd_id_seq;
fin_rule_acdep_id_seq;
fin_rule_master_id_seq;
fin_sub_account_sub_id_seq;
fin_day_total_id_seq;
fin_meal_total_id_seq;
fin_tt_event_lend_id_seq;

验证数据正确性

select * from pg_tables a ,pg_class b where a.tablename =b.relname and reltuples >0 AND a.schemaname ='public';

drop table if exists pg_tables_public;
create table pg_tables_public as
select * from pg_tables a where a.schemaname ='public';

create table pg_tables_public_reltupes as
select * from pg_tables a ,pg_class b where a.tablename =b.relname and reltuples >0 AND a.schemaname ='public';

select tablename from pg_tables_public_reltupes;

SELECT relname, reltuples from pg_tables a ,pg_class b
WHERE a.tablename= b.relname and tablename NOT LIKE 'pg%' AND tablename NOT LIKE 'sql_%' and reltuples>0
ORDER BY tablename ;

配置pglogical

添加主键(主库和备库同时需要)

alter table fin_device_control add primary key (device_id);
alter table fin_log_history add primary key (id);
alter table fin_merchant_device add column id int primary key ;
alter table fin_sub_expired add column id int primary key ;
alter table fin_identity_type add primary key (id);
alter table fin_tt_cred add primary key (cred_id);
alter table fin_tt_key add column id int ;
update fin_tt_key set id =1;
alter table fin_tt_key add primary key (id);

插入表格到默认集(仅仅主库需要)

select pglogical.replication_set_add_table('default','fin_account');
select pglogical.replication_set_add_table('default','fin_account_card_synch');
select pglogical.replication_set_add_table('default','fin_account_unit');
select pglogical.replication_set_add_table('default','fin_acdep_dev');
select pglogical.replication_set_add_table('default','fin_acdep_mode');
select pglogical.replication_set_add_table('default','fin_bank_trans_flow');
select pglogical.replication_set_add_table('default','fin_card');
select pglogical.replication_set_add_table('default','fin_card_cardslot');
select pglogical.replication_set_add_table('default','fin_card_dynamic');
select pglogical.replication_set_add_table('default','fin_crowd_detail');
select pglogical.replication_set_add_table('default','fin_crowd_master');
select pglogical.replication_set_add_table('default','fin_crowd_rule');
select pglogical.replication_set_add_table('default','fin_dep_amt');
select pglogical.replication_set_add_table('default','fin_day_total');
select pglogical.replication_set_add_table('default','fin_deposit_set');
select pglogical.replication_set_add_table('default','fin_destroy_account');
select pglogical.replication_set_add_table('default','fin_device');

select pglogical.replication_set_add_table('default','fin_device_control');
select pglogical.replication_set_add_table('default','fin_device_dynamic');
select pglogical.replication_set_add_table('default','fin_device_flow');
select pglogical.replication_set_add_table('default','fin_fee_set');
select pglogical.replication_set_add_table('default','fin_global_authorization');
select pglogical.replication_set_add_table('default','fin_identity_set');
select pglogical.replication_set_add_table('default','fin_log');
select pglogical.replication_set_add_table('default','fin_log_history');
select pglogical.replication_set_add_table('default','fin_meal');
select pglogical.replication_set_add_table('default','fin_meal_total');
select pglogical.replication_set_add_table('default','fin_merchant_account');
select pglogical.replication_set_add_table('default','fin_merchant_dep');
select pglogical.replication_set_add_table('default','fin_merchant_device');
select pglogical.replication_set_add_table('default','fin_merchant_power');
select pglogical.replication_set_add_table('default','fin_offline_error_record');
select pglogical.replication_set_add_table('default','fin_open_account');

select pglogical.replication_set_add_table('default','fin_operator_ip');
select pglogical.replication_set_add_table('default','fin_pos_real_total');
select pglogical.replication_set_add_table('default','fin_rule_acdep');
select pglogical.replication_set_add_table('default','fin_rule_master');
select pglogical.replication_set_add_table('default','fin_sub_account');
select pglogical.replication_set_add_table('default','fin_sub_expired');
select pglogical.replication_set_add_table('default','fin_sub_flow');
select pglogical.replication_set_add_table('default','fin_sub_import_result');
select pglogical.replication_set_add_table('default','fin_sub_master');
select pglogical.replication_set_add_table('default','fin_sub_slave');

select pglogical.replication_set_add_table('default','fin_subject_amt');
select pglogical.replication_set_add_table('default','fin_subject_amt_bak');
select pglogical.replication_set_add_table('default','fin_system_set');
select pglogical.replication_set_add_table('default','fin_trans_log');
select pglogical.replication_set_add_table('default','fin_tt_card_type');
select pglogical.replication_set_add_table('default','fin_tt_cardholder_payment');
select pglogical.replication_set_add_table('default','fin_tt_cardreader');
select pglogical.replication_set_add_table('default','fin_tt_cardslot');
select pglogical.replication_set_add_table('default','fin_tt_cred');
select pglogical.replication_set_add_table('default','fin_tt_event_lend');

更新统计信息

analyse;

查看是否插入

insert into fin_system_set(sys_name,current_day,next_day,is_auto_bill,auto_bill_time,sys_state,month_start_date,fixed_setting,sys_account,sys_card_serial,sys_merchant,operator,client,sj,off_set,time_out)
values('在线消费系统',cast('2017-12-01' as date),cast('2017-12-02' as date),1,'00:00',1,10,30,1,1,1,'sys','127.0.0.1',now(),120,300);

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,284评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,115评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,614评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,671评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,699评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,562评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,309评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,223评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,668评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,859评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,981评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,705评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,310评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,904评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,023评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,146评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,933评论 2 355

推荐阅读更多精彩内容

  • 1.简介 数据存储有哪些方式?电子表格,纸质文件,数据库。 那么究竟什么是关系型数据库? 目前对数据库的分类主要是...
    乔震阅读 1,716评论 0 2
  • 非常感谢简书给我带来的快乐,简书好像一位朋友,带我品尝文学的味道,好像回到校园,重回老师和同学的身边
    媛媛8888阅读 183评论 0 0
  • 女为悦己者容,花为知己者香 ——毛慧 郁金香
    Snow_宁博阅读 231评论 0 0
  • 1,带着觉知讲话 今天跟老公,父母,老师说的每一句话都是在脑袋里想好了,才说出来的。如果,不知道怎么说,宁肯不说,...
    Lulu轻悦阅读 184评论 0 0
  • 【论语导读】2.21何不为政 《论语》为政第二篇第二十一章2.21,原文,或谓孔子曰:“子奚不为政?”子曰:“书云...
    赛德传播阅读 605评论 0 1