During the production deployment of one of our services, we encountered a database error when running our liquibase migration. As additional information, the service that we’re trying to deploy already have an initial set of data. The error we encountered is this:

ERROR:  duplicate key violates unique constraint

This error happens because liquibase assumes that we’re migrating a fresh set of data, so it starts the primary key sequence at 1. This can be solved by setting the primary key sequence at the right location. Which can be done by running this query on your database console.

SELECT setval('primary_id_seq', (SELECT MAX(id) FROM table)+1);

This will set the primary key sequence to the next sequence which will prevent the error that we encountered. Ideally, when migrating with Liquibase on a services that has initial data, we should include the update key sequence query on our migration script.