PostgreSQL copy命令使用记录

上篇写到用pg_bulkload来导入数据,但是实际的环境是solaris,pg_bulkload还不支持,于是用copy的方式,下面附上脚本

 

-bash-4.1$ cat copy.sh
#!/bin/sh

#$1 data fil ename

file=$1

if [ ! -f $file  ]
then
    echo "File is not exist"
    exit 1
fi

echo "-----------------------------------------------------------------"

tbname=$( echo $file |cut -d . -f1 )
echo "Table name is : "$tbname

echo "set table unlogging model"
psql -c "update pg_class set relpersistence='u' where relname = '$tbname'" -d sgdw

zcat $file|psql -c "copy $tbname from stdin with (format csv, delimiter ',')" -d sgdw
#zcat $file|psql -c "copy $tbname from stdin with (format csv, delimiter ',', null '\n', encoding 'utf8' quote '"', force_quote *)" -d sgdw

echo "set table logging model"
psql -c "update pg_class set relpersistence='p' where relname = '$tbname'" -d sgdw

echo "load $tbname complete"
echo "-----------------------------------------------------------------"

 

批量导入:

-bash-4.1$ cat copy_all.sh
#!/bin/sh

files=$(ls *.gz)

for file in $files;
do
    echo "-----------------------------------------------------------------"
    echo "Load file : "$file

    starttime=$(date +"%F %X")
    startseconde=$(date +%s)
    echo "Start time is :" $starttime

    ./copy.sh $file

    endtime=$(date +"%F %X")
    echo "End time is :" $endtime

    endseconde=$(date +%s)
    echo "Total time is :" $(($endseconde-$startseconde))
    echo "-----------------------------------------------------------------"
    echo ""
    echo ""
done

 

后台执行:

nohup ./copy_all.sh > copy_all.log 2>&1 &

 

加入新的用法,一次搞定:

for i in $(ls *.csv);do psql -c "copy ${i/.csv/} from '/var/lib/pgsql/tpc-h/tpc-h-orcale/$i' with delimiter '|'" tpch; echo $i;done

 

转载于:https://www.cnblogs.com/kuang17/p/9753292.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值