PostgreSQL 服务端COPY和客户端COPY - 暨PG有哪些服务端操作接口

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

PostgreSQL , copy , 服务端接口 , 客户端接口


背景

有些朋友在使用COPY命令对数据库进行数据导入导出时,可能会发现COPY到文件时,怎么和想象的不一样,COPY到文件并没有落到指定的地方。实际上原因是COPY命令是服务端命令,COPY到某个文件实际上是COPY到数据库所在的服务器上面了,而不是客户端所在的服务器。

Oracle数据库也一样,都有服务端和客户端(协议端)概念。

PostgreSQL数据库还有哪些接口是服务端操作的,哪些接口是客户端操作的?

一、大对象

客户端接口

https://www.postgresql.org/docs/10/static/lo-interfaces.html

服务端接口

https://www.postgresql.org/docs/10/static/lo-funcs.html

例子

《PostgreSQL 大对象或bytea存取pdf,jpg等文件 - PostgreSQL export regular file like pdf, word, text, doc stored in bytea type》

二、文件读写接口

服务端文件读(写)接口

https://www.postgresql.org/docs/10/static/functions-admin.html#FUNCTIONS-ADMIN-GENFILE

服务端 FILE_FDW 接口

https://www.postgresql.org/docs/10/static/file-fdw.html

三、COPY 数据接口

服务端COPY接口

https://www.postgresql.org/docs/10/static/sql-copy.html

copy tbl to 'filepath';

copy tbl from 'filepath';

这里的filepath都是指PostgreSQL数据库所在服务器的路径。

需要数据库superuser权限。

客户端COPY接口

1、标准输入、输出

copy tbl to stdout;

copy (query) to stdout;

copy tbl from stdin;

不需要数据库superuser权限。

通过标准输出、输入,可以实现COPY接收来自客户端的数据。

通过封装管道,则可以实现将客户端的文件加载到数据库中,或者将数据写到客户端。

cat file|psql -c "copy tbl from stdin"

psql -c "copy tbl to stdout" > local.csv

2、libpq COPY

同时数据库还支持libpq协议的COPY语法,通过libpq协议将数据导入PG,或者从PG导出到客户端。

https://www.postgresql.org/docs/10/static/libpq-copy.html

不需要数据库superuser权限。

3、psql 客户端copy接口

最后psql客户端,内部封装了copy接口,通过libpq协议的COPY接口与数据库交互。

https://www.postgresql.org/docs/10/static/app-psql.html#APP-PSQL-VARIABLES

不需要数据库superuser权限。


\copy { table [ ( column_list ) ] | ( query ) } { from | to } { 'filename' | program 'command' | stdin | stdout | pstdin | pstdout } [ [ with ] ( option [, ...] ) ]
    Performs a frontend (client) copy. This is an operation that runs an SQLCOPY(7) command, but instead of the server reading or writing the specified file, 
    psql reads or writes the file and routes the data between the
    server and the local file system. This means that file accessibility and privileges are those of the local user, 
    not the server, and no SQL superuser privileges are required.

    When program is specified, command is executed by psql and the data passed from or to command is routed between 
    the server and the client. Again, the execution privileges are those of the local user, not the server, and
    no SQL superuser privileges are required.

    For \copy ... from stdin, data rows are read from the same source that issued the command, continuing until \.  
    is read or the stream reaches EOF. This option is useful for populating tables in-line within a SQL script
    file. For \copy ... to stdout, output is sent to the same place as psql command output, 
    and the COPY count command status is not printed (since it might be confused with a data row). 
    To read/write psql's standard input
    or output regardless of the current command source or \o option, write from pstdin or to pstdout.

    The syntax of this command is similar to that of the SQLCOPY(7) command. All options other than the data 
    source/destination are as specified for COPY(7). Because of this, special parsing rules apply to the \copy
    meta-command. Unlike most other meta-commands, the entire remainder of the line is always taken to be 
    the arguments of \copy, and neither variable interpolation nor backquote expansion are performed in the arguments.

        Tip
        This operation is not as efficient as the SQLCOPY command because all data must pass through the client/server connection. For large amounts of data the SQL command might be preferable.

例子

服务端copy命令依赖超级用户

postgres=> copy pgbench_accounts to '/home/digoal/123.csv' with (format csv);
ERROR:  must be superuser or a member of the pg_write_server_files role to COPY to a file
HINT:  Anyone can COPY to stdout or from stdin. psql's \copy command also works for anyone.
Time: 0.588 ms


psql封装的\copy,使用libpq copy接口,不依赖超级用户
postgres=> \copy pgbench_accounts to '/home/digoal/123.csv' with (format csv);
COPY 2000000
Time: 1590.136 ms (00:01.590)

postgres=> truncate pgbench_accounts ;
TRUNCATE TABLE
Time: 180.988 ms
postgres=> \copy pgbench_accounts from '/home/digoal/123.csv' with (format csv);
COPY 2000000
Time: 5733.883 ms (00:05.734)

注意,很多人会误以为psql提供的copy和数据库服务端的copy命令一致,实际上并不是这样的,比如Greenplum提供的服务端COPY,支持log errors, reject error rows的语法,而psql的客户端copy可能并不支持(只是pg社区提供的psql客户端肯定不支持。gpdb提供的psql客户端尚不清楚).如果你的服务端是gpdb,并且需要log errors,那么应该使用服务端COPY命令,使用stdin, stdout来进出。

参考

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
3月前
|
存储 关系型数据库 Java
polardb有没有搞过pg 全量及增量备份管理的
【1月更文挑战第3天】【1月更文挑战第11篇】 polardb有没有搞过pg 全量及增量备份管理的
33 1
|
2月前
|
SQL 存储 缓存
PostgreSQL函数管理接口
学习PostgreSQL服务端开发必须要对函数管理接口有比较深入的了解
142 0
|
27天前
|
负载均衡 监控 关系型数据库
PostgreSQL从小白到高手教程 - 第48讲:PG高可用实现keepalived
PostgreSQL技术大讲堂 - 第48讲:PG高可用实现keepalived
64 1
|
2月前
|
关系型数据库 分布式数据库 PolarDB
PolarDB for PostgreSQL下载问题之客户端 X-Paxos下载失败如何解决
PolarDB for PostgreSQL是基于PostgreSQL开发的一款云原生关系型数据库服务,它提供了高性能、高可用性和弹性扩展的特性;本合集将围绕PolarDB(pg)的部署、管理和优化提供指导,以及常见问题的排查和解决办法。
|
2月前
|
SQL 关系型数据库 数据库
PostgreSQL从小白到高手教程 - 第44讲:pg流复制部署
PostgreSQL技术大讲堂 - 第44讲:pg流复制部署
54 0
|
3月前
|
关系型数据库 分布式数据库 PolarDB
在PolarDB中,对于join操作,系统会采用拉取内表
【1月更文挑战第21天】【1月更文挑战第103篇】在PolarDB中,对于join操作,系统会采用拉取内表
19 1
|
4月前
|
SQL 关系型数据库 数据库
postgresql|数据库|pg数据库的文件系统详解---最全面的解析
postgresql|数据库|pg数据库的文件系统详解---最全面的解析
112 0
|
4月前
|
安全 关系型数据库 数据库
上新|阿里云RDS PostgreSQL支持PG 16版本,AliPG提供丰富自研能力
AliPG在社区版16.0的基础上,在安全、成本、可运维性等多个方面做了提升,丰富的内核/插件特性支持,满足业务场景的需求
|
4月前
|
关系型数据库 数据库 PostgreSQL
flink postgresql cdc实时同步(含pg安装配置等)
flink postgresql cdc实时同步(含pg安装配置等)
150 0
|
5月前
|
安全 关系型数据库 分布式数据库
polardb大表修改字符集,怎么操作风险最低
polardb大表修改字符集,怎么操作风险最低
32 1

相关产品

  • 云原生数据库 PolarDB