1.創建用戶並將某個數據庫的屬主修改為該用戶
create user uhxl;
alter user uhxl with password 'uhxl';
alter user uhxl with CONNECTION LIMIT 20;
alter database hxl owner to uhxl; ##修改數據庫屬主
GRANT ALL PRIVILEGES ON DATABASE hxl TO uhxl; ##或是授權
2.進入數據庫查看數據庫下的表
psql -h localhost -U uhxl -d hxl
select * from pg_tables where schemaname = 'public';
3.修改表的owner
person目前屬主是postgres,現在修改為uhxl
psql -h localhost -U postgres -d hxl
alter table person owner to uhxl;
4.查看表的字段
hxl=#\d 表名稱
5.查看表的ddl
只能使用pg_dump,使用pg_dump我們可以把表還有索引的語句都dump出來,這里使用-s選項(schema only)和-t選項(tables)。
pg_dump -s -t person -d hxl|egrep -v "^--|^$"
6.清除數據並清除自增ID
--清除所有的記錄(有外鍵關聯的情況下)
truncate table tb_test cascade;
--清除所有的記錄,並且索引號從0開始
truncate table tb_test restart identity cascade;
7.查看權限
登陸具體的數據庫:
psql -h localhost -U uhxl -d hxl
select * from information_schema.table_privileges where grantee='uhxl';
select * from information_schema.usage_privileges where grantee='uhxl';
select * from information_schema.routine_privileges where grantee='uhxl';
8.登陸數據庫授權
psql -h localhost -U hxl01 -d hxl
grant SELECT on table public.tb_hxl to uhxl;
9.查詢某個表在那個庫下面:
SELECT * FROM information_schema.tables WHERE table_name='tb_hxl';
10.查看用戶擁有的角色
postgres=# \du uhxl
List of roles
Role name | Attributes | Member of
-----------+----------------+-----------
uhxl | 20 connections | {}
postgres=# \du postgres
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
11.創建數據庫指定字符集
create database db_zifuji encoding = 'utf8';
12.查看所有的庫
postgres=# \l
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
13.查看用戶
postgres=# \du
goldengate | Superuser | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}