Error when restoring backup on fresh install

I was following this procedure to backup the data of our version 6.0.4 install and restore it in a fresh install of the latest version: Backup and restore Taiga

Now, restoring the DB backup only works if i first drop the existing “taiga” database and create a new one. After that, when i run the taiga backend service (docker compose up -d taiga-back) it crashes after a couple seconds with the following error:

django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"
DETAIL:  Key (id)=(234) already exists.

I’ve tried multiple things and nothing seems to make the restore work. A clean install without restoring the database works fine (but I need to carry over the data :sweat_smile:).

This error is about Postgres sequence on django migrations table. Not sure how this happened, though.

To solve this specific error, you can execute inside the container:

:/# psql -U taiga taiga -c "SELECT setval('django_migrations_id_seq', (SELECT MAX(id) FROM django_migrations))"

Thank you so much for your response.

I managed to make it work after finding this guide and adapting it to my situation: Django integrity error while running migration | bmwlog

Basically i ran these commands inside psql, inside the taiga-db container:

ALTER SEQUENCE django_migrations_id_seq RESTART WITH 288;
ALTER SEQUENCE django_content_type_id_seq RESTART WITH 118;
ALTER SEQUENCE auth_permission_id_seq RESTART WITH 312;

I chose the numbers randomly since the above guide says to choose “any sane values that are greater” than the ones already there. I tried running taiga-back after each of these changes and it only worked after all three were applied.

Your response selects the “MAX(id) FROM django_migrations” and that seems to fix that first error. Do you know to what values I should change the other two sequences that are also giving me errors (django_content_type_id_seq and auth_permission_id_seq)?

Thank you in advance

Django has a custom command that prints the sql commands to run:

docker compose -f docker-compose.yml -f docker-compose-inits.yml run --rm taiga-manage sqlsequencereset auth contenttypes

Gives something like:

BEGIN;
SELECT setval(pg_get_serial_sequence('"auth_permission"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "auth_permission";
SELECT setval(pg_get_serial_sequence('"auth_group_permissions"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "auth_group_permissions";
SELECT setval(pg_get_serial_sequence('"auth_group"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "auth_group";
SELECT setval(pg_get_serial_sequence('"auth_user_groups"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "auth_user_groups";
SELECT setval(pg_get_serial_sequence('"auth_user_user_permissions"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "auth_user_user_permissions";
SELECT setval(pg_get_serial_sequence('"auth_user"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "auth_user";
SELECT setval(pg_get_serial_sequence('"django_content_type"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "django_content_type";
COMMIT;