I have a postgresql database and after migrating to a newer version and importing my old data I have a problem with the primary key:
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "browser_link_pkey" Detail: Key (id)=(173) already exists. So I wanted to reset my sequence, but running:
select nextval('browser_link_id_seq') Also fails with:
column „browser_link_id_seq“ does not exist SQL Status:42703 This is the SQL for table creation
CREATE TABLE browser_link ( id bigint NOT NULL, .... ); ALTER TABLE ONLY browser_link ADD CONSTRAINT browser_link_pkey PRIMARY KEY (id); I tried selecting the serial sequence, but it seems none exists:
postgres=# \connect historify You are now connected to database "historify" as user "postgres". historify=# select pg_get_serial_sequence('browser_link', 'id'); pg_get_serial_sequence ------------------------ (1 row) I am using postgresql 9.5.3. Also, until the error occured the id column did increment as expected, so somehow it does work.
Now my two questions:
- why does the autoincrement work?
- how do I reset the autoincrement counter?
select nextval('browser_link_id_seq')Double quotes are for identifiers. (the sequence name in the function call may look like an identifier, but it is actually a string literal)idcolumn. You should alter the column, adding the sequence as a default value/expression.primary keyconstraint does create a sequence.