How to list and alter table sequences in PostgreSQL

By | 22 September, 2014

So you screwed up with your ninja manual inserts on some lost and forgotten table. In such a way that when your shiny django backoffice gives you a fullscreen yellow error about duplicate keys. What now? Well, you read and i will show you how to circumvent the system (well, kind of).

I will assume you logged in to your postgresql server and selected your database (if you dont know what i am talking about, go read this).

to list all available sequences on your database, login to postgresql and do this:


after you locate your table sequence, you can check the counter like this:

SELECT last_value FROM[insert your sequence table name here];

since you did some unwanted manual ninja inserts, get your last inserted id (from your table key).
To change the sequence, you can use this command:

ALTER SEQUENCE [sequence table] RESTART with [your last id + 1]

I am keeping this brief, but if you want to learn all the other commands to deal with sequences, visit the links below.

– PostgreSQL documentation – ALTER SEQUENCE (for 8.2)