暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练 第14天 导出数据

原创 Lily_tj 2021-12-21
734

## 学习感受
继续学习,坚持就是胜利!
每天学习有点累,做作业发现之前学的语法忘记了,需要回来查资料,坚持再坚持!!

## 学习目标

学习openGauss导出数据



## 课后作业
连接openGauss

#第一次进入等待15秒
#数据库启动中...
```
su - omm
gsql -r
```



1.创建数据库tpcc,在数据库tpcc中创建模式schema1,在模式schema1中建表products



```

create database tpcc;

\c tpcc
create schema schema1;
CREATE TABLE schema1.products
( product_id integer,
product_name char(30),
category char(20)
);


insert into schema1.products values(1001,'aaa1','qasq'),(1002,'aaa2','dddd'),(1003,'aaa3','addf');


\q
```


2.使用gs_dump工具以文本格式导出数据库tpcc的全量数据


```

gs_dump -f /home/omm/backup_tpcc_all.sql tpcc -F p

more /home/omm/backup_tpcc_all.sql



```


3.使用gs_dump工具以文本格式导出模式schema1的定义


```

gs_dump -f /home/omm/backup_schema_all.sql tpcc -n schema1 -F p


```


4.使用gs_dump工具以文本格式导出数据库tpcc的数据,不包含定义


```
gs_dump -f /home/omm/backup_tpcc_data.sql tpcc -a -F p

more /home/omm/backup_tpcc_data.sql


```


5.删除表、模式和数据库


```
DROP table schema1.products ;
DROP schema schema1;
DROP database tpcc;

```

执行演示代码如下:
```

omm@modb:~$ gsql -r
gsql ((openGauss 2.0.0 build 78689da9) compiled at 2021-03-31 21:03:52 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

omm=# create database tpcc;
CREATE DATABASE
omm=#
omm=# \c tpcc
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "tpcc" as user "omm".
tpcc=# create schema schema1;
CREATE SCHEMA
tpcc=# CREATE TABLE schema1.products
tpcc-# ( product_id integer,
tpcc(# product_name char(30),
tpcc(# category char(20)
tpcc(# );
CREATE TABLE
tpcc=#
tpcc=#
tpcc=# tpcc=#
insert into schema1.products values(1001,'aaa1','qasq'),(1002,'aaa2','dddd'),(1003,'aaa3','addf');
INSERT 0 3

tpcc=# select * from schema1.products;
product_id | product_name | category
------------+--------------------------------+----------------------
1001 | aaa1 | qasq
1002 | aaa2 | dddd
1003 | aaa3 | addf
(3 rows)

tpcc=# \q

omm@modb:~$
omm@modb:~$ gs_dump -f /home/omm/backup_tpcc_all.sql tpcc -F p
gs_dump[port='5432'][tpcc][2021-12-17 22:10:28]: The total objects number is 389.
gs_dump[port='5432'][tpcc][2021-12-17 22:10:28]: [100.00%] 389 objects have been dumped.
gs_dump[port='5432'][tpcc][2021-12-17 22:10:28]: dump database tpcc successfully
gs_dump[port='5432'][tpcc][2021-12-17 22:10:28]: total time: 96 ms
omm@modb:~$ gs_dump -f /home/omm/backup_tpcc_all.sql tpcc-F p
gs_dump: too many command-line arguments (first is "p")
Try "gs_dump --help" for more information.
omm@modb:~$
omm@modb:~$ more /home/omm/backup_tpcc_all.sql
--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET xmloption = content;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET default_tablespace = '';
--More--(31%)SET client_min_messages = warning;

--
-- Name: schema1; Type: SCHEMA; Schema: -; Owner: omm
--

CREATE SCHEMA schema1;


ALTER SCHEMA schema1 OWNER TO omm;

SET search_path = schema1;


SET default_with_oids = false;

--
-- Name: products; Type: TABLE; Schema: schema1; Owner: omm; Tablespace:
--

CREATE TABLE products (
product_id integer,
product_name character(30),
category character(20)
)
WITH (orientation=row, compression=no);


ALTER TABLE schema1.products OWNER TO omm;

--
-- Data for Name: products; Type: TABLE DATA; Schema: schema1; Owner: omm
--

COPY products (product_id, product_name, category) FROM stdin;
1001 aaa1 qasq
1002 aaa2 dddd
1003 aaa3 addf
\.
;

--
-- Name: public; Type: ACL; Schema: -; Owner: omm
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM omm;
GRANT CREATE,USAGE ON SCHEMA public TO omm;
GRANT USAGE ON SCHEMA public TO PUBLIC;


--
-- PostgreSQL database dump complete
--

omm@modb:~$
omm@modb:~$
omm@modb:~$ gs_dump -f /home/omm/backup_schema_all.sql tpcc -n ds -F p
gs_dump[port='5432'][tpcc][2021-12-17 22:11:27]: no matching schemas were found for pattern "ds"
gs_dump[port='5432'][tpcc][2021-12-17 22:11:27]: No matching schemas were found
omm@modb:~$ gs_dump -f /home/omm/backup_schema_all.sql tpcc -n schema1 -F p
gs_dump[port='5432'][tpcc][2021-12-17 22:12:42]: The total objects number is 380.
gs_dump[port='5432'][tpcc][2021-12-17 22:12:42]: [100.00%] 380 objects have been dumped.
gs_dump[port='5432'][tpcc][2021-12-17 22:12:42]: dump database tpcc successfully
gs_dump[port='5432'][tpcc][2021-12-17 22:12:42]: total time: 88 ms
omm@modb:~$ gs_dump -f /home/omm/backup_tpcc_data.sql tpcc -a -F p
gs_dump[port='5432'][tpcc][2021-12-17 22:13:07]: dump database tpcc successfully
gs_dump[port='5432'][tpcc][2021-12-17 22:13:07]: total time: 79 ms
omm@modb:~$ DROP table schema1.products ;
-bash: DROP: command not found

omm@modb:~$ gsql -r
gsql ((openGauss 2.0.0 build 78689da9) compiled at 2021-03-31 21:03:52 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

omm=# DROP table schema1.products ;
ERROR: schema "schema1" does not exist

omm=# \c tpcc
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "tpcc" as user "omm".
tpcc=# DROP table schema1.products ;
DROP TABLE
tpcc=# DROP schema schema1;
DROP SCHEMA

omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
backup | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
omm | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/omm +
| | | | | omm=CTc/omm
template1 | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/omm +
| | | | | omm=CTc/omm
tpcc | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(6 rows)

omm=# \c omm
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "omm" as user "omm".
omm=# drop database tpcc


```

最后修改时间:2021-12-22 14:47:20
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论