sqlpostgresqlauto-increment

How to set up manually the next value of auto_increment?


I added some rows into the table manually and also I set up the ID (auto_increment) manually. Now when I try to add new row through my app into DB table, to DB table I am getting the error , that the created ID value already exist.

How can I set manually the next ID value (for example, in table I have to IDs, so how to tell to PostgreSQL, that the next ID should be counted since the number 3)?


Solution

  • http://www.postgresql.org/docs/current/static/functions-sequence.html

    select setval('sequence-name', <new-value>);
    

    You can get the sequence name from the the table definition:

    id       | integer                | not null default nextval('id_seq'::regclass)
    

    In this case the sequence is named 'id_seq'

    Edit (10x to @Glenn):

    SELECT setval('id_seq', max(id)) FROM table;