1、修改用戶postgres的密碼
1
|
# alter user postgres with password ‘xxxx';(其中xxxx是修改的密碼)。 |
2、查看下當(dāng)前schema的所有者:
1
2
3
4
5
6
|
SELECT n.nspname AS "Name" , pg_catalog.pg_get_userbyid(n.nspowner) AS "Owner" FROM pg_catalog.pg_namespace n WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema' ORDER BY 1; |
3、查詢結(jié)果如圖所示,模式“abc”的所有者為postgresql用戶
針對(duì)模式“abc”, 使用超級(jí)管理員postgresql給普通用戶test授權(quán),命令如下:
1
2
3
4
5
6
|
// 最后一條命令就是授予初始權(quán)限 grant select on all tables in schema abc to test; grant usage on schema abc to test; alter default privileges in schema abc #將表mytable,授權(quán)給testUser; # GRANT SELECT ON TABLE mytable TO testUser; |
4、查看默認(rèn)權(quán)限
授權(quán)完成,通過pg_default_acl表查看默認(rèn)權(quán)限:
1
2
|
// 查看初始權(quán)限 select * from pg_catalog.pg_default_acl; |
5、把模式“abc”的擁有者(owner)修改為dbadmin用戶(可以事先創(chuàng)建好),執(zhí)行以下命令:
1
2
3
4
5
6
7
8
|
// 修改模式“abc”擁有者為:dbadmin ALTER SCHEMA abc OWNER TO "dbadmin" ; // 查看模式的擁有者,相當(dāng)于\du元命令 SELECT n.nspname AS "Name" , pg_catalog.pg_get_userbyid(n.nspowner) AS "Owner" FROM pg_catalog.pg_namespace n WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema' ORDER BY 1; |
6、postgre查詢所有用戶,postgre中查詢用戶所擁有的權(quán)限
1
2
|
select * from pg_roles; select * from pg_user; |
權(quán)限查詢:
1
|
select * from information_schema.table_privileges where grantee= 'cc' ; |
查看當(dāng)前用戶的所有權(quán)限
1
|
select * from information_schema.table_privileges where grantee= 'user_name' ; |
7、把適用于該對(duì)象的所有權(quán)限都賦予目標(biāo)角色。
用特殊的名字 PUBLIC 把對(duì)象的權(quán)限賦予系統(tǒng)中的所有角色。 在權(quán)限聲明的位置上寫 ALL,表示把適用于該對(duì)象的所有權(quán)限都賦予目標(biāo)角色。
1
2
3
4
|
beigang=# grantall on schema csm_ca to public ; GRANT beigang=# revoke all on schema csm_ca frompublic; REVOKE |
8、先創(chuàng)建一個(gè)角色xxx,再創(chuàng)建一個(gè)超級(jí)用戶csm、普通用戶csm_ca,csm用戶創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)testdb,在這個(gè)數(shù)據(jù)庫(kù)里創(chuàng)建一個(gè)schema:csm_ca,然后賦予普通用戶csm_ca操作數(shù)據(jù)庫(kù)testdb里schema:csm_ca里的表的權(quán)限。
1
2
3
4
5
|
# create role: # create role xxx with superuser; # Create user : # create user csm with superuserpassword 'csm' ; # create user csm_ca with password 'csm_ca' ; |
9、超級(jí)用戶csm給普通用戶csm_ca授予操作schema csm_ca的權(quán)限
1
2
3
4
|
beigang=# grant all on schema csm_ca to csm_ca; GRANT beigang=# grant all on all tables in schema csm_ca to csm_ca; GRANT |
10、創(chuàng)建用戶
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#創(chuàng)建普通用戶 postgres=# create user test encrypted password 'test' ; #創(chuàng)建超級(jí)用戶 postgres=# create user test2 superuser; #創(chuàng)建一個(gè)普通用戶,并且賦予相關(guān)權(quán)限 # create user test createdb createrole inherit password 'test' ; #將超級(jí)用戶修改為普通用戶 # alter user test nosuperuser; #修改用戶為超級(jí)用戶 postgres=# alter user test superuser; #修改用戶密碼 postgres=# alter user test2 password 'test' ; #修改用戶名 postgres=# alter user test2 rename to test3; #鎖定/解鎖用戶,不允許/允許其登錄 postgres=# alter user test nologin; postgres=# alter user test login; #設(shè)置用戶的連接數(shù),其中0表示不允許登錄,-1表示無限制 postgres=# alter user test connection limit 10; |
11、授予用戶數(shù)據(jù)庫(kù)權(quán)限
1
|
GRANT ALL PRIVILEGES ON DATABASE 數(shù)據(jù)庫(kù)名 TO 用戶名; |
12、授予用戶查看剛授權(quán)的數(shù)據(jù)庫(kù)的里面的表的權(quán)限
1
|
GRANT ALL PRIVILEGES ON TABLE 表名 TO 用戶名; |
13、附帶一條:修改的表的類型
1
|
alter table 表名 alter 字段名 type 類型; |
14、附帶一條:增加表新的字段
1
|
alter table 表名 add column 字段名 text(字段類型); |
15、新增:設(shè)置主鍵自增
1
2
3
4
5
6
7
8
|
CREATE SEQUENCE user_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1; alter table sys_user alter COLUMN id set DEFAULT nextval( 'user_id_seq' ); |
16、新增:postgres創(chuàng)建B-Tree索引
1
2
|
-- create index '索引名' on '表名' ('需要索引的字段') CREATE INDEX ip_store_inde on ip_store (ip_network); |
添加各種約束
(1)、 添加主鍵
1
|
alter table goods add primary key (sid); |
(2)、 添加外鍵
1
|
alter table orders add foreign key (goods_id) references goods(sid) on update cascade on delete cascade ; |
on update cascade
:被引用行更新時(shí),引用行自動(dòng)更新;
on update restrict
:被引用的行禁止更新;
on delete cascade
:被引用行刪除時(shí),引用行也一起刪除;
on dellete restrict
:被引用的行禁止刪除;
(3). 刪除外鍵
1
|
alter table orders drop constraint orders_goods_id_fkey; |
(4). 添加唯一約束
1
|
alter table goods add constraint unique_goods_sid unique (sid); |
(5). 刪除默認(rèn)值
1
|
alter table goods alter column sid drop default ; |
(6). 修改字段的數(shù)據(jù)類型
1
|
alter table goods alter column sid type character varying ; |
(7). 重命名字段
1
|
alter table goods rename column sid to ssid; |
17、創(chuàng)建唯一鍵約束
1
|
constraint user_info_unique_userid unique (userid) |
擴(kuò)展
編輯配置文件
文件:postgresql.conf
位置:/var/lib/pgsql/data/postgresql.conf
添加/修改:在所有IP地址上監(jiān)聽,從而允許遠(yuǎn)程連接到數(shù)據(jù)庫(kù)服務(wù)器:
1
|
listening_address: '*' |
文件:pg_hba.conf
位置:/var/lib/pgsql/data/pg_hba.conf
添加/修改:允許任意用戶從任意機(jī)器上以密碼方式訪問數(shù)據(jù)庫(kù),把下行添加為第一條規(guī)則:
1
|
host all all 0.0.0.0/0 md5 |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://blog.csdn.net/qq_40907977/article/details/105395845