PostgreSQL不同库间数据copy脚本

173 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第4天

PostgreSQL的copy命令进行数据导入导出的效率是很高的,我们可以通过下面这个脚本来实现两个库之前数据的快速copy。

shell脚本:

!/bin/bash
psql \
    -X \
    -U user_name \
    -h host_name1 \
    -d database_name \
    -c "\\copy tbl_Students to stdout" \
| \
psql \
    -X \
    -U user_name \
    -h host_name2 \
    -d database_name \
    -c "\\copy tbl_Students from stdin"

例子:

pg13@cnndr4pptliot-> cat copy_data.sh

#!/bin/bash
psql \
    -X \
    -U bill \
    -h localhost \
    -d bill \
    -c "\\copy test1 to stdout" \
| \
psql \
    -X \
    -U bill \
    -h 172.23.11.144 \
    -d bill \
    -c "\\copy test1 from stdin"

pg13@cnndr4pptliot-> sh copy_data.sh
Password for user bill:
COPY 100000