• Windows环境使用psql执行 Copy命令进行Postgres表数据迁移


    Postgres实例之间通过copy命令迁移数据

    利用COPY 的stdout 和 stdin 进行数据迁移比较快,如:
    源实例: -h 10.10.20.10 -p 5432 表 tab_test
    目标实例: -h 10.10.20.20 -p 5532 表 tab_test
    psql -h 10.10.20.10 -U postgres -d postgres -p 5432 -c "copy tab_test to stdout" | psql -h 10.10.20.20 -U postgres -d postgres -p 5532 -c "copy tab_test from stdin"

    Windows 10环境使用CMD

    运行 cmd ,执行 chcp 命令,查看当前的cmd的编码:

    C:\Users\admin>chcp
    Active code page: 65001
    

    若当前编码不是65001 (UTF8) 则执行如下命令,更改当前cmd的编码:

    C:\Users\admin>chcp 65001
    

    接下来执行如下命令,迁移 diagnose 表的 四列数据 diag_name ,diag_code , spell_code, wb_code,执行成功如下:

    C:\Users\admin>psql -h xxx.xxx.xxx.xxx.xxx -p 5432 -U pguser -d db0  -X -q  -c "copy (select  column1, column2, column3, column4  from table1 offset 20 limit 10) to STDOUT (FORMAT csv ,HEADER false ,FORCE_QUOTE *, encoding 'UTF8')" |psql -h yyy.yyy.yyy.yyy.yyy -p 5432 -U pguser -d db1 -c "set client_encoding to 'UTF8'; copy table2( columna, columnb, columnc, columnd) from STDIN  (DELIMITER ',', FORMAT  csv,HEADER false ,encoding 'UTF8') "
    Password for user pguser: Password for user pguser:
    
    COPY 10
    Time: 1409.675 ms (00:01.410)
    

    Windows 10环境使用powershetll

    在执行Postgresql实例迁移传输单张表数据时候使用PowerShell,需要更改编码为UTF8:

    $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
    

    PowerShell注意:

    • Powershell必须要进行如下设置,设置PowerShell的编码为UTF-8,每次重新进入PowerShell都需要执行此命令

    • PowerShell中如果需要使用管道命令显示输出结果 可以使用 Write-Output 命令 或者 Write-Host ,可以优先使用前者

      PS C:\Users\admin> psql -h xxx.xxx.xxx.xxx.xxx -p 5432 -U pguser -d db1 -c "copy (select  column1, column2 from table1 offset 20 limit 10) to STDOUT (FORMAT csv ,HEADER false ,FORCE_QUOTE *,encoding 'UTF8')" | Write-Output
      

      如上命令,可以检查输出的数据是否因为编码问题乱码。

    接下来 ,执行如下命令,期间需要分多次输入密码,执行成功后如下:

    PS C:\Users\admin> psql -h xxx.xxx.xxx.xxx.xxx -p 5432 -U pguser -d db0  -X -q  -c "copy (select  column1, column2, column3, column4  from table1 offset 20 limit 10) to STDOUT (FORMAT csv ,HEADER false ,FORCE_QUOTE *, encoding 'UTF8')" |psql -h yyy.yyy.yyy.yyy.yyy -p 5432 -U pguser -d db1 -c "set client_encoding to 'UTF8'; copy table2( columna, columnb, columnc, columnd) from STDIN  (DELIMITER ',', FORMAT  csv,HEADER false ,encoding 'UTF8') "
    Password for user pguser:
    Password for user pguser:
    COPY 10
    Time: 11.277 ms
    

    注意:

    • -X --no-psqlrc 表示 不读取启动文档(~/.psqlrc) ,可以忽略.psqlrc中的一些设置,目的是为了去除下图所示的执行的一个耗时,在使用copy命令从STDID执行导入的时候 会影响导入的结果 导致导入报错。

    • -q --quiet 表示以沉默模式运行(不显示消息,只有查询结果),这样会忽略一些其他的干扰因素的数据从而导致copy命令执行失败。
  • 相关阅读:
    Maven记录
    TP-Link的config.bin的解码
    SLF4JLogFactory does not implement org.apache.commons.logging.LogFactory
    axis1.4调用WebService报找不到分派方法
    Spring在单例bean中使用session、request范围的bean
    使用spring-session同时用session范围bean的问题
    tomcat session共享快速入门
    Log4j配置spring+druid打印日志
    基于WebSocket的多人在线坦克大战demo
    IDEA将maven项目配置到本地tomcat中运行
  • 原文地址:https://www.cnblogs.com/hand/p/16180832.html
Copyright © 2020-2023  润新知