ERROR: duplicate key value violates unique constraint "template_pkey" code example

Example 1: ERROR: duplicate key value violates unique constraint statuslogs_pkey DETAIL: Key (id)=(1621) already exists.

How to reset postgres' primary key sequence when it falls out of sync?

Example 2: duplicate key value violates unique constraint "django_migrations_pkey"

from django.db import connections

query = "SELECT setval('django_migrations_id_seq', (SELECT MAX(id) FROM django_migrations))"
cursor = connections['default'].cursor()
cursor.execute(query) 
row = cursor.fetchone()

Tags:

Misc Example