Synchronize a postgres table through bash and csv

Please note that the csv export process does not escape commas.

#!/bin/bash
DIR='/root/sql_dump'
mkdir -p "$DIR"
cd "$DIR"

SIZE=100000
#START=611244350
START=0
END=$((START + SIZE))
STOP=189097000
TABLNAME="schema.tablename"
while [[ $START -lt $STOP ]] && [[ $END -le $STOP ]]; do
export PGPASSWORD="PASSWORD#1"

echo "Read from SOURCE start $START end $END"
FILE="${DIR}/dump${START}.csv"
SQL="SELECT * FROM ${TABLNAME} where id > $START and id <= $END" psql -U postgres -h SOURCEHOST -d DB1 -t -A -F"," -c "$SQL" > "$FILE"

export PGPASSWORD="PASSWORD#1"
SQL="DELETE FROM ${TABLNAME} where id > $START and id <= $END" psql -U postgres -h localhost -d DB2 -t -A -F"," -c "$SQL" psql -U postgres -h localhost -d DB2 -c "\copy ${TABLNAME} FROM '${FILE}' DELIMITER ',' CSV" echo "Injected to DESTINATION" START=$END END=$((START + SIZE)) if [ $END -gt $STOP ]; then END=$STOP fi rm $FILE done

Leave a Reply

Your email address will not be published. Required fields are marked *


7 × four =