我想将我的大型csv文件复制到Postgres。架构create table doe(firstname text, lastname text, phone text);
CSV文件
Firstname|LastName|Phone
John|Doe|55-55-555
Jane|Doe|66-66-666
开始
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
cmd := "psql"
args := fmt.Sprintf("-U postgres -d test -c \"\\copy doe from '%s' delimiter '|' csv header;\"", os.Args[1])
if err := exec.Command(cmd, args).Run(); err != nil {
panic(err)
}
println("Ok")
}
现在有错误
./ copy /tmp/test.csv panic:退出状态2
goroutine 1 [running]:main.main()/ tmp / copy.go:21 + 0x16b
我做错了什么?如果在控制台中运行
psql -U postgres -d test -c" \ copy doe from' /tmp/test.csv'分隔符' |' csv header;"
COPY 2
答案 0 :(得分:-1)
func main() {
cmd := "psql"
args := []string{"-U", "postgres", "-d", "test", "-c", fmt.Sprintf(`\copy doe from '%s' delimiter '|' csv header;`, os.Args[1])}
v, err := exec.Command(cmd, args...).CombinedOutput()
if err != nil {
panic(string(v))
}
println("Ok")
}