PostgreSQL 序列增刪改案例

創建序列

CREATE SEQUENCE if not exists test_mergetable_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 999999999
START 1
CACHE 1;
//或者: 
create sequence if not exists test_mergetable_id_seq increment by 1 minvalue 1 no maxvalue start with 1; 

指定序列(給表的主鍵指定創建好的序列)

alter table test_mergetable alter column "i_id" set default nextval('test_mergetable_id_seq');

設置序列自增長從當前最大值開始

SELECT setval('test_mergetable_id_seq', (SELECT MAX(i_id) FROM test_mergetable));
alter sequence test_mergetable_id_seq start with 12;

刪除序列

drop sequence IF EXISTS test_mergetable_id_seq

查看序列

SELECT nextval('test_mergetable_id_seq')

補充:pgsql的schema對用戶授權,單個用戶跨schema增刪改查操作

–創建用戶

create user user1;

–修改密碼

alter user report with password 'password';

–授權查詢權限

grant usage on schema schema1 to user1;
grant usage on schema schema2 to user1;

修改search_path可跨schema操作

set search_path = "$user",user1,user2

–授權schema:schema1給user1權限 這個權限太大需要慎用

grant all on schema schema1 to user1;

–授權schema的表權限給user1 用戶權限太多需慎用

grant all on all tables in schema schema1 to user1;

–授權schema的表權限給user1 用戶權限太多需慎用

grant all on all tables in schema schema1 to user1;

–授權某個schema的單個表查權限

grant select on schema2.table1           to user1;

–收回所有授權

revoke all on all tables in schema schema1 from user1;

–為某個特定用戶設置search_path

alter user user1 set search_path="$user",user1,user2;

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀: