PostgreSql 数据相关的命令
进入容器
docker exec -it f8911198f0d3 bash
进入postgresql客户端
psql -h localhost -U postgres -p 5432
#查看所有数据库
\l
创建数据库
create database 数据库名字(cp_user);
#进入某个数据库
\c 数据库名字(cp_user)
创建表
drop table if exists "public"."t_user";
create table "public"."t_user" (
"id" int4 not null,
"name" varchar(255) collate "pg_catalog"."default"
);
comment on column "public"."t_user"."id" is '编号';
comment on column "public"."t_user"."name" is '名称';
alter table "public"."t_user" add constraint "t_user_pkey" primary key ("id");
查看数据库中的表
\dt 表名 : user
插入数据
insert into "public"."t_user"("id", "name") values (1001, 'lisa');
insert into "public"."t_user"("id", "name") values (1002, 'jack');
#查看某个表的所有数据
select * from 表名
select * from t_user;
如果表名是user,因user是保留此,查询需要 select * from public.user;
修改某条数据
update "public"."t_user" set "name" = 'jack' where "id" = 1002;
删除某条数据
delete from t_user where id =1001
添加字段
alter table t_user add age varchar(32);
修改字段的注释
comment on column t_user.age is '年龄';
导出数据库
pg_dump -U 用户名 数据库名 > 导出文件名
导入数据库
psql -U 用户名 数据库名 < 导入文件名 :
#退出数据库
\q 或 ctrl + z
查询特定模式下的所有表名
select tablename from pg_tables where schemaname = 'public'`
备份数据库。
使用命令`pg_dump -U 'username' -p 'port number' -d 'database' > 'dump file name'`