SRC_DB_HOST="localhost"
SRC_DB_PORT="5442"
SRC_DB_NAME="postgres"
SRC_DB_USER="myuser"
SRC_DB_PASS='mypwd'
DEST_DB_HOST="localhost"
DEST_DB_PORT="5443"
DEST_DB_NAME="postgres"
DEST_DB_USER="myuser"
DEST_DB_PASS='mypwd'
TABLE_NAME="my_table"
DUMP_FILE="/tmp/${TABLE_NAME}.sql"
export PGPASSWORD=$SRC_DB_PASS
RESULT=$(psql -h ${SRC_DB_HOST} -p ${SRC_DB_PORT} -U ${SRC_DB_USER} -d ${SRC_DB_NAME} -t -c "SELECT count(1) FROM information_schema.tables WHERE table_name = '${TABLE_NAME}' ")
if [[ "$RESULT" -eq "0" ]]; then
echo "Error: The table ${TABLE_NAME} doesn't exist in the Source DB."
exit 1
fi
export PGPASSWORD=$DEST_DB_PASS
RESULT=$(psql -h ${DEST_DB_HOST} -p ${DEST_DB_PORT} -U ${DEST_DB_USER} -d ${DEST_DB_NAME} -t -c "SELECT count(1) FROM information_schema.tables WHERE table_name = '${TABLE_NAME}' ")
if [[ "$RESULT" -ne "0" ]]; then
echo "Error: The table ${TABLE_NAME} exists in the Destination DB."
exit 1
fi
export PGPASSWORD=$SRC_DB_PASS
pg_dump -h ${SRC_DB_HOST} -p ${SRC_DB_PORT} -U ${SRC_DB_USER} -t ${TABLE_NAME} -F p ${SRC_DB_NAME} > ${DUMP_FILE}
export PGPASSWORD=$DEST_DB_PASS
psql -h ${DEST_DB_HOST} -p ${DEST_DB_PORT} -U ${DEST_DB_USER} -d ${DEST_DB_NAME} -f ${DUMP_FILE} -v ON_ERROR_STOP=1
export PGPASSWORD=$DEST_DB_PASS
RESULT=$(psql -h ${DEST_DB_HOST} -p ${DEST_DB_PORT} -U ${DEST_DB_USER} -d ${DEST_DB_NAME} -t -c "SELECT COUNT(*) FROM ${TABLE_NAME}")
if [[ "$RESULT" -eq "0" ]]; then
echo "Error: The table ${TABLE_NAME} could not be copied."
exit 1
fi
export PGPASSWORD=$SRC_DB_PASS
source_count=$(psql -h ${SRC_DB_HOST} -p ${SRC_DB_PORT} -U ${SRC_DB_USER} -d ${SRC_DB_NAME} -c "SELECT COUNT(*) FROM ${TABLE_NAME}" -t)
export PGPASSWORD=$DEST_DB_PASS
dest_count=$(psql -h ${DEST_DB_HOST} -p ${DEST_DB_PORT} -U ${DEST_DB_USER} -d ${DEST_DB_NAME} -c "SELECT COUNT(*) FROM ${TABLE_NAME}" -t)
if [ "$source_count" -ne "$dest_count" ]; then
echo "Error: The number of records in the source and destination tables ${TABLE_NAME} do not match."
exit 1
else
echo "Success: The table ${TABLE_NAME} was copied successfully."
fi
echo "The table was ${TABLE_NAME} successfully copied to the destination database."
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?