Jun 7, 2012

Creating postgres database tables into Flat files (.copy) using shell script


#!/bin/sh
if [ ! $# -eq 2 ] ; then
echo "Invalid arguments! Usage : creatflatfiles.sh  outpurdir tablelistfilename";
echo "ex:creatflatfiles.sh  1L_joblogs tableslist.txt";
exit;
else
echo "Executing database  downloading to faltflies ..." ;
exec<$2
/bin/mkdir -p $1
cd $1
value=0;
while read line
 do
   value=`expr $value + 1`;  
   touch $line.copy;
   chmod 666 $line.copy;
   query="COPY "$line" TO '/usr/"$1"/"$line".copy';"
   echo $query
  #psql -h localhost -d databasename -U username -c command
   psql -h localhost -d webacc -U postgres -c "$query" >> /usr/FFtoDB.log 2>&1
 done
echo "****$value file got created..";
fi
#------------------------------------------

tableslist.txt should contain the table names.Script will generate tables data into flat files (tablename.copy) in the outputdir. Errors and return values will be saved in  /usr/FFtoDB.log file.

No comments:

Post a Comment