postgreSQL copy与\copy的区别

区别是:

  • copy必须使用能够超级用户使用;
  • copy .. to file ,copy file to ..中的文件都是数据库服务器所在的服务器上的文件。
  • \copy 一般用户即可执行
  • \copy 保存或者读取的文件是在客户端所在的服务器

  比如当使用192.168.17.53连上192.168.17.52的数据库,使用copy tb1 to ‘/home/postgres/aa.txt’,该文件是存放在192.168.17.52上;
  当使用\copys时候就会把文件存放到客户端所在的服务器上,即使用\copy tb1 to ‘/home/postgres/aa.sql’,该文件是存放在192.168.17.53上;

环境:
host: 192.168.17.52
client:192.168.17.53

使用192.168.17.53登录到 192.168.17.52做演示:

- 1. 先到192.168.17.52的/home/postgres下看看当前的文件

[root@localhost postgres]# pwd
/home/postgres
[root@localhost postgres]# ls
archive  base  dump  pg_log  pp.sql  python  script  soft  tb10.csv  test.sql
[root@localhost postgres]# 

- 2. 使用copy命令进行导出

[postgres@localhost root]$ psql -h 192.168.17.52 -U postgres postgres
could not change directory to "/root": Permission denied
psql (9.3.5)
Type "help" for help.

postgres=# 
postgres=# copy bbs to '/home/postgres/bbs.sql';
COPY 31

使用copy的时候文件是保存在服务器端的,切换到192.168.17.52的/home/postgres下看看文件是否存在:

[root@localhost postgres]# pwd
/home/postgres
[root@localhost postgres]# ls
archive  base  bbs.sql  dump  pg_log  pp.sql  python  script  soft  tb10.csv  test.sql

- 3. 使用\copy命令进行复制:
在192.168.17.53下:

[postgres@localhost ~]$ pwd
/home/postgres
[postgres@localhost ~]$ ls
archive  archive_failover  base  pg_log  script  trigger
postgres=# \copy bbs to '/home/postgres/bbs.sql';
postgres=# 

在192.168.17.53下在看一下:

[postgres@localhost ~]$ pwd
/home/postgres
[postgres@localhost ~]$ ls
archive  archive_failover  base  bbs.sql  pg_log  script  trigger

使用\copy是备份到客户端上。

恢复的时候也是一样,使用copy是从服务端寻找文件,使用\copy是从客户端上寻找文件。

参考: http://francs3.blog.163.com/blog/static/40576727201412135333388/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值