【数据库】postgres基本操作

1.pgadmin工具

2.增删改查

​ 插入批量记录:

​ 1)生成insertDeviceChannels

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
create or replace function insertDeviceChannels(num_limit integer)
returns
boolean
AS
$$
declare
id integer default 1;
devName varchar;
channelName varchar;
devIndexCode varchar;
devIp varchar;
devIpEnd1 integer default 1;
devIpEnd2 integer default 1;
devPort integer default 1001;
devExternalCode varchar;
channelExternalCode varchar;
regionCount integer;
regionResourceLimit integer;
regionResourceNum integer default 0;
regionIndexCode varchar;
offsetNum integer default 0;
begin
select count(o.region_index_code) into regionCount from tb_region o where region_status = '0';
raise notice 'regionCount %' , regionCount;
if num_limit % regionCount = 0 then
regionResourceLimit := num_limit / regionCount;
end if;
if num_limit % regionCount > 0 then
regionResourceLimit := num_limit / regionCount + 1;
end if;
while devIpEnd1 <= 100
loop
devIpEnd2 := 1;
while devIpEnd2 <= 100
loop
devip := '10.33.' || devIpEnd1 || '.' || devIpEnd2;
devPort := 1000;
while devPort <= 1099
loop
devName := devip;
devIndexCode := uuid_generate_v4();
if regionIndexCode is null then
select region_index_code into regionIndexCode from tb_region where region_status = '0' limit 1 offset offsetNum;
end if;
INSERT INTO public.tb_device (dev_id, dev_index_code, dev_name, dev_addr, dev_port,
dev_model, active_device_code,
dev_username, dev_password, pwd_strength,
dev_type, dev_serial_num, dis_order,
dev_capability,
manufacturer, treaty_type, driver, sync_iac, remote_status,
region_index_code, domain_id,
ezviz_user_id, ezviz_dev_code,
dev_restype, business_class,
description, version,
is_cascade,
extended_attribute, com_id,
data_no, status, create_time, update_time,
creator, modifier
)
VALUES (uuid_generate_v4(), devIndexCode, devName, devIp, devPort,
'Simulator', null,
'admin', 'Dg/I6if34PWRn093VjyPqg==', 3,
null, 'SimulatorDevice', 1,
'{}',
'hikvision', 'hiksdk_net', 'drv_vss_hiksdk_general_1.13.100', 0, 1,
regionIndexCode, 0,
null, null,
'{encodeDevice}', 'vms_encodeDevice_model',
null, 0,
0,
'{
"zeroChanCount": "0",
"analogIoInCount": "0",
"analogIoOutCount": "0",
"digitalIoInCount": "0",
"dataCollectStatus": "1",
"digitalIoOutCount": "0",
"analogChannelCount": "0",
"digitalChannelCount": "1"
}', 'sdmc',
1, 0, now(), now(),
'admin', 'admin'
);

channelName := devName || '_1';
INSERT INTO public.tb_channel (channel_id, channel_index_code, channel_name, channel_num,
channel_main_type,
channel_sub_type, channel_capability, dev_index_code,
region_index_code, description,
com_id, ezviz_safe_watch_key,
treaty_type,
is_cascade, cascade_platform_code,
dis_order,
sync_iac, data_no, status, extended_attribute,
business_class, create_time,
update_time,
channel_restype,
creator, modifier)
VALUES (uuid_generate_v4(), uuid_generate_v4(), channelName, '1',
'camera',
'digital', '{}', devIndexCode,
regionIndexCode, null,
'sdmc', null,
null,
'0', '0',
id,
null, id, 0, '{
"osdStatus": "0"
}',
'vss_encodeDevice_camera', now(),
now(),
'{camera}',
'admin', 'admin');
devPort := devPort + 1;

regionResourceNum := regionResourceNum + 1;

if regionResourceNum = regionResourceLimit then
offsetNum := offsetNum + 1;
regionIndexCode := null;
regionResourceNum := 0;
end if;



if id = num_limit then
return null;
end if;

id := id + 1;
end loop;
devIpEnd2 := devIpEnd2 + 1;
end loop;
devIpEnd1 := devIpEnd1 + 1;
end loop;
return null;
end
$$
LANGUAGE plpgsql;

2)插入10w记录

1
select insertDeviceChannels(100000);

3)删除指定记录

1
delete from tb_device where region_index_code = '219f98dd-a83a-468f-a2c0-3a30cd2e6082';