#!/bin/bash
i=1
username=user
port=5432
hostname=x.x.x.x
password=111111
maxid=8388608
minid=8388608
while :
do
#echo "$i"
sleep 0.5
(( i++ ))
maxid=`expr $minid`
minid=`expr $minid - 10000`
echo $minid,$maxid
psql "host=$hostname port=$port user=$username password=$password dbname=postgres" << EOF
\c db1111;
insert into tab1111_bak
select * from tab1111 where id>$minid and id<=$maxid;
EOF
if ((i==20))
then
break
fi
done
说明:
1)需从源表找到最大的ID,并赋maxid和minid参数为初始值;
2)minid在每次循环时减10000,即每次循环10000条数据;
3)循环次数i ,根据实际情况指定,本例子是20次循环退出,即本例子总共插入目标表19万条数据。
4)调用方式如下:
sh ./postgresql_insert.sh >>/wzdata/postgresql_insert.log 2>&1 &