100%
This guide walks through upgrading your local PostgreSQL setup from version 12 to 17 for a Rails application on macOS using Homebrew, while preserving data and ensuring your Rails app keeps working.

βœ… Overview
Tools Involved:

  • PostgreSQL (brew install postgresql@17)

  • pg_upgrade for migrating old data

  • Ruby on Rails + pg gem

  • Homebrew service management

  • libpq for client libraries

πŸ”§ Steps

1. Install PostgreSQL 17

bashCopyEditbrew install postgresql@17

Ensure it's linked correctly:

bashCopyEditbrew link postgresql@17 --force

2. Check for Binary Version Mismatches

Make sure initdb and postgres match:

bashCopyEditinitdb --version
postgres --version

If mismatched, uninstall libpq and reinstall PostgreSQL:

bashCopyEditbrew uninstall libpq
brew reinstall postgresql@17

3. Initialize PostgreSQL 17 Data Directory

If needed:

bashCopyEditrm -rf /opt/homebrew/var/postgresql@17
initdb -D /opt/homebrew/var/postgresql@17

4. Stop Postgres 17 Before Upgrading

bashCopyEditbrew services stop postgresql@17

5. Run pg_upgrade to Migrate Data

bashCopyEditpg_upgrade \
  -b /opt/homebrew/opt/postgresql@12/bin \
  -B /opt/homebrew/opt/postgresql@17/bin \
  -d /opt/homebrew/var/postgresql@12 \
  -D /opt/homebrew/var/postgresql@17

6. Start PostgreSQL 17

bashCopyEditbrew services start postgresql@17

7. Fix Rails pg Gem Dynamic Linking Error

The Rails app may crash with a libpq.5.dylib error.

Fix it by reinstalling and linking libpq:

bashCopyEditbrew install libpq
brew link --force libpq

8. Confirm Everything Works

  • Start Rails server:

  • bash
  • CopyEdit
  • rails s
    
  • Check your app connects and loads data as expected

🧹 Optional Cleanup
After verifying everything:

bashCopyEditrm -rf /opt/homebrew/var/postgresql@12
./delete_old_cluster.sh  # optional, auto-generated by pg_upgrade

🏁 You're Done!
Your Rails app is now fully upgraded to PostgreSQL 17 πŸŽ‰
Β All data was preserved, and your environment is clean and up to date.



0  0
 
Created:


| | Required db upgrade heroku | Rails | Your stories