PostgreSQL(二)常用命令整理

一、常用命令

登录命令:

psql -h IP -p 端口 -U 用户名 -d 数据库名 -W
  • \? 所有命令帮助
  • \l 列出所有数据库
  • \d 列出数据库中所有表
  • \dt 列出数据库中所有表
  • \d [table_name] 显示指定表的结构
  • \di 列出数据库中所有 index 索引
  • \dv 列出数据库中所有 view 视图
  • \h sql命令帮助
  • \q 退出连接
  • \c [database_name] 切换到指定的数据库
  • \c 显示当前数据库名称和用户
  • \conninfo 显示客户端的连接信息
  • \du 显示所有用户
  • \dn 显示数据库中的schema
  • \encoding 显示字符集
  • select version(); 显示版本信息
  • \i testdb.sql 执行sql文件
  • \x 扩展展示结果信息,相当于MySQL的\G
  • \o /tmp/test.txt 将下一条sql执行结果导入文件中

查看版本:

select version();

在这里插入图片描述


二、用户

2.1 创建账号

创建用户:

create user 用户名 password '密码';

设置只读权限:

alter user 用户名 set default_transaction_read_only = on;

设置可操作的数据库:

grant all on database 数据库名 to 用户名;

授权可操作的模式和权限:

-- 授权
grant select on all tables in schema public to 用户名;
-- 授权
GRANT ALL ON TABLE public.user TO mydata;
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE public.user TO mydata_dml;
GRANT SELECT ON TABLE public.user TO mydata_qry;

2.2 删除账号

撤回在public模式下的权限:

revoke select on all tables in schema public from 用户名;

撤回在information_schema模式下的权限:

revoke select on all tables in schema information_schema from 用户名;

撤回在pg_catalog模式下的权限:

revoke select on all tables in schema pg_catalog from 用户名;

撤回对数据库的操作权限:

revoke all on database 数据库名 from 用户名;

删除用户:

drop user 用户名;

三、权限

3.1 授权

设置只读权限:

alter user 用户名 set default_transaction_read_only = on;

设置可操作的数据库:

grant all on database 数据库名 to 用户名;

设置可操作的模式和权限:

grant select,insert,update,delete on all tables in schema public to 用户名;

3.2 撤回权限

撤回在public模式下的权限:

revoke select on all tables in schema public from 用户名;

撤回在information_schema模式下的权限:

revoke select on all tables in schema information_schema from 用户名;

撤回在pg_catalog模式下的权限:

revoke select on all tables in schema pg_catalog from 用户名;

撤回对数据库的操作权限:

revoke all on database 数据库名 from 用户名;

四、模式 Schema

创建和当前用户同名模式(schema):

注意:用户名与 schema 同名,且用户具有访问改 schema 的权限,用户连入数据库时,默认即为当前 schema。

create schema AUTHORIZATION CURRENT_USER;

自定义创建模式(schema):

create schema 模式名称;

注意:如果不创建scheme,并且语句中不写scheme,则默认scheme使用内置的public

查看数据库下的所有(schema):

select * from information_schema.schemata;

五、数据库

查询所有数据库:

select datname from pg_database;

创建数据库:

create database 数据库名 owner 所属用户 encoding UTF8;

注意:创建完数据库,需要切换到数据库下,创建和当前用户同名scheme,删除数据库后schema也会一并删除:

-- 重新登陆到新数据库下,执行如下语句
create schema AUTHORIZATION CURRENT_USER;

删除数据库:

drop database 数据库名;

注意:删库前需要关闭所有会话,不然会提示:

ERROR:  database "mydb" is being accessed by other users
DETAIL:  There are 8 other sessions using the database.

关闭数据库所有会话:

SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE datname='mydb' AND pid<>pg_backend_pid();

六、表

查询schema中所有表:

select table_name from information_schema.tables where table_schema = 'myuser';

创建表:

CREATE TABLE public.t_user (
  "id" BIGSERIAL NOT NULL,
  "username" VARCHAR(64) NOT NULL,
  "password" VARCHAR(64) NOT NULL,
  "create_time" TIMESTAMP(0) default CURRENT_TIMESTAMP not null,
  "update_time" TIMESTAMP(0) default CURRENT_TIMESTAMP not null
);
-- 注释
COMMENT ON TABLE public.t_user IS '用户表';
COMMENT ON COLUMN public.t_user.id IS '主键';
COMMENT ON COLUMN public.t_user.username IS '用户名';
COMMENT ON COLUMN public.t_user.password IS '密码';
COMMENT ON COLUMN public.t_user.create_time IS '创建时间';
COMMENT ON COLUMN public.t_user.update_time IS '更新时间';
-- 创建自增序列
alter sequence "t_user_ID_seq" restart with 1 increment by 1;
-- 创建主键序列
drop index if exists "t_user_pkey";
alter table "t_user" add constraint "t_user_pkey" primary key ("ID");

根据已有表结构创建表:

create table if not exists 新表 (like 旧表 including indexes including comments including defaults);

删除表:

drop table if exists "t_template" cascade;

查询注释:

SELECT
a.attname as "字段名",
col_description(a.attrelid,a.attnum) as "注释",
concat_ws('',t.typname,SUBSTRING(format_type(a.atttypid,a.atttypmod) from '(.*)')) as "字段类型"
FROM
pg_class as c,
pg_attribute as a,
pg_type as t
WHERE
c.relname = 't_batch_task'
and a.atttypid = t.oid
and a.attrelid = c.oid
and a.attnum>0;

七、索引

创建索引:

drop index if exists t_user_username;
create index t_user_username on t_user (username);

创建唯一索引:

drop index if exists t_user_username;
create unique index t_user_username on t_user (username);

查看索引:

\d t_user

删除索引:

drop index t_user_username;

更新ID序列:

select setval('t_sys_menu_id_seq',(select max(id) from t_sys_menu));

八、查询SQL

注意:PostgreSQL中的字段大小写敏感,而且只认小写字母,查询时需注意。

其他与基本sql大致相同。

8.1 to_timestamp() 字符串转时间

select * from t_user
where create_time >= to_timestamp('2023-01-01 00:00:00', 'yyyy-mm-dd hh24:MI:SS');

8.2 to_char 时间转字符串

select to_char(create_time, 'yyyy-mm-dd hh24:MI:SS') from t_user;

8.3 时间加减

-- 当前时间加一天
SELECT NOW()::TIMESTAMP + '1 day';
SELECT NOW() + INTERVAL '1 DAY';
SELECT now()::timestamp + ('1' || ' day')::interval
-- 当前时间减一天
SELECT NOW()::TIMESTAMP + '-1 day';
SELECT NOW() - INTERVAL '1 DAY';
SELECT now()::timestamp - ('1' || ' day')::interval
-- 加1年1月1天1时1分1秒
select NOW()::timestamp + '1 year 1 month 1 day 1 hour 1 min 1 sec';

8.4 like模糊查询

SELECT * FROM 表名 WHERE 字段 LIKE ('%关键字%');

8.5 substring字符串截取

--从第一个位置开始截取,截取4个字符,返回结果:Post
SELECT SUBSTRING ('PostgreSQL', 1, 4);
-- 从第8个位置开始截取,截取到最后一个字符,返回结果:SQL
SELECT SUBSTRING ('PostgreSQL', 8);
--正则表达式截取,截取'gre'字符串
SELECT SUBSTRING ('PostgreSQL', 'gre');

九、关联更新、关联删除SQL

9.1 关联更新

更新单列数据:

update table1 a set address = (select address from table2 b where a.id=b.id);

更新多列数据:

update table1 a set (address,people_cnt) = (select address,people_cnt from table2 b where a.id=b.id);

9.2 关联删除

示例:

delete from table1 a using table2 where a.id=b.id and address='china'; 

十、执行sql脚本

方式一:先登录再执行

\i testdb.sql

方式二:通过psql执行

psql -d testdb -U postgres -f /pathA/xxx.sql

十一、导出

11.1 导出数据

写法一:

pg_dump -h localhost -p 5432 -U postgres --column-inserts -t table_name -f data_sql.sql database_name
  • --column-inserts 以带有列名的 INSERT 命令形式转储数据。
  • -t 只转储指定名称的表。(不指定具体表则导出整个库。)
  • -f 指定输出文件或目录名。

写法二:

直接使用 Linux 的 > 将内容写入到文件。

pg_dump -h localhost -p 5432 -U postgres --column-inserts -t table_name database_name > data_sql.sql

11.2 导出表结构

写法一:

pg_dump -h localhost -p 5432 -U postgres -s -t table_name -f struct_sql.sql database_name
  • --column-inserts 以带有列名的 INSERT 命令形式转储数据。
  • -t 只转储指定名称的表。(不指定具体表则导出整个库。)
  • -f 指定输出文件或目录名。

写法二:

直接使用 Linux 的 > 将内容写入到文件。

pg_dump -h localhost -p 5432 -U postgres -s -t table_name -f struct_sql.sql database_name

整理完毕,完结撒花~





参考地址:

1.PostgreSql常用命令,https://blog.youkuaiyun.com/weixin_48321825/article/details/121775011

2.Postgresql创建账号及删除账号详细命令,https://blog.youkuaiyun.com/qq_44322586/article/details/123084962

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不愿放下技术的小赵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值