psql 命令总结
概念
首先,介绍下 PG 中常见的几个概念,包括了 Cluster、Database、Schemas 。
- Cluster:抽象概念,一系列 DBs 的集合,但是不像 DBs 可以直接通过 SQL 在内部表中查看。
- Databases:一系列 Schemas 的集合,在一个 Cluster 中名字必须唯一。
- Schemas:一系列 tables、functions、indexs、data types、operators 的集合,名称需要在一个 DB 中唯一。
指令
- 连接数据库: psql -h 127.0.0.1 -U user [-d dbname]; //[] 代表可选填
- 关闭连接: \q;
- 切换数据库: \c dbname;
- 创建用户: create user username password ‘xxx’; //注意password上的单引号
- 给用户赋予权限:
- grant select on all tables in schema public to user; //将所在数据库下的public 模式的所有表的查询权限赋予用户user
- grant all privilege on [table] device-info to user; //将表 device-info 所有权限赋予用户user
- grant connect on database “FMS”, “VMS” to user; //将数据库 “FMS”, “VMS”的连接权限赋予用户user