postgres容器版docker批量sql

postgres容器版docker批量sql

#!/bin/bash
# Author : royoy
# Date   : 09/2019
# Desc   : SQL query and so on

env="$1"
sql=/home/ubuntu/pay_db/sql

db(){
    echo -e "\nThe results of the execution is as follows:\n"
    docker run --rm -i \
        -v /tmp:/tmp \
        -e PGHOST=uas.cfgxco79q1h9.ap-northeast-1.rds.amazonaws.com \
        -e PGDATABASE=$env \
        -e PGUSER=$env \
        -e PGPASSWORD=$env \
        -e PAGER=less \
        postgres:alpine psql -c "`echo "$@" | sed 's/\*/\\*/g' |sed "s/'/\\\'/g"`"
}

if [[ "dev staging product" =~ "$env" && -f $sql ]];then
    cat $sql | while read line
    do
        db "$line" &
        wait
    done
    > $sql
else
    exit 9
fi