Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6679517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:22:06+00:00 2026-05-26T04:22:06+00:00

I have and existing database, which I have migrated with SQLAlchemy to a new

  • 0

I have and existing database, which I have migrated with SQLAlchemy to a new PostgreSQL database.

I moved all primary keys with the same values as before. Now I have tables filled with data, but the associated sequences starts from 1. I have pk values stored 1 to 2000.

Now, when I try to save something with Django, I have the

duplicate key value violates unique constraint
regarding to the Primary Key.

How can I modify the sequence start values or escape this situation?
My current solution is:

conn = psycopg2.connect(...)
    for table_name in table_names:

        cursor = conn.cursor()
        cursor.execute("""
        SELECT setval('%s_id_seq', (SELECT COALESCE(MAX(id),0)+1 FROM %s));
        """% (table_name, table_name))

It works for me, but I don’t like it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:22:06+00:00Added an answer on May 26, 2026 at 4:22 am

    Ways to set / reset a sequence in PostgreSQL

    (not necessarily to max(id)).

    • There’s the simple way you have in the question. You can set the sequence to start at an arbitrary number with setval():

      SELECT setval('tbl_id_seq');
      
    • Then there’s the standard SQL way with ALTER SEQUENCE doing the same:

      ALTER SEQUENCE myseq RESTART WITH 1;
      
    • If you like to restart your sequences at numbers other than the default 1:

      CREATE TABLE foo(id serial, a text);      -- creates sequence "foo_id_seq"
      INSERT INTO foo(a) VALUES('a');           -- seq. starts with --> 1
      
      ALTER SEQUENCE foo_id_seq START WITH 10;  -- doesn't restart sequence
      INSERT INTO foo(a) VALUES('b');           --> 2
      
      ALTER SEQUENCE foo_id_seq RESTART;        -- restarts sequence
      INSERT INTO foo(a) VALUES('c');           --> 10
      
    • And there is another way, when you empty a table with TRUNCATE:

      TRUNCATE foo RESTART IDENTITY;
      

      Implicitly executes ALTER SEQUENCE foo_id_seq RESTART;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing SQL Database in which all tables have an ID column
I have an existing PostgreSQL database that I'm using for a new Django site.
I have an existing database with tables which each have 4 partitions (there are
I have a CSV export script (enrdata_arch.php) which calls information from an existing database
I am developing an application in which I have to use existing database but
I have an existing database (PostgreSQL in my case), and would like to access
We have an existing Firebird database which we wish to setup in an ASP.NET
I have an existing Access database which contains training records for employees. If an
I have an existing database in which the majority of the tables have int
I have a existing MySQL database which has large number of tables. But the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.