Re: List sequence assigned to primary key - Mailing list pgsql-novice

From Henshall, Stuart - Design & Print
Subject Re: List sequence assigned to primary key
Date
Msg-id E2870D8CE1CCD311BAF50008C71EDE8E0506DC94@MAIL_EXCHANGE
Whole thread Raw
In response to List sequence assigned to primary key  ("Brian Johnson" <bjohnson@jecinc.on.ca>)
List pgsql-novice

Brian Johnson wrote:
> I added a few records to a table and assigned numbers to the id field
> - now when I try to add records properly, it says that it can't
> accept duplicate values on the primary field (id)
>
> Although there were only a few records and I could just redo them, I
> wonder if there is another method (for future reference)
>
> So here are my two questions
>
> 1. How can you list (find out) the sequence name assigned to a
> primary key?
>
The sequence name for a serial field is:
tablename_fieldname_seq
eg:
CREATE TABLE tst (x SERIAL);
would create a sequence called:
tst_x_seq

> 2. How can you change that sequence value?
>
SELECT setval('seq_name',seq_value)
eg:
SELECT setval('tablename_fieldname_seq',MAX(field_name) FROM tablename;
To resync a sequence with a field.
hth,
- Stuart

pgsql-novice by date:

Previous
From: "Brian Johnson"
Date:
Subject: List sequence assigned to primary key
Next
From: Ciprian Popovici
Date:
Subject: OT: displaying sender's email on archives.postgresql.org