PostgreSQL(二)常用命令整理
目录
- 一、常用命令
- 二、用户
- 2.1 创建账号
- 2.2 删除账号
- 三、权限
- 3.1 授权
- 2.2 撤回权限
- 四、模式 Schema
- 五、数据库
- 六、表
- 七、索引
- 八、查询SQL
- 8.1 to_timestamp() 字符串转时间
- 8.2 to_char 时间转字符串
- 8.3 时间加减
- 8.4 like模糊查询
- 九、执行sql脚本
- 十、导出数据到SQL文件
一、常用命令
登录命令:
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执行结果导入文件中
二、用户
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 用户名;
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 用户名;
四、模式 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 index t_user_username on t_user (username);
八、查询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 ('%关键字%');
九、执行sql脚本
方式一:先登录再执行
\i testdb.sql
方式二:通过psql执行
psql -d testdb -U postgres -f /pathA/xxx.sql
十、导出数据到SQL文件
pg_dump -h localhost -p 5432 -U postgres --column-inserts -t table_name -f save_sql.sql database_name
--column-inserts
以带有列名的INSERT
命令形式转储数据。-t
只转储指定名称的表。-f
指定输出文件或目录名。
整理完毕,完结撒花~
参考地址:
1.PostgreSql常用命令,https://blog.csdn.net/weixin_48321825/article/details/121775011
2.Postgresql创建账号及删除账号详细命令,https://blog.csdn.net/qq_44322586/article/details/123084962
PostgreSQL(二)常用命令整理相关推荐
- python常用命令汇总-python数据分析之pandas常用命令整理
原标题:python数据分析之pandas常用命令整理 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型 ,提供了高效地 ...
- CMake常用命令整理
CMake常用命令整理 转自:https://zhuanlan.zhihu.com/p/315768216 CMake 是什么我就不用再多说什么了,相信大家都有接触才会看一篇文章.对于不太熟悉的开发人 ...
- kubernetes常用命令整理
一.维护类: master节点: [root@k8s-master01 -]# for I in etcd kube-apiserver kube-controller-manager kube-sc ...
- Linux 常用命令整理(Centos7) (时区/正则/sed/htop/screen/ln/awk/……)
目录 Linux 常用命令整理(Centos7) (附加正则.时区.git等) 一.系统查询常用命令: 1.文件中空格制表符替换sed 2.查看宿主机的用户名密码 3.last .w.lastb 查看 ...
- 常用命令整理一(git、adb、vim、cmd、sh)
常用命令整理一(git.adb.vim.cmd.sh) 1.git篇 (1)安装git (2)配置 (3)使用 (4)基础概念 (5)gerrit相关 (6)git 指令遇到的一些问题 2.adb篇 ...
- PostgreSQL一些常用命令
最近一直在学习Postgresql,下面是自己整理的Postgresql的常用命令 连接数据库, 默认的用户和数据库是postgres psql -U user -d dbname 切换数据库,相当于 ...
- docker常用命令_docker常用命令整理
docker常用命令整理 代码狂魔 • 5 天前 • 技术积累 • 14 • 0 前面已经把docker是什么和 docker怎么安装已经写了,现在我将常用命令做一个整理,方便日后查阅 常用命令 do ...
- android 常用命令,Android开发常用命令整理
一些命令,不常用就忘记了,特整理在这里,忘了就查查.. · alias 用于定义和查询别名,但不保存.修改home目录下.bashrc保存定义别名格式,不加参数即查询别名:alias c ...
- 嵌入式Linux系统编程学习之二常用命令
嵌入式Linux系统编程学习之二常用命令 文章目录 嵌入式Linux系统编程学习之二常用命令 前言 一.常用命令 1.su(用户切换) 2.useradd(添加用户) 3.passwd(修改密码) 4 ...
- docker 容器 exited_docker常用命令整理
docker常用命令整理 代码狂魔 • 5 天前 • 技术积累 • 14 • 0 前面已经把docker是什么和 docker怎么安装已经写了,现在我将常用命令做一个整理,方便日后查阅 常用命令 do ...
最新文章
- 送100本书!涵盖Java 、大数据、推荐系统、机器学习、黑客、数据库、手游、少儿编程等!免费包邮!...
- 使用Hyperledger Ursa简化区块链安全性
- and or not 优先级_我的侠客公测成就奖励大全 成就解锁优先级排名
- 垂直网站架构改造 - 怎样成为一个山寨CTO
- php python-浅谈php调用python文件
- python 笔记:爱因斯坦求和 einsum
- 解决webview调用 goBack() 返回上一页自动刷新闪白的情况
- Spring Boot文档阅读笔记=Caching Data with Spring
- linux安装selenium、chromedriver、Chrome浏览器、BrowserMob Proxy(代理)爬虫爬站环境安装及测试实例
- devops测试_使用DevOps管道自动执行用户验收测试
- Windows修改远程端口号
- 重温微积分 —— 偏微分与链式法则
- 转:Oracle数据库一致性读的原理(Consistent Read)
- python中tell函数_PYTHON学习14.09:Python seek()和tell()函数详解
- 静默安装apk,静默卸载apk
- k380没有验证码_罗技K380蓝牙键盘
- Windows驱动之编写键盘记录器
- 中国橡胶防老剂4020市场现状与投资预测报告(2022版)
- MySQL事务原子性、一致性和持久性以及回滚是如何实现的?
- matlab模拟塞曼图谱,基于MATLAB的塞曼效应数值模拟
热门文章
- pystaller打包exe心酸史
- 上计算机课用什么耳机,在电脑上录课程用什么软件好?实现音画同步,画板标注就选它...
- html大作业网页代码 景点介绍网站设计——汉中印象景点介绍 (19页) HTML+CSS+JavaScript 学生旅行 游玩 主题住宿网页
- deepcamp面试日记
- Leetcode 957. N 天后的牢房 循环节
- [音视频拉普]无损格式flac是怎么无损的?假无损是什么概念?
- 数据库标准化之CDISC
- 软件工程--功能规格说明书
- 饥人谷 2019 前端押题(讲义)
- 高斯分布的各阶中心距和原点矩超强总结