微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

怎么使用PostgreSQL中的COPY命令

这篇文章主要讲解了“怎么使用Postgresql中的copY命令”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么使用Postgresql中的copY命令”吧!

copy命令在PG 12有所增强,在copY FROM时可添加WHERE条件过滤.

PG 11
copy命令

testdb=# \help copy
Command:     copY
Description: copy data between a file and a table
Syntax:
copY table_name [ ( column_name [, ...] ) ]
    FROM { 'filename' | PROGRAM 'command' | STDIN }
    [ [ WITH ] ( option [, ...] ) ]
copY { table_name [ ( column_name [, ...] ) ] | ( query ) }
    TO { 'filename' | PROGRAM 'command' | STDOUT }
    [ [ WITH ] ( option [, ...] ) ]
where option can be one of:
    FORMAT format_name
    OIDS [ boolean ]
    FREEZE [ boolean ]
    DELIMITER 'delimiter_character'
    NULL 'null_string'
    HEADER [ boolean ]
    QUOTE 'quote_character'
    ESCAPE 'escape_character'
    FORCE_QUOTE { ( column_name [, ...] ) | * }
    FORCE_NOT_NULL ( column_name [, ...] )
    FORCE_NULL ( column_name [, ...] )
    ENCODING 'encoding_name'

简单使用

testdb=# drop table if exists t_copy;
DROP TABLE
testdb=# CREATE TABLE t_copy(id int,c1 varchar(20));
CREATE TABLE
testdb=# insert into t_copy SELECT x,'c1-'||x FROM generate_series(1, 1000) AS x;
INSERT 0 1000
testdb=# 
testdb=# copY t_copy TO '/tmp/data/t_copy.txt' with DELIMITER '|';
copY 1000
testdb=# drop table if exists t_import;
DROP TABLE
testdb=# CREATE TABLE t_import(id int,c1 varchar(20));
CREATE TABLE
testdb=# copY t_import FROM '/tmp/data/t_copy.txt'  with DELIMITER '|';
copY 1000
testdb=# select * from t_import limit 10;
 id |  c1   
----+-------
  1 | c1-1
  2 | c1-2
  3 | c1-3
  4 | c1-4
  5 | c1-5
  6 | c1-6
  7 | c1-7
  8 | c1-8
  9 | c1-9
 10 | c1-10
(10 rows)

不支持WHERE条件过滤

testdb=# copY t_import FROM '/tmp/data/t_copy.txt'  with DELIMITER '|' where id < 5;
ERROR:  Syntax error at or near "where"
LINE 1: ...t FROM '/tmp/data/t_copy.txt'  with DELIMITER '|' where id <...

PG 12
copY命令语法

[local]:5432 pg12@testdb=# \help copy
Command:     copY
Description: copy data between a file and a table
Syntax:
copY table_name [ ( column_name [, ...] ) ]
    FROM { 'filename' | PROGRAM 'command' | STDIN }
    [ [ WITH ] ( option [, ...] ) ]
    [ WHERE condition ]
copY { table_name [ ( column_name [, ...] ) ] | ( query ) }
    TO { 'filename' | PROGRAM 'command' | STDOUT }
    [ [ WITH ] ( option [, ...] ) ]
where option can be one of:
    FORMAT format_name
    FREEZE [ boolean ]
    DELIMITER 'delimiter_character'
    NULL 'null_string'
    HEADER [ boolean ]
    QUOTE 'quote_character'
    ESCAPE 'escape_character'
    FORCE_QUOTE { ( column_name [, ...] ) | * }
    FORCE_NOT_NULL ( column_name [, ...] )
    FORCE_NULL ( column_name [, ...] )
    ENCODING 'encoding_name'
URL: https://www.postgresql.org/docs/12/sql-copy.html

支持WHERE条件过滤

[local]:5432 pg12@testdb=# drop table if exists t_copy;
DROP TABLE
Time: 50.327 ms
[local]:5432 pg12@testdb=# CREATE TABLE t_copy(id int,c1 varchar(20));
CREATE TABLE
Time: 5.038 ms
[local]:5432 pg12@testdb=# insert into t_copy SELECT x,'c1-'||x FROM generate_series(1, 1000) AS x;
INSERT 0 1000
Time: 16.422 ms
[local]:5432 pg12@testdb=# 
[local]:5432 pg12@testdb=# copY t_copy TO '/tmp/data/t_copy.txt' with DELIMITER '|';
copY 1000
Time: 4.795 ms
[local]:5432 pg12@testdb=# drop table if exists t_import;
DROP TABLE
Time: 4.798 ms
[local]:5432 pg12@testdb=# CREATE TABLE t_import(id int,c1 varchar(20));
CREATE TABLE
Time: 2.462 ms
[local]:5432 pg12@testdb=# copY t_import FROM '/tmp/data/t_copy.txt'  with DELIMITER '|' WHERE id < 5;
copY 4
Time: 4.842 ms
[local]:5432 pg12@testdb=# select * from t_import;
 id |  c1  
----+------
  1 | c1-1
  2 | c1-2
  3 | c1-3
  4 | c1-4
(4 rows)
Time: 6.103 ms

感谢各位的阅读,以上就是“怎么使用Postgresql中的copY命令”的内容了,经过本文的学习后,相信大家对怎么使用Postgresql中的copY命令这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程之家,小编将为大家推送更多相关知识点的文章,欢迎关注!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐