The COPY command moves data between Hologres and your local file system. Use COPY FROM STDIN to load local data into a Hologres table, and COPY (query) TO STDOUT to export query results to a local file.
How it works
The COPY command runs through the psql client, which acts as a bridge between your local files and Hologres:
-
Import: psql reads your local file and pipes it to Hologres as standard input (STDIN).
-
Export: Hologres writes query results to standard output (STDOUT), and psql redirects that output to a local file.
This client-side bridging is why COPY works only in the psql client — it requires direct access to your local file system. DataStudio and HoloWeb run on the server side and cannot access your local file system, so they do not support command-line COPY operations.
The psql \copy meta-command works the same way — it invokes COPY FROM STDIN or COPY TO STDOUT internally, with file access handled on the client side.
Prerequisites
Before you begin, make sure you have:
-
Access to a Hologres instance with a database and table ready
-
The psql client installed and connected to your Hologres instance. For setup instructions, see psql client
Limitations
| Limitation | Details |
|---|---|
| Data types | The COPY command supports the same data types as the Hologres engine. See Data types. |
| Partitioned tables | You can import data into the child tables of a partitioned table, but not into the parent table. |
| Supported commands | Hologres supports only COPY FROM STDIN for imports and COPY (query) TO STDOUT for exports. |
| Column coverage | You cannot import data into only one column of a table. |
| DEFAULT and serial fields | In Hologres v1.1.43 and later, tables with fields that use the DEFAULT keyword or serial type are supported in COPY FROM STDIN. Earlier versions do not support this. |
For full COPY command documentation, see COPY command.
Syntax
-- Import data from standard input
COPY table_name [ ( column_name [, ...] ) ]
FROM STDIN
[ [ WITH ] ( option [, ...] ) ]
-- Export data to standard output
COPY { ( query ) }
TO STDOUT
[ [ WITH ] ( option [, ...] ) ]
where option can be one of:
FORMAT format_name
DELIMITER 'delimiter_character'
NULL 'null_string'
HEADER [ boolean ]
QUOTE 'quote_character'
ESCAPE 'escape_character'
FORCE_QUOTE { ( column_name [, ...] ) | * }
FORCE_NOT_NULL ( column_name [, ...] )
ENCODING 'encoding_name'
Parameters
| Parameter | Description |
|---|---|
table_name |
Name of the Hologres table. |
query |
A SELECT query whose results are exported. |
STDIN |
Reads data from standard input on the client. |
STDOUT |
Writes data to standard output on the client. |
FORMAT |
File format: TEXT (default), CSV, or BINARY. Only data export and FIXED COPY mode data import support BINARY format. |
DELIMITER |
Field separator character. Default: tab for TEXT format, comma (,) for CSV format. Example: DELIMITER AS ','. |
NULL |
String to represent null values. Default: \N for TEXT format, unquoted empty string for CSV format. Not supported for BINARY format. |
HEADER |
When set, the first row contains column names. CSV format only. |
QUOTE |
Quote character surrounding field values. Must be a single-byte character. Default: double quote ("). CSV format only. |
ESCAPE |
Character that precedes a quote character within a field value. Must be a single-byte character. Default: matches the QUOTE value. CSV format only. |
FORCE_QUOTE |
Forces quoting of all non-null values in the specified columns. COPY TO with CSV format only. |
FORCE_NOT_NULL |
Treats empty strings as zero-length strings instead of null values in the specified columns. COPY FROM with CSV format only. |
ENCODING |
File encoding. Default: current client encoding. |
Import data
Import inline data with STDIN
Use COPY FROM STDIN to load data entered directly in the psql session. Terminate the input with \. on its own line.
-- Create the table
CREATE TABLE copy_test (
id int,
age int,
name text
);
-- Import inline data
COPY copy_test FROM STDIN WITH DELIMITER AS ',' NULL AS '';
53444,24,wangming
55444,38,ligang
55444,38,luyong
\.
-- Verify the import
SELECT * FROM copy_test;
To import a CSV file in the same way:
-- Create the table
CREATE TABLE partsupp (
ps_partkey integer not null,
ps_suppkey integer not null,
ps_availqty integer not null,
ps_supplycost float not null,
ps_comment text not null
);
-- Import a CSV file with pipe delimiter
COPY partsupp FROM STDIN WITH DELIMITER '|' CSV;
1|2|3325|771.64|final theodolites
1|25002|8076|993.49|ven ideas
\.
-- Verify the import
SELECT * FROM partsupp;
COPY FROM STDIN is supported only in the psql client. DataStudio and HoloWeb do not support command-line data import.
Import a local file from the terminal
To load a local file, pipe it to Hologres using the psql -c flag:
psql -U <username> -p <port> -h <endpoint> -d <databasename> \
-c "COPY <table> FROM STDIN WITH DELIMITER '|' CSV;" < <filename>
| Parameter | Description | Example |
|---|---|---|
<username> |
Your AccessKey ID (for Alibaba Cloud accounts) or username (for custom accounts, e.g., BASIC$abc). Store credentials in environment variables to avoid exposing them. |
LTAI5tXxx |
<port> |
Public port of the Hologres instance. | 80 |
<endpoint> |
Public endpoint of the Hologres instance. | xxx-cn-hangzhou.hologres.aliyuncs.com |
<databasename> |
Name of the Hologres database. | mydb |
<table> |
Name of the target table. | copy_test |
<filename> |
Path to the local file. | D:\tmp\copy_test.csv |
The following example imports a file named copy_test into Hologres:
The file contains:
01,01,name1
02,01,name2
03,01,name3
04,01,name4
After the import completes, query the table in the psql client to verify the data:
Export data
Export with \copy in psql
The \copy meta-command exports query results directly to a local file. This is the simplest export method and works entirely within the psql session.
-- Create and populate the table
CREATE TABLE copy_to_local (
id int,
age int,
name text
);
INSERT INTO copy_to_local VALUES
(1, 1, 'a'),
(1, 2, 'b'),
(1, 3, 'c'),
(1, 4, 'd');
-- Export to a local file
\copy (SELECT * FROM copy_to_local) TO '/root/localfile.txt';
\copy is supported only in the psql client.
Export to a local file from the terminal
To export data from the terminal and save it to a local file, redirect STDOUT to a file:
psql -U <username> -p <port> -h <endpoint> -d <databasename> \
-c "COPY (SELECT * FROM <tablename>) TO STDOUT WITH DELIMITER '|' CSV;" > <filename>
Replace <username>, <port>, <endpoint>, <databasename>, and <filename> with your actual values (see the parameter table in Import a local file from the terminal).
Import and export with CopyManager (JDBC)
The PostgreSQL JDBC driver's CopyManager class lets you import and export data programmatically in Java applications.
Store your AccessKey ID and AccessKey secret in environment variables rather than hardcoding them in source code.
Import a file using CopyManager
package com.aliyun.hologram.test.jdbc;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;
public class jdbcCopyFile {
public static void main(String args[]) throws Exception {
System.out.println(copyFromFile(getConnection(), "/Users/feng/Workspace/region.tbl", "region"));
}
public static Connection getConnection() throws Exception {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://endpoint:port/dbname";
Properties props = new Properties();
// AccessKey ID of your current account. Use environment variables to avoid exposing credentials.
props.setProperty("user", "******");
// AccessKey secret of your current account. Use environment variables to avoid exposing credentials.
props.setProperty("password", "******");
return DriverManager.getConnection(url, props);
}
/**
* Imports a local file into a Hologres table.
*
* @param connection Active database connection
* @param filePath Path to the local file
* @param tableName Target table name
* @return Number of rows imported
*/
public static long copyFromFile(Connection connection, String filePath, String tableName)
throws SQLException, IOException {
long count = 0;
FileInputStream fileInputStream = null;
try {
CopyManager copyManager = new CopyManager((BaseConnection) connection);
fileInputStream = new FileInputStream(filePath);
count = copyManager.copyIn("COPY " + tableName + " FROM STDIN DELIMITER '|' CSV", fileInputStream);
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return count;
}
}
Export data using CopyManager
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class copy_to_local_file {
public static void main(String args[]) throws Exception {
System.out.println(copyToFile(getConnection(), "/Users/feng/Workspace/region.tbl", "SELECT * FROM region"));
}
public static Connection getConnection() throws Exception {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://endpoint:port/dbname";
Properties props = new Properties();
// AccessKey ID of your current account. Use environment variables to avoid exposing credentials.
props.setProperty("user", "******");
// AccessKey secret of your current account. Use environment variables to avoid exposing credentials.
props.setProperty("password", "******");
return DriverManager.getConnection(url, props);
}
/**
* Exports Hologres data to a local file.
*
* @param connection Active database connection
* @param filePath Path to the output file
* @param SQL_Query SELECT query to export
* @return Path of the output file
*/
public static String copyToFile(Connection connection, String filePath, String SQL_Query)
throws SQLException, IOException {
FileOutputStream fileOutputStream = null;
try {
CopyManager copyManager = new CopyManager((BaseConnection) connection);
fileOutputStream = new FileOutputStream(filePath);
copyManager.copyOut("COPY (" + SQL_Query + ") TO STDOUT DELIMITER '|' CSV", fileOutputStream);
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return filePath;
}
}
Import data using HoloWeb
HoloWeb provides a visual interface for uploading local files without using the command line.
-
Open HoloWeb. For connection instructions, see Connect to HoloWeb and run a query.
-
In the top menu bar, click Data Solutions.
-
In the left navigation pane, choose Import On-premises File > New Data Import.
-
On the Select Destination Table page, configure the following parameters.
Parameter Description Job name Name of the import job. Instance name Select the logged-in Hologres instance. Destination database Name of the target database in the selected instance. Target Schema Schema that contains the target table. Defaults to public if no custom schemas exist. Select the data table to import Name of the target table. Create the table before starting the import. 
-
Click Next and configure the file upload parameters.
Parameter Description Select file Local file to upload. Supported formats: TXT, CSV, and LOG. The column order and count in the file must match the table definition. Select delimiter Field delimiter: Comma, Tab, Semicolon, Space, |, #, &, or a custom character. Source character set File encoding: GBK, UTF-8, CP936, or ISO-8859. First row as header Select this option if the first row contains column names. -
Click Next. On the Confirm Import Information page, review the destination schema, database, and table details, then click Upload.