首页 技术 正文
技术 2022年11月10日
0 收藏 584 点赞 4,227 浏览 2012 个字

COPY 命令可以快速的导入数据到postgresql数据库中,文件格式类似TXT、CVS之类。适合批量导入数据,速度比较快。注意COPY只能用于表,不能用于视图。

COPY 命令里面的文件必须是由服务器直接读或写的文件,而不是由客户端应用读写。因此,它们必须位于数据库服务器上或者可以为数据库服务器所访问,而不是由客户端做这些事情。它们必须是PostgresqlSQL用户(服务器运行的用户 ID)可以访问到并且可读或者可写,而不是客户端。 COPY 到一个命名文件是只允许数据库超级用户进行的,因为它允许读写任意服务器有权限访问的文件。

导入文件或者 STDIN到表中

导出表数据到文件或 STDOUT

copy命令可以操作的文件类型有:txt、sql、csv、压缩文件、二进制格式

1、 copy命令导入数据示例:tb2是表名,delimiter ‘,’ 表示按逗号分隔字段数据

postgresql=# copy tb2 from '/mnt/postgresql/weibo.1000'delimiter ',';
COPY1000
postgresql=# select count(*)from tb2;
count
-------
1000
(1 row)

2、 copy命令导入导出数据为sql格式

postgresql=# COPY tb2 TO '/mnt/postgresql/weibo.sql';
COPY1000
postgresql=# COPY tb2 from '/mnt/postgresql/weibo.sql';
COPY2000

3、 copy命令导出指定字段数据在控制台

postgresql=# COPY tb2 (t1,t2,t3) TO STDOUT;
21317568596 1270505818
21317568149 2302617224
21317568470 1297983318
21317568110 2069993004 2302781822
21317568354 362106137
21317568308 1450475836
21317568584 83103917
21317568208 1844532765 1713926427
21317568603 1227221083 2478474742
21317568151 1430992492 1253397461
21317567390 1037539510

4、copy命令导入导出数据为csv格式

postgresql=# COPY  tb2 (t1,t2,t3) TO '/mnt/postgresql/weibo.csv' CSV HEADER;
COPY2000
postgresql=# COPY tb2 from '/mnt/postgresql/weibo1.csv';
COPY2000

5、 copy命令导入导出数据为txt格式

postgresql=#   COPY tb2 TO '/mnt/postgresql/weibo.txt';
COPY2000
postgresql=# COPY tb2 from '/mnt/postgresql/weibo.txt';
COPY2000

6、 copy命令导出数据为压缩文件

postgresql=# COPY tb2 TOPROGRAM 'gzip >/mnt/postgresql/weibo.1000.gz';
COPY2000

7、 copy命令导入导出文件为二进制

用copyto命令以二进制形式把tb2表的内容拷贝到binary文件中

postgresql=# copy binary tb2 to'/mnt/postgresql/binary';
COPY 8000

用copyfrom命令把binary文件中的数据拷贝到表从tb2中

postgresql=# copy binary tb2 from'/mnt/postgresql/binary';
COPY 8000

7、将excel表中的数据导入到Postgresql数据库的某张表中。

步骤:

1.将excel表格字段,按照postgresql数据库中表的字段顺序来整理数据,并保存为csv文件。

2.用记事本打开csv文件,另存为UTF-8格式。

3.使用客户端链接postgresql数据库,执行如下脚本,导入csv文件到Postgresql数据表:

  copy testdatafrom 'd:/test/testdata.csv' delimiter as',' csv quote as '"'

注:testdata是postgresql数据库表的名称。

注意事项:

1.test目录需要赋予postgresql用户可读写的权限,否则会有如下报错信息:

  ERROR: could not open file "d:/testdata2.csv" forwriting:Permission denied

2.csv文件要为utf-8格式,否则导入时可能会有报错:

  ERROR:invalid bytesequence for encoding "UTF8": 0xcdf5
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295