Postgresql常用命令

Posted 种树飞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Postgresql常用命令相关的知识,希望对你有一定的参考价值。

以下为刚接触postgresql常需要的数据库命令:

  1. 登录数据库                          psql -U 用户名 -W 数据库名      -h ip -p 端口  最后这两个参数可以有可以无,看使用情况
  2. 查看所有的用户                       select * from pg_user;
  3. 查看当前数据库下有的表                  select * from pg_tables;                 或者/dt
  4. 查看用户表权限                       select * from information_schema.table_privileges where grantee=‘用户名‘;
  5. 查看用户具有权限的表                   select * from information_schema.usage_privileges where grantee=‘用户名‘;
  6. 查看某表的具体字段情况                  d +表名
  7. 查看数据库下所有的schema                 dr
  8. 创建数据库                           createdb  数据库名称
  9. 删除数据库                           dropdb   数据库名
  10. 创建用户                            create user /role with password  ‘你要设置的密码‘; 
                                      这里user和role的区别是: 用user默认创建的用户具有登录权限,用role无登录权限后面需要授权
  11. 修改密码                            ALTER ROLE 用户名 WITH PASSWORD ‘*‘; 
  12. admin用户,INSERT INTO authors,提示没有表权限
  1. admindb=> INSERT INTO authors (name) VALUES (‘Mo Yan‘); 
  2. ERROR: permission denied for table authors
  1. postgres用户,授予表权限
  1. admindb=# GRANT ALL PRIVILEGES ON TABLE authors TO admin; 
  2. GRANT
  1. admin用户,INSERT INTO authors,提示没有sequence权限
  1. admindb=> INSERT INTO authors (name) VALUES (‘Mo Yan‘); 
  2. ERROR: permission denied for sequence authors_id_seq
  1. postgres用户,授予sequence权限
  1. admindb=# GRANT ALL PRIVILEGES ON SEQUENCE authors_id_seq TO admin; GRANT

 

以上是关于Postgresql常用命令的主要内容,如果未能解决你的问题,请参考以下文章

postgresql常用命令

postgresql centos6.5安装以及常用命令

PostgreSQL常用命令

PostgreSQL教程收集(中文文档/命令行工具/常用命令)

Postgresql常用命令汇总

提效小技巧——记录那些不常用的代码片段