Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

Fixing sequence problems in PostgreSQL

This one’s mainly for my own future reference. The following error message in PostgreSQL:

ERROR:  Cannot insert a duplicate key into unique index auth_permissions_pkey

Is caused by a table with an automatically incremented primary key (set up using the SERIAL keywords in the table creation statement) getting out of sync somehow with the sequence that tracks the next available primary key. You can re-sync the sequence with the following query:

SELECT setval('auth_permissions_id_seq', 
  (SELECT max(id) FROM auth_permissions));

This is Fixing sequence problems in PostgreSQL by Simon Willison, posted on 21st April 2004.

View blog reactions

Next: Python in Mathematics

Previous: Slashdot Humour

1 comment

  1. If you don't know what the sequence name is (older versions of postgres truncate them in unusual ways), you can find out by using \d table in psql.

    Dominic Mitchell - 21st April 2004 22:58 - #

Comments are closed.

Previously hosted at http://simon.incutio.com/archive/2004/04/21/postgres

A django site