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/ Developer Guide(Distributed_2.x)/ Importing Data/ Using a gsql Meta-Command to Import Data

Using a gsql Meta-Command to Import Data

Updated on 2024-10-14 GMT+08:00

The gsql tool provides the \copy to import data.

\copy Command

For the format and description of the \copy command, see Table 1.

Table 1 \copy meta-command

Syntax

Description

\copy { table [ ( column_list ) ] |

( query ) } { from | to } { filename |

stdin | stdout | pstdin | pstdout }

[ with ] [ binary ] [ oids ] [ delimiter

[ as ] 'character' ] [ useeof ] [ null [ as ] 'string' ]

[ csv [ header ] [ quote [ as ]

'character' ] [ escape [ as ] 'character' ]

[ force quote column_list | * ] [ force

not null column_list ] ]

You can run this command to import or export data after logging in to a database on any gsql client. Different from the COPY statement in SQL, this command performs read/write operations on local files rather than files on database servers. The accessibility and permissions of the local files are restricted to local users.

NOTE:

\copy applies only to small-scale data import in good format. It does not preprocess invalid characters or provides error tolerance. Therefore, \copy cannot be used in scenarios where abnormal data exists. GDS or COPY is preferred for data import.

Parameter Description

  • table

    Specifies the name (possibly schema-qualified) of an existing table.

    Value range: an existing table name

  • column_list

    Indicates an optional list of columns to be copied.

    Value range: any field in the table. If no column list is specified, all columns of the table will be copied.

  • query

    Specifies that the results will be copied.

    Value range: a SELECT or VALUES command in parentheses.

  • filename

    Specifies the absolute path of a file. To run the \copy to command, you must have the write permission on the path. To run the \copy from command, you must have the read permission on the path.

  • stdin

    Specifies that input comes from the client application.

  • stdout

    Specifies that output goes to the client application.

  • pstdin

    Specifies that input comes from the gsql client.

  • pstout
  • Specifies that output goes to the gsql client.
  • binary

    Specifies that data is stored and read in binary mode instead of text mode. In binary mode, you cannot declare DELIMITER, NULL, or CSV. After binary is specified, CSV, FIXED, and TEXT cannot be specified through option or copy_option.

  • oid

    Specifies the internal OID to be copied for each row.

    NOTE:

    An error is raised if OIDs are specified for a table that does not have OIDs, or in the case of copying a query.

    Value range: true/on and false/off

    Default value: false

  • delimiter [ as ] 'character'
    Specifies the character that separates columns within each row (line) of the file.
    NOTE:
    • The value of delimiter cannot be \r or \n.
    • A delimiter cannot be the same as the null value. The delimiter for the CSV format cannot be same as the quote value.
    • The delimiter for the TEXT format data cannot contain backslashs (\), dots (.), lowercase letters, or digits, for example, \.abcdefghijklmnopqrstuvwxyz0123456789.
    • The data length of a single row should be less than 1 GB. A row that has many columns using long delimiters cannot contain much valid data.
    • You are advised to use multi-character delimiters or invisible delimiters. For example, you can use multi-characters (such as $^&) and invisible characters (such as 0x07, 0x08, and 0x1b).

    Value range: a multi-character delimiter within 10 bytes

    Default value:

    • A tab character in text format
    • A comma (,) in CSV format
    • No delimiter in FIXED format
  • null [ as ] 'string'

    Specifies that a string represents a null value in a data file.

    Value range:

    • A null value cannot be \r or \n. The maximum length is 100 characters.
    • A null value cannot be the same as the delimiter or quote value.

    Default value:

    • an empty string without quotation marks in CSV format
    • \N in text format
  • useeof

    Does not report an error for \. in the imported data.

    Value range: true or false

    Default value: false

  • header

    Specifies whether a file contains a header with the names of each column in the file. header is available only for CSV and FIXED files.

    When data is imported, if header is on, the first row of the data file will be identified as the header and ignored. If header is off, the first row will be identified as a data row.

    When data is exported, if header is on, fileheader must be specified. fileheader specifies the content in the header. If header is off, an exported file does not contain a header.

    Value range: true/on and false/off

    Default value: false

  • quote [ as ] 'character'

    Specifies a quote character for a CSV file.

    Default value: ""

    NOTE:
    • The quote value cannot be the same as the delimiter or null value.
    • The quote value must be a single-byte character.
    • Invisible characters are recommended, such as 0x07, 0x08, and 0x1b.
  • escape [ as ] 'character'

    Specifies an escape character for a CSV file. The value must be a single-byte character.

    Default value: "" If the value is the same as the quote value, it will be replaced with \0.

  • force quote column_list | *

    In CSV COPY TO mode, forces quoting to be used for all not-null values in each specified column. NULL values are not quoted.

    Value range: an existing column

  • force not null column_list

    Assigns a value to a specified column in CSV COPY FROM mode.

    Value range: an existing column

Examples

  1. Create a target table a.
    1
    openGauss=# CREATE TABLE a(a int);
    
  2. Import data.
    1. Copy data from stdin to table a.
      openGauss=# \copy a from stdin;

      When the >> characters are 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
      >> 2
      >> \.

      Query data imported to table a.

      1
      2
      3
      4
      5
      6
      openGauss=# SELECT * FROM a;
       a 
      ---
       1
       2
      (2 rows)
      
    2. Copy data from a local file to table a. For example, the local file /home/omm/2.csv exists.
      • Commas (,) are used as delimiters.
      • If the number of columns defined in a source data file is greater than that in a foreign table, extra columns will be ignored during import.
      1
      openGauss=# \copy a FROM '/home/omm/2.csv' WITH (delimiter',',IGNORE_EXTRA_DATA 'on');
      

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback