SoFunction
Updated on 2025-04-08

PostgreSQL sequence addition, deletion and modification cases

Create a sequence

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

Specify the sequence (specify the created sequence to the primary key of the table)

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

Set the sequence self-growth starts at the current maximum

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

Delete the sequence

drop sequence IF EXISTS test_mergetable_id_seq

View the sequence

SELECT nextval('test_mergetable_id_seq')

Supplement: pgsql's schema authorizes users, and a single user adds, deletes, modifys and checks across schemas

--Create a user

create user user1;

--Modify password

alter user report with password 'password';

--Authorize query permissions

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

Modify search_path to cross-schema operation

set search_path = "$user",user1,user2

--Authorization schema:schema1 gives user1 permissions. This permission is too large and needs to be used with caution.

grant all on schema schema1 to user1;

--Authorize the table permissions of schema to user1. There are too many user permissions and need to be used with caution

grant all on all tables in schema schema1 to user1;

--Authorize the table permissions of schema to user1. There are too many user permissions and need to be used with caution

grant all on all tables in schema schema1 to user1;

--Authorize a single table query permission for a schema

grant select on schema2.table1           to user1;

--Reclaim all authorizations

revoke all on all tables in schema schema1 from user1;

--Set search_path for a specific user

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

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.