postgreSQL數(shù)據(jù)庫默認用戶postgres常用命令分享
1、修改用戶postgres的密碼
#alter user postgres with password ‘xxxx';(其中xxxx是修改的密碼)。
2、查看下當前schema的所有者:
// 查看當前schema的所有者,相當于\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;
3、查詢結(jié)果如圖所示,模式“abc”的所有者為postgresql用戶
針對模式“abc”, 使用超級管理員postgresql給普通用戶test授權(quán),命令如下:
// 最后一條命令就是授予初始權(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、查看默認權(quán)限
授權(quán)完成,通過pg_default_acl表查看默認權(quán)限:
// 查看初始權(quán)限 select * from pg_catalog.pg_default_acl;
5、把模式“abc”的擁有者(owner)修改為dbadmin用戶(可以事先創(chuàng)建好),執(zhí)行以下命令:
// 修改模式“abc”擁有者為:dbadmin ALTER SCHEMA abc OWNER TO "dbadmin"; // 查看模式的擁有者,相當于\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)限
select * from pg_roles; select * from pg_user;
權(quán)限查詢:
select * from information_schema.table_privileges where grantee='cc';
查看當前用戶的所有權(quán)限
select * from information_schema.table_privileges where grantee='user_name';
7、把適用于該對象的所有權(quán)限都賦予目標角色。
用特殊的名字 PUBLIC 把對象的權(quán)限賦予系統(tǒng)中的所有角色。 在權(quán)限聲明的位置上寫 ALL,表示把適用于該對象的所有權(quán)限都賦予目標角色。
beigang=# grantall on schema csm_ca to public; GRANT beigang=# revoke all on schema csm_ca frompublic; REVOKE
8、先創(chuàng)建一個角色xxx,再創(chuàng)建一個超級用戶csm、普通用戶csm_ca,csm用戶創(chuàng)建一個數(shù)據(jù)庫testdb,在這個數(shù)據(jù)庫里創(chuàng)建一個schema:csm_ca,然后賦予普通用戶csm_ca操作數(shù)據(jù)庫testdb里schema:csm_ca里的表的權(quán)限。
#create role: #create role xxx with superuser; #Create user: # create user csm with superuserpassword 'csm'; # create user csm_ca with password 'csm_ca';
9、超級用戶csm給普通用戶csm_ca授予操作schema csm_ca的權(quán)限
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)建用戶
#創(chuàng)建普通用戶 postgres=# create user test encrypted password 'test'; #創(chuàng)建超級用戶 postgres=# create user test2 superuser; #創(chuàng)建一個普通用戶,并且賦予相關(guān)權(quán)限 # create user test createdb createrole inherit password 'test'; #將超級用戶修改為普通用戶 # alter user test nosuperuser; #修改用戶為超級用戶 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ù)庫權(quán)限
GRANT ALL PRIVILEGES ON DATABASE 數(shù)據(jù)庫名 TO 用戶名;
12、授予用戶查看剛授權(quán)的數(shù)據(jù)庫的里面的表的權(quán)限
GRANT ALL PRIVILEGES ON TABLE 表名 TO 用戶名;
13、附帶一條:修改的表的類型
alter table 表名 alter 字段名 type 類型;
14、附帶一條:增加表新的字段
alter table 表名 add column 字段名 text(字段類型);
15、新增:設(shè)置主鍵自增
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索引
-- create index '索引名' on '表名' ('需要索引的字段') CREATE INDEX ip_store_inde on ip_store (ip_network);
添加各種約束
(1)、 添加主鍵
alter table goods add primary key(sid);
(2)、 添加外鍵
alter table orders add foreign key(goods_id) references goods(sid) on update cascade on delete cascade;
on update cascade
:被引用行更新時,引用行自動更新;
on update restrict
:被引用的行禁止更新;
on delete cascade
:被引用行刪除時,引用行也一起刪除;
on dellete restrict
:被引用的行禁止刪除;
(3). 刪除外鍵
alter table orders drop constraint orders_goods_id_fkey;
(4). 添加唯一約束
alter table goods add constraint unique_goods_sid unique(sid);
(5). 刪除默認值
alter table goods alter column sid drop default;
(6). 修改字段的數(shù)據(jù)類型
alter table goods alter column sid type character varying;
(7). 重命名字段
alter table goods rename column sid to ssid;
17、創(chuàng)建唯一鍵約束
constraint user_info_unique_userid unique(userid)
擴展
編輯配置文件
文件:postgresql.conf
位置:/var/lib/pgsql/data/postgresql.conf
添加/修改:在所有IP地址上監(jiān)聽,從而允許遠程連接到數(shù)據(jù)庫服務(wù)器:
listening_address: '*'
文件:pg_hba.conf
位置:/var/lib/pgsql/data/pg_hba.conf
添加/修改:允許任意用戶從任意機器上以密碼方式訪問數(shù)據(jù)庫,把下行添加為第一條規(guī)則:
host all all 0.0.0.0/0 md5
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
PostgreSQL怎么創(chuàng)建分區(qū)表詳解
數(shù)據(jù)庫表分區(qū)把一個大的物理表分成若干個小的物理表,并使得這些小物理表在邏輯上可以被當成一張表來使用,下面這篇文章主要給大家介紹了關(guān)于PostgreSQL怎么創(chuàng)建分區(qū)表的相關(guān)資料,需要的朋友可以參考下2022-06-06快速解決PostgreSQL中的Permission denied問題
這篇文章主要介紹了快速解決PostgreSQL中的Permission denied問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01postgresql 中position函數(shù)的性能詳解
這篇文章主要介紹了postgresql 中position函數(shù)的性能詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02Postgresql常用函數(shù)及使用方法大全(看一篇就夠了)
使用函數(shù)可以極大的提高用戶對數(shù)據(jù)庫的管理效率,函數(shù)表示輸入?yún)?shù)表示一個具有特定關(guān)系的值,下面這篇文章主要給大家介紹了關(guān)于Postgresql常用函數(shù)及使用方法的相關(guān)資料,需要的朋友可以參考下2022-11-11在postgreSQL中運行sql腳本和pg_restore命令方式
這篇文章主要介紹了在postgreSQL中運行sql腳本和pg_restore命令方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01