V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sjmcefc2
V2EX  ›  程序员

请教 postgres 的 copy 问题

  •  
  •   sjmcefc2 · 2019-06-15 21:13:29 +08:00 · 2053 次点击
    这是一个创建于 1769 天前的主题,其中的信息可能已经有所发展或是发生改变。

    postgres 的 co'p'y 有下面几种 COPY 命令概述 copy 命令用于表与文件(和标准输出,标准输入)之间的相互拷贝; copy to 由表至文件,copy from 由文件至表; copy 命令始终是到数据库服务端找文件,以超级用户执行导入导出权限要求很高,适合数据库管理员操作; \copy 命令可在客户端执行导入客户端的数据文件,权限要求没那么高,适合开发人员,测试人员使用。

    现在研究 copy,从数据库服务端找文件,导入。copy 貌似是只要一条不成功,整个文件都会导入失败。 但是我的文件有点问题,有部分记录无法导入。好像 copy 没有参数可以部分导入?并把错误的行挑出来?

    请教大家有没有好办法?能够成批导入(挑出未导入的记录)?

    9 条回复    2019-06-16 20:30:24 +08:00
    kxjhlele
        1
    kxjhlele  
       2019-06-15 21:49:24 +08:00 via Android
    用 gp 的 copy 或者 pgloader 或者有一个 commit 是解决错误行的,但是没有合并呢
    sjmcefc2
        2
    sjmcefc2  
    OP
       2019-06-15 21:58:30 +08:00
    @kxjhlele gp 是 pg 的笔误?
    pg 的 copy 不能解决错误行问题吧,是用那个参数?
    pgloader 查了一下,貌似是为了解决 rejected line 而存在的。
    commit 没有查到是什么
    “但是没有合并”这句话没有看懂,能不能稍作解释?
    sjmcefc2
        3
    sjmcefc2  
    OP
       2019-06-15 22:00:18 +08:00
    如果用英文查,该用什么关键词呢
    tiluo
        4
    tiluo  
       2019-06-15 22:24:03 +08:00
    为什么无法导入呢,格式问题的话试一试 NULL option
    NULL
    Specifies the string that represents a null value. The default is \N (backslash-N) in text format, and an unquoted empty string in CSV format. You might prefer an empty string even in text format for cases where you don't want to distinguish nulls from empty strings. This option is not allowed when using binary format.
    FORCE_NOT_NULL
    Do not match the specified columns' values against the null string. In the default case where the null string is empty, this means that empty values will be read as zero-length strings rather than nulls, even when they are not quoted. This option is allowed only in COPY FROM, and only when using CSV format.
    FORCE_NULL
    Match the specified columns' values against the null string, even if it has been quoted, and if a match is found set the value to NULL. In the default case where the null string is empty, this converts a quoted empty string into NULL. This option is allowed only in COPY FROM, and only when using CSV format.
    kxjhlele
        5
    kxjhlele  
       2019-06-15 22:52:57 +08:00 via Android
    sjmcefc2
        6
    sjmcefc2  
    OP
       2019-06-15 23:10:23 +08:00
    @tiluo 无法导入很多是因为某个字段的编码不对,比如整个文件可能编码是 utf8,但是某行的某个字段可能是 gbk,这样就挂掉了,导致整个文件无法 copy 进去。或者是某个字段有个转义字符\与其他的字符合成了一个异常字符。

    “格式问题”您指的是?


    @kxjhlele 非常感谢。这个问题还是挺困扰了,可能因为某一行的某个字段问题,导致整个文件不被接受,特别困扰。
    sjmcefc2
        7
    sjmcefc2  
    OP
       2019-06-15 23:12:07 +08:00
    @kxjhlele pgloader 您有导入具有分隔符 txt 的例子吗?
    sjmcefc2
        8
    sjmcefc2  
    OP
       2019-06-16 17:38:11 +08:00
    一定要在--field 里面列出所有的字段?
    那样有点麻烦啊
    sjmcefc2
        9
    sjmcefc2  
    OP
       2019-06-16 20:30:24 +08:00
    pgloader --type CSV \
    -D /home/postgres/pgloader \
    -L /home/postgres/pgloader/testc.log
    --with "fields terminated by '\t'" \
    /home/postgres/pgloader/2013_Gaz_113CDs_national.txt \
    postgresql:///testb?tablename=test.districts_longlat


    自己在测试的时候表名字、模式名字用了大写,
    postgresql:///testb?tablename=“ TEST ”."DISTRICTS_LONGLAT"破不了
    postgresql:///testb?tablename=“\"TEST\”.\"DISTRICTS_LONGLAT\""也破不了
    FATAL error: pgloader found 0 target tables for name test.districts_longlat:
    An unhandled error condition has been signalled:
    pgloader found 0 target tables for name test.districts_longlat:




    What I am doing here?

    pgloader found 0 target tables for name test.districts_longlat:


    大写表名字怎么破?
    大家 postgresql 是不是都是小写的名字?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2808 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:38 · PVG 19:38 · LAX 04:38 · JFK 07:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.