如何解决在使用PowerShell执行psql和Copy命令时出现乱码的问题?

huangapple go评论125阅读模式
英文:

How to resolve garbled code when using powershell to execute psql and the Copy command?

问题

我正在编写PowerShell脚本,使用psql复制命令将客户端机器上的“csv”文件复制到远程PostgreSQL数据库表格中。

复制命令执行成功,但是一些日文字符变成了乱码。

我已经为客户端和控制台的标准输入和输出设置了编码,但仍然出现了乱码问题。

[System.Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8
[System.Console]::InputEncoding = [Text.UTF8Encoding]::UTF8
$env:PGPASSWORD = 'pwd';
foreach($file in Get-ChildItem .\Import -Filter *.csv){
    $table = $file.BaseName
    $Truncate_Sql = "truncate table $table;"
    $Result = & psql -h 10.0.xxx.xx -d db1 -U user1 -w -c $Truncate_Sql
    Write-Host $Result  $table  "Ok."
    $Result =   Write-Output (Get-Content -Path $file.FullName -Encoding utf8) | & psql -h 10.0.xxx.xx -d db1 -U user1 -w -c "set client_encoding=UTF8;COPY $table FROM STDIN WITH(FORMAT CSV,HEADER TRUE, ENCODING 'UTF8')"
    $Result
}

例如,csv(utf8编码)的内容如下所示:

header1,header2
1001,その他

当复制到PostgreSQL后,我使用pgAdmin查看表格数据。显示的内容如下:

header1 header2
1001 ???
英文:

I'm writing powershell script to copy "csv" from client machine to remote postgre database table with psql copy command.

The copy command executed successfully, but some Japanese characters have become garbled.

I have set the encoding for client and console stdin and stdout, but garbled code still occurred.

[System.Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8
[System.Console]::InputEncoding = [Text.UTF8Encoding]::UTF8
$env:PGPASSWORD = 'pwd';
foreach($file in Get-ChildItem .\Import -Filter *.csv){
    $table = $file.BaseName
    $Truncate_Sql = "truncate table $table;"
    $Result = & psql -h 10.0.xxx.xx -d db1 -U user1 -w -c $Truncate_Sql
    Write-Host $Result  $table  "Ok."
    $Result =   Write-Output (Get-Content -Path $file.FullName -Encoding utf8) | & psql -h 10.0.xxx.xx -d db1 -U user1 -w -c "set client_encoding=UTF8;COPY $table FROM STDIN WITH(FORMAT CSV,HEADER TRUE, ENCODING 'UTF8')"
    $Result
}

For example, the csv(utf8 encoding) content looks like the following

header1,header2
1001,その他

When copied to postgre, I use pgAdmin to view the table data. It shows:

header1 header2
1001 ???

答案1

得分: 0

我解决了我的问题。我混淆了PostgreSQL的COPY命令和psql的\COPY指令。

实际上,我需要使用\COPY指令来执行copy命令,然后我可以直接从文件复制而不是写入stdin

[System.Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8
[System.Console]::InputEncoding = [Text.UTF8Encoding]::UTF8
$env:PGPASSWORD = 'pwd';
foreach($file in Get-ChildItem .\Import -Filter *.csv){
    $table = $file.BaseName
    $Truncate_Sql = "truncate table $table;"
    $Result = & psql -h 10.0.xxx.xx -d db1 -U user1 -w -c $Truncate_Sql
    Write-Host $Result  $table  "Ok."
    $Result = & psql -h 10.0.xxx.xx -d db1 -U user1 -w -c "\COPY $table FROM $($file.FullName) WITH(FORMAT CSV,HEADER TRUE, ENCODING 'UTF8')"
    $Result
}
英文:

I resolved my question. I confused the Postgre SQL COPY command with psql \COPY instruction.

Actually, I need to use \COPY instruction to execute copy command, then I can directly copy from file instead of writing to stdin.

[System.Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8
[System.Console]::InputEncoding = [Text.UTF8Encoding]::UTF8
$env:PGPASSWORD = 'pwd';
foreach($file in Get-ChildItem .\Import -Filter *.csv){
    $table = $file.BaseName
    $Truncate_Sql = "truncate table $table;"
    $Result = & psql -h 10.0.xxx.xx -d db1 -U user1 -w -c $Truncate_Sql
    Write-Host $Result  $table  "Ok."
    $Result = & psql -h 10.0.xxx.xx -d db1 -U user1 -w -c "\COPY $table FROM $($file.FullName) WITH(FORMAT CSV,HEADER TRUE, ENCODING 'UTF8')"
    $Result
}

huangapple
  • 本文由 发表于 2023年3月7日 14:00:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定