Bugfix: Resetting Production DB Post ID Sequence
Sunday May 11 2025 • 06:21 AM
After seeding the production database, I came across an interesting bug. When creating new posts, the ID would start at 1. Since the previous posts already had those IDs, and these were automatically incremented with each new post being seeded, creating new posts resulted in an error.
ActiveRecord::RecordNotUnique - PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "posts_pkey": DETAIL: Key (id)=(5) already exists.
This led me into a bit of a side quest to investigate running particular SQL queries within dokku.
Listing dokku services
First I listed my dokku services to remind myself of the name of the Postgresql service.
$ dokku postgres:list
=====> Postgres services commit-redux-db
Connecting to the database
Next I connected to the database with $ dokku postgres:connect commit-redux-db
to run the query:
commit_redux_db=# SELECT setval(pg_get_serial_sequence('posts', 'id'), MAX(id)) FROM posts;
That fixed things ✌️