![]() That will set the sequence to the next available value that's higher than any existing primary key in the sequence. Back up your PG database (just in case), then run thisL SELECT setval('the_primary_key_sequence', (SELECT MAX(the_primary_key) FROM the_table)+1) If the first value is higher than the second value, your sequence is out of sync. The target column names can be listed in any order. One can insert one or more rows specified by value expressions, or zero or more rows resulting from a query. SELECT nextval('the_primary_key_sequence') Description INSERT inserts new rows into a table. At any rate, to see if your values are out of sync, run these two commands: SELECT MAX(the_primary_key) FROM the_table Note: The existing records in the target table are unaffected. In this case, the tmpperson table has the same columns as person. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Call it a "bug by design", but it seems that you have to manually reset the a primary key index after restoring from a dump file. You can insert data in a table as the result of a select statement: INSERT INTO person SELECT FROM tmpperson WHERE age < 30 Note that the projection of the select must match the columns required for the insert. That likely means that the primary key sequence in the table you're working with has somehow become out of sync, likely because of a mass import process (or something along those lines). If you get this message when trying to insert data into a PostgreSQL database:ĮRROR: duplicate key violates unique constraint How to fix PostgreSQL error "duplicate key violates unique constraint" This post helped me solve my problem, not sure what went wrong:
0 Comments
Leave a Reply. |