怎么使用PostgreSQL中的COPY命令
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章主要讲解了"怎么使用PostgreSQL中的COPY命令",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"怎么使用PostgreSQL中的COPY
千家信息网最后更新 2026年10月31日怎么使用PostgreSQL中的COPY命令
这篇文章主要讲解了"怎么使用PostgreSQL中的COPY命令",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"怎么使用PostgreSQL中的COPY命令"吧!
Copy命令在PG 12有所增强,在COPY FROM时可添加WHERE条件过滤.
PG 11
Copy命令
testdb=# \help copyCommand: COPYDescription: copy data between a file and a tableSyntax: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 TABLEtestdb=# CREATE TABLE t_copy(id int,c1 varchar(20));CREATE TABLEtestdb=# insert into t_copy SELECT x,'c1-'||x FROM generate_series(1, 1000) AS x;INSERT 0 1000testdb=# testdb=# COPY t_copy TO '/tmp/data/t_copy.txt' with DELIMITER '|';COPY 1000testdb=# drop table if exists t_import;DROP TABLEtestdb=# CREATE TABLE t_import(id int,c1 varchar(20));CREATE TABLEtestdb=# COPY t_import FROM '/tmp/data/t_copy.txt' with DELIMITER '|';COPY 1000testdb=# 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 copyCommand: COPYDescription: copy data between a file and a tableSyntax: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 TABLETime: 50.327 ms[local]:5432 pg12@testdb=# CREATE TABLE t_copy(id int,c1 varchar(20));CREATE TABLETime: 5.038 ms[local]:5432 pg12@testdb=# insert into t_copy SELECT x,'c1-'||x FROM generate_series(1, 1000) AS x;INSERT 0 1000Time: 16.422 ms[local]:5432 pg12@testdb=# [local]:5432 pg12@testdb=# COPY t_copy TO '/tmp/data/t_copy.txt' with DELIMITER '|';COPY 1000Time: 4.795 ms[local]:5432 pg12@testdb=# drop table if exists t_import;DROP TABLETime: 4.798 ms[local]:5432 pg12@testdb=# CREATE TABLE t_import(id int,c1 varchar(20));CREATE TABLETime: 2.462 ms[local]:5432 pg12@testdb=# COPY t_import FROM '/tmp/data/t_copy.txt' with DELIMITER '|' WHERE id < 5;COPY 4Time: 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命令这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
命令
条件
学习
内容
支持
就是
思路
情况
文章
更多
知识
知识点
篇文章
语法
跟着
问题
实践
推送
研究
验证
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
有格互联网科技
新华互联网科技
广东猪兼强互联网科技有限公司
互联网科技产业
科技互联网公司装修
互联网科技有限公司图片
互联网科技汛
创业天下互联网科技有限公司
想开互联网科技公司怎么样
本届互联网博览会上的黑科技
畅思互联网科技
互联网科技创新的路径
互联网科技公司营业执照怎么办理
每天都有哪些好的互联网科技
皮皮虾互联网科技有限公司
从那以后互联网科技有限公司
东营紫格互联网科技
山东大美朱楼互联网科技有限公司
宁波允成互联网科技有限公司
如何强化互联网金融科技
互联网最新的科技
赫美联合互联网科技
互联网科技的未来发展
互联网科技论坛
汉立互联网科技有限公司联系电话
新华互联网科技2019
荆州市昕露互联网络科技有限公司
威龙国际互联网科技公司
广州百诚互联网科技有限公司官网
互联网公司属于科技吗