Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I change current value for identity type primary key column in table in PostgreSQL database?

I had an application and PostgreSQL database with EF Core orm. There was a table Cars and Id column had a sequence, where I could change current value for next record. Now I created a new app and moved records from old table to new table in new database. First record has an Id=12000 for example.
New records started from 1 and when it reached record with Id 12000 I started get following error:

Npgsql.PostgresException (0x80004005): 23505: duplicate key value violates unique constraint "PK_Cars"

In new table Id coloumn not using sequence anymore, it has an Identity type and i can not change current value in pgAdmin. How can I make my Id coloumn change current id value?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First, find out the actual maximum in the table:

SELECT max(id) FROM tab;

  max 
════════
 123000
(1 row)

Then, set the underlying sequence to a higher value:

ALTER TABLE tab ALTER id RESTART 200000;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading