博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Greenplum小把戏 - 快速复制无数据SCHEMA
阅读量:5756 次
发布时间:2019-06-18

本文共 3506 字,大约阅读时间需要 11 分钟。

  hot3.png

今天分享一条简单的命令,帮助大家尽快的复制一个新的SCHEMA。话不多说,看例子:

首先创建一个有三个表(t1\t2\t3)的新模式(test1):

postgres=# \d               List of relations Schema |   Name    | Type  | Owner  | Storage--------+-----------+-------+--------+--------- public | hgimdbtab | table | hgimdb | heap(1 row)postgres=# create schema test1;CREATE SCHEMApostgres=# create table test1.t1(id int);NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table.HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.CREATE TABLEpostgres=# insert into test1.t1 select generate_series(1,100);INSERT 0 100postgres=# select count(*) from test1.t1; count-------   100(1 row)postgres=# create table test1.t2(id int);NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table.HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.CREATE TABLEpostgres=# create table test1.t3(id int);NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table.HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.CREATE TABLEpostgres=# \dt test1.*            List of relations Schema | Name | Type  | Owner  | Storage--------+------+-------+--------+--------- test1  | t1   | table | hgimdb | heap test1  | t2   | table | hgimdb | heap test1  | t3   | table | hgimdb | heap(3 rows)

使用命令快速复制一个无数据模式:命令类似pg_dump -n test1 postgres |sed '1,${s/test1/test2/}'|psql postgres

[hgimdb@ps1 ~]$ pg_dump -n test1 postgres |sed '1,${s/test1/test2/}'|psql postgresSETSETSETSETSETSETCREATE SCHEMAALTER SCHEMASETSETCREATE TABLEALTER TABLECREATE TABLEALTER TABLECREATE TABLEALTER TABLE[hgimdb@ps1 ~]$ psqlpsql (8.3.23)Type "help" for help.postgres=# \dt test2.*            List of relations Schema | Name | Type  | Owner  | Storage--------+------+-------+--------+--------- test2  | t1   | table | hgimdb | heap test2  | t2   | table | hgimdb | heap test2  | t3   | table | hgimdb | heap(3 rows)postgres=# \dt test1.*            List of relations Schema | Name | Type  | Owner  | Storage--------+------+-------+--------+--------- test1  | t1   | table | hgimdb | heap test1  | t2   | table | hgimdb | heap test1  | t3   | table | hgimdb | heap(3 rows)postgres=# select count(*) from test1.t1; count-------   100(1 row)postgres=# select count(*) from test2.t1; count-------   100(1 row)postgres=#

这种方式不仅可以在同一个数据库下操作,也可以在不同的数据库中实现SCHEMA复制:

postgres=# create database tpch;CREATE DATABASEpostgres=# \q[hgimdb@ps1 ~]$ pg_dump -n test1 postgres |sed '1,${s/test1/test2/}'|psql tpchSETSETSETSETSETSETCREATE SCHEMAALTER SCHEMASETSETCREATE TABLEALTER TABLECREATE TABLEALTER TABLECREATE TABLEALTER TABLE[hgimdb@ps1 ~]$ psql -d tpchpsql (8.3.23)Type "help" for help.tpch=# \dNo relations found.tpch=# \dt test2.*            List of relations Schema | Name | Type  | Owner  | Storage--------+------+-------+--------+--------- test2  | t1   | table | hgimdb | heap test2  | t2   | table | hgimdb | heap test2  | t3   | table | hgimdb | heap(3 rows)tpch=# select count(*) from test2.t1; count-------   100(1 row)tpch=#

 

转载于:https://my.oschina.net/javacy/blog/2998074

你可能感兴趣的文章
采购申请转采购订单错误:在语言EN中没有维护短文本(请重维护物料460300080)
查看>>
Migration to S/4HANA
查看>>
sed 对目录进行操作
查看>>
什么是代码
查看>>
洛谷P2891 [USACO07OPEN]吃饭Dining
查看>>
Web 性能优化
查看>>
webpack最简单的入门教程里bundle.js之运行单步调试的原理解析
查看>>
如何安装Virtual Box的VBox Guest Additions扩展程序
查看>>
数据类型一
查看>>
腾讯云技术专家卢萌凯手把手教你Demo一个人脸识别程序!
查看>>
Array和常用API
查看>>
移动端开发单位——rem,动态使用
查看>>
vue实现选中效果
查看>>
一方app
查看>>
Go 标准库 http.FileServer 实现静态文件服务
查看>>
系列文章目录
查看>>
SVG 新手入门
查看>>
手把手教你如何提高神经网络的性能
查看>>
前端布局原理涉及到的相关概念总结
查看>>
递归调用 VS 循环调用
查看>>