Halaman ini belum tersedia dalam bahasa lokal Anda. Kami berusaha keras untuk menambahkan lebih banyak versi bahasa. Terima kasih atas dukungan Anda.

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
Help Center/ GaussDB/ User Guide/ Database Migration/ Using the copy to/from Command to Export and Import Data

Using the copy to/from Command to Export and Import Data

Updated on 2025-01-09 GMT+08:00

Scenarios

The gsql tool provides the \copy meta-command to import or export data. \copy applies only to small-scale data import in good format. It does not preprocess invalid characters or provide error tolerance. Therefore, \copy cannot be used in scenarios where abnormal data exists.

Preparing for Data Migration

  1. Prepare an ECS or a device that can access the GaussDB instance over EIP.
    • To connect to a GaussDB instance through an ECS, you must first create an ECS.

      For details on how to create and log in to an ECS, see Purchasing an ECS and Logging In to an ECS in Elastic Cloud Server Getting Started.

    • To connect to a GaussDB instance through a device that can access the GaussDB instance over EIP, you must:
      1. Bind an EIP to the GaussDB instance. For details, see Binding an EIP.
      2. Ensure that the local device can access the EIP that has been bound to the GaussDB instance.
  2. Install the gsql client on the prepared ECS or device that can access the GaussDB database, and connect it to the GaussDB database instance. For details, see Using gsql to Connect to a Database.

Exporting Data

On the prepared ECS or device that can access GaussDB, connect to the GaussDB instance and export the content of the copy_example table.
  • Method 1: Export the content of the copy_example table to stdout in CSV format. Use quotation marks (") as the quotes, and use the quotes to enclose the fourth and fifth columns.
    \copy copy_example to stdout CSV quote as '"' force quote col_4,col_5;
    1,iamtext,iamvarchar,"2006-07-07","12:00:00"
    2,sometext,somevarchar,"2006-07-07","12:00:00"
    3,sometext,somevarchar,"2006-07-07","12:00:00"
    4,sometext,somevarchar,"2022-07-07","19:00:02"
    5,sometext,somevarchar,"2006-07-07",
    6,sometext,somevarchar,"2022-07-07","19:00:02"
  • Method 2: Export the content of the copy_example table to the copy_example.csv file under the local path /tmp/data/. Use vertical bars (|) as the delimiters and quotation marks (") as the quotes.
    \copy copy_example to '/tmp/data/copy_example.csv' csv delimiter '|' quote '"';

    Check the /tmp/data/copy_example.csv file to ensure that the data has been exported.

    1|iamtext|iamvarchar|2006-07-07|12:00:00
    2|sometext|somevarchar|2006-07-07|12:00:00
    3|sometext|somevarchar|2006-07-07|12:00:00
    4|sometext|somevarchar|2022-07-07|19:00:02
    5|sometext|somevarchar|2006-07-07|
    6|sometext|somevarchar|2022-07-07|19:00:02
  • Method 3: Export the query result set of the copy_example table to the copy_example2.csv file under the local path /tmp/data/ . Use commas (,) as the delimiters and quotation marks (") as the quotes.
    \copy (select * from copy_example where col_1 = 1) to '/tmp/data/copy_example2.csv' csv delimiter ',' quote '"';

    Check the /tmp/data/copy_example2.csv file to ensure that the data has been exported.

    1,iamtext,iamvarchar,2006-07-07,12:00:00

Importing Data

Import data to a GaussDB instance. For example, import data to the target table copy_example, the schema is as follows:
1
2
3
4
5
6
7
8
create table copy_example
(
 col_1 integer,
 col_2 text,
 col_3 varchar(12),
 col_4 date,
 col_5 time
);
On the prepared ECS or device that can access GaussDB, connect to the GaussDB instance and import data to the target table copy_example.
  • Method 1: Copy data from stdin and import data to the target table copy_example.
    \copy copy_example from stdin csv;

    When >> is displayed, enter data. To end your input, enter a backslash and a period (\.).

    Enter data to be copied followed by a newline.
    End with a backslash and a period on a line by itself.
    >> 1,"iamtext","iamvarchar",2006-07-07,12:00:00
    >> 2,"sometext","somevarchar",2006-07-07,12:00:00
    >> \.
    

    View the imported data.

    select * from copy_example;
     col_1 |  col_2   |    col_3    |        col_4        |  col_5   
    -------+----------+-------------+---------------------+----------
         1 | iamtext  | iamvarchar  | 2006-07-07 00:00:00 | 12:00:00
         2 | sometext | somevarchar | 2006-07-07 00:00:00 | 12:00:00
    (2 rows)
  • Method 2: The example.csv file exists in the local /tmp/data/ directory. The file contains the header row. Use vertical bars (|) as the delimiters and quotation marks (") as the quotes. The content is as follows:
    header
    3|"sometext"|"somevarchar"|2006-07-07|12:00:00
    4|"sometext"|"somevarchar"|2022-07-07|19:00:02

    Import data from the local file example.csv to the target table copy_example. If header is set to on, the first row is automatically ignored. Use quotation marks (") as the quotes by default.

    \copy copy_example from  '/tmp/data/example.csv' with(header 'on', format 'csv', delimiter '|', date_format 'yyyy-mm-dd',  time_format 'hh24:mi:ss');

    View the imported data.

    select * from copy_example;
     col_1 |  col_2   |    col_3    |        col_4        |  col_5   
    -------+----------+-------------+---------------------+----------
         1 | iamtext  | iamvarchar  | 2006-07-07 00:00:00 | 12:00:00
         2 | sometext | somevarchar | 2006-07-07 00:00:00 | 12:00:00
         3 | sometext | somevarchar | 2006-07-07 00:00:00 | 12:00:00
         4 | sometext | somevarchar | 2022-07-07 00:00:00 | 19:00:02
    (4 rows)
  • Method 3: The example2.csv file exists in the local directory /tmp/data/. Use commas (,) as the delimiters and quotation marks (") as the quotes. The last field is missing in the first line, and the last field is added in the second line. The content is as follows:
    5,"sometext","somevarchar",2006-07-07
    6,"sometext","somevarchar",2022-07-07,19:00:02,12:00:00

    Import data from the local file example2.csv to the target table copy_example. The default delimiters are commas (,). Because the error tolerance parameters IGNORE_EXTRA_DATA and FILL_MISSING_FIELDS are specified, the missing fields are replaced with NULL, and the extra fields are ignored.

    \copy copy_example from  '/tmp/data/example2.csv' with( format 'csv', date_format 'yyyy-mm-dd',  time_format 'hh24:mi:ss', IGNORE_EXTRA_DATA 'true', FILL_MISSING_FIELDS 'true');

    View the imported data.

    select * from copy_example;
     col_1 |  col_2   |    col_3    |        col_4        |  col_5   
    -------+----------+-------------+---------------------+----------
         1 | iamtext  | iamvarchar  | 2006-07-07 00:00:00 | 12:00:00
         2 | sometext | somevarchar | 2006-07-07 00:00:00 | 12:00:00
         3 | sometext | somevarchar | 2006-07-07 00:00:00 | 12:00:00
         4 | sometext | somevarchar | 2022-07-07 00:00:00 | 19:00:02
         5 | sometext | somevarchar | 2006-07-07 00:00:00 | 
         6 | sometext | somevarchar | 2022-07-07 00:00:00 | 19:00:02
    (6 rows)

Helpful Links

For more information, see:

Kami menggunakan cookie untuk meningkatkan kualitas situs kami dan pengalaman Anda. Dengan melanjutkan penelusuran di situs kami berarti Anda menerima kebijakan cookie kami. Cari tahu selengkapnya

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback