Skip to content

Instantly share code, notes, and snippets.

@paulsturgess
Created February 27, 2013 19:49
Show Gist options
  • Save paulsturgess/5051080 to your computer and use it in GitHub Desktop.
Save paulsturgess/5051080 to your computer and use it in GitHub Desktop.
Reset postgres primary key index using Rails
$ ActiveRecord::Base.connection.reset_pk_sequence!('table_name')

If you need the table names:

$ ActiveRecord::Base.connection.tables
=> ["accounts", "assets", ...]
@holamendi
Copy link

It's been 5 years and this is still useful. Thanks! 🥇

@tomav
Copy link

tomav commented May 3, 2018

Combine both of them:

        ActiveRecord::Base.connection.tables.each do |t|
          ActiveRecord::Base.connection.reset_pk_sequence!(t)
        end

@tasdendu
Copy link

This is it. Perfect answer.

@kinoute
Copy link

kinoute commented Jun 7, 2020

2020, still great. thanks!

@Mozart2234
Copy link

2020, still great. thanks!

Yep!!

@Rukosenpa
Copy link

2021, here for get help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment