云数据库 GAUSSDB-需自行解析文件的导出场景:示例

时间:2025-06-03 09:37:30

示例

  1. 数据准备。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    gaussdb=# create database db1 encoding='UTF-8' LC_COLLATE='en_US.UTF-8' LC_CTYPE ='en_US.UTF-8' dbcompatibility = 'A';
    CREATE DATABASE
    gaussdb=# \c db1
    Non-SSL connection (SSL connection is recommended when requiring high-security)
    You are now connected to database "db1" as user "omm".
    db1=# CREATE TABLE test_copy(id int, name text);
    CREATE TABLE
    db1=# insert into test_copy values(1, 'aaa');
    INSERT 0 1
    db1=# insert into test_copy values(2, 'bb"b');
    INSERT 0 1
    db1=# insert into test_copy values(3, 'cc  c');
    INSERT 0 1
    db1=# insert into test_copy values('', e'dd\td');
    INSERT 0 1
    db1=# insert into test_copy values('5', e'ee\e');
    INSERT 0 1
    db1=# select * from test_copy;
     id |   name    
    ----+-----------
      1 | aaa
      2 | bb"b
      3 | cc  c
        | dd      d
      5 | eee
    (5 rows)
    

  2. 数据导出。

    1
    2
    db1=# COPY test_copy TO '/home/xy/test.fixed' encoding 'UTF-8' FIXED FORMATTER(id(0,1), name(1,5));
    COPY 5
    

    导出数据文件内容如下所示:

    1
    2
    3
    4
    5
    1  aaa
    2 bb"b
    3cc  c
      dd	d
    5  eee
    

  3. 数据导入。

    db1=# truncate test_copy;
    TRUNCATE TABLE
    db1=# copy test_copy from '/home/xy/test.fixed' encoding 'UTF-8' FIXED FORMATTER(id(0,1), name(1,5));
    COPY 5

support.huaweicloud.com/centralized-devg-v8-gaussdb/gaussdb-42-1989.html