PostgreSQL数据库

常用查询语句

====================================================================================================

====================================================================================================
pg_user;
pg_roles;
pg_class;
pg_tables;
pg_attribute;
pg_type;
pg_description;

获取当前DB中所有表信息(包括schemaname、tablename、tableowner等字段) select * from pg_tables;
用户自定义的表,未指定schema,默认放在public下 select tablename from pg_tables where schemaname = 'public';
创建数据库用户 create user 用户名 with password 'password'; 如:create user data_service with password '123456';
修改数据库用户密码 alter user 用户名 with password 'password'; 如:alter user data_service with password '123456';
删除用户 drop user 用户名; 如:drop user data_service;
给用户赋予权限
查询当前数据库:select current_database();
查询当前用户:select user;/select current_user;
select * from pg_attribute;
information_schema;
select * from pg_user;
select * from pg_class;
select * from pg_tables;
select * from pg_roles;
获取当前数据库的用户 select * from pg_user;

用户postgres是管理员,可以修改其他用户的密码,而postgres本身不需要密码就能登录,这样不安全。
退出客户端 \q
====================================================================================================
通过cmd访问数据库
====================================================================================================
psql -U 数据库用户名 -d 数据库名称
如:psql -U postgres -d data_service_db
psql -h 主机 -U 数据库用户名 -d 数据库名称
如:psql -h localhost -U postgres -d data_service_db

====================================================================================================
快捷命令
====================================================================================================
\q 退出
\du 查看所有用户
\t
\? 查看psql命令列表
\c 查询当前用户以及数据库
\c 数据库名称 切换数据库
\c 数据库名称 用户名称 切换数据库以及用户
\l 查看所有数据库
\d 表名称 查看表结构
\password 设置密码
\h SQL命令 查看SQL命令的解释,\h select
\conninfo 列出当前数据库和连接的信息
\dn 查询schema
\dt

====================================================================================================
用户权限管理
====================================================================================================
createdb
nocreatedb
====================================================================================================
centos刚安装postgresql如何登陆数据库
====================================================================================================
1、centos切换到postgres用户 su postgres
2、psql

====================================================================================================
====================================================================================================
====================================================================================================
====================================================================================================
====================================================================================================

posted @ 2020-01-05 23:51  海*风  阅读(271)  评论(0编辑  收藏  举报