Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbranchaud/25ef32a47ba142c057d095a3e326dff5 to your computer and use it in GitHub Desktop.
Save jbranchaud/25ef32a47ba142c057d095a3e326dff5 to your computer and use it in GitHub Desktop.
PostgreSQL: Migrating from INT to BIGINT

PostgreSQL: Migrating from int to bigint

Resources

  • This approach requires setting up a PostgreSQL trigger to keep the mirrored bigint column in sync with the existing int column until it is time to swap them out.
  • This includes a handy query that you can run against your database to identify columns at risk to int overflow.
  • One approach (quick fix, not long-term solution) suggested in this article is to switch to negative integers and auto-decrement the primary key sequence.
  • The main approach suggested in this article is to create a secondary bigint column called new_id and swap it out with the existing id column. This approach does not require a trigger.

Notes

  • When switching from int to bigint on a primary key column, you'll also need to update any foreign key columns that reference that id making them bigint as well.

Other

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