This commit is contained in:
2023-10-25 14:53:54 +08:00
parent 37056237bb
commit 9e54ad2ffe
4 changed files with 162 additions and 12 deletions

View File

@ -1,4 +1,4 @@
### 数据库导出
## 数据库导出
#### 链接信息
@ -365,10 +365,6 @@ pattern参数
#### psql
#### pg_restore
@ -628,10 +624,14 @@ psql -Uicctestedb -dicctestedb -h192.168.53.123 -f ~/
### 实操数据导入导出
### 整库导出
使用 pg_dump 与 pg_restore 完成数据转移,适合整表 整库 迁移
#### 数据导出
```shell
#导出 iccedb库
# -O 不设置表归属,
@ -651,8 +651,10 @@ psql -Uicctestedb -dicctestedb -h192.168.53.123 -f ~/
#-c 指定恢复过程中清空目标数据库中的现有表
#--strict-names 指定严格遵守输入文件中的名称规范
#~/icc_data_20220719_test.sql 指定要恢复的输入文件路径
#-n revenue_mgt 恢复 revenue_mgt schema
#-j 4 并行化操作
./pg_restore -Uenterprisedb -dtest2 -h10.23.101.119 -F c -c -v --strict-names ~/icc_data_20220719_test.sql
./pg_restore -Uenterprisedb -dtest2 -h10.23.101.119 -j4 -nrevenue_mgt -F c -c -v --strict-names ~/icc_data_20220719_test.sql
```
@ -663,21 +665,23 @@ psql -Uicctestedb -dicctestedb -h192.168.53.123 -f ~/
#### COPY 导出导出部分数据
### 部分数据导出
COPY
```sh
sudo su - enterprisedb
psql edb
```
##### 导出
#### COPY 导出
```sql
COPY (select * from icc.tcn_stat
where afferent_date between to_date('20220701','yyyymmdd') and to_date('20220714','yyyymmdd') ) TO '/home/edbdata/test.csv' WITH csv;
```
##### 导入
#### COPY 导入
```sql
COPY tcn_stat_sales_volume_total FROM '/home/edbdata/tcn_stat_sales_volume_total.csv' WITH csv;

View File

@ -0,0 +1,51 @@
连接数据库, 默认的用户和数据库是postgres
psql -U user -d dbname
切换数据库,相当于mysql的use dbname
\c dbname
列举数据库相当于mysql的show databases
\l
列举表相当于mysql的show tables
\dt
查看表结构相当于desc tblname,show columns from tbname
\d tblname
\di 查看索引
创建数据库:
create database [数据库名];
删除数据库:
drop database [数据库名];
*重命名一个表:
alter table [表名A] rename to [表名B];
*删除一个表:
drop table [表名];
*在已有的表里添加字段:
alter table [表名] add column [字段名] [类型];
*删除表中的字段:
alter table [表名] drop column [字段名];
*重命名一个字段:
alter table [表名] rename column [字段名A] to [字段名B];
*给一个字段设置缺省值:
alter table [表名] alter column [字段名] set default [新的默认值];
*去除缺省值:
alter table [表名] alter column [字段名] drop default;
在表中插入数据:
insert into 表名 ([字段名m],[字段名n],......) values ([列m的值],[列n的值],......);
修改表中的某行某列的数据:
update [表名] set [目标字段名]=[目标值] where [该行特征];
删除表中某行数据:
delete from [表名] where [该行特征];
delete from [表名];--删空整个表
创建表:
create table ([字段名1] [类型1] <references 关联表名(关联的字段名)>;,[字段名2] [类型2],......<,primary key (字段名m,字段名n,...)>;);
\copyright 显示 PostgreSQL 的使用和发行条款
\encoding [字元编码名称]
显示或设定用户端字元编码
\h [名称] SQL 命令语法上的说明,用 * 显示全部命令
\prompt [文本] 名称
提示用户设定内部变数
\password [USERNAME]
securely change the password for a user
\q 退出 psql