DBA Hub

📋Steps in this guide1/1

Manage sequences in postgres

-- Find the sequence details:

postgresql configurationintermediate
by PostgreSQL
12 views
1

Manage sequences in postgres

Code/Command (click line numbers to comment):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
-- Find the sequence details:
select * from pg_sequences;
(or)
\ds+
List of relations
Schema  | Name      | Type     | Owner        | Size | Description
--------+-----------+----------+--------------+------------+-------------
public  | class_seq | sequence | enterprisedb | 8192 bytes |
(1 row)
-- Create sequences:
postgres#
CREATE SEQUENCE class_seq INCREMENT 1 MINVALUE 1 MAXVALUE 1000 START 1;
CREATE SEQUENCE
-- Create sequence in descending:
postgres#
CREATE SEQUENCE class_seq INCREMENT -1 MINVALUE 1 MAXVALUE 1000 START 1000;
CREATE SEQUENCE
-- Alter sequence to change maxvalue:
postgres=#
alter sequence class_seq maxvalue 500;
ALTER SEQUENCE
-- Reset a sequence using alter command:
postgres=#
alter sequence class_seq restart with 1;
ALTER SEQUENCE
-- Find next_val and currval of a sequence:
postgres=#
select nextval('class_seq');
nextval
---------
1
(1 row)
postgres=#
select currval('class_seq');
currval
---------
1
(1 row)

Comments (0)

Please to add comments

No comments yet. Be the first to comment!