Upgrading Postgres With Homebrew
Tutorial | Reference
November 13, 2022 (updated: October 22, 2023)
I’ve battled with this before and thought I’d learned my lesson. Apparently not! Today I faced some issues upgrading PostgreSQL on my laptop again.
What I was trying to do
I downloaded a dump of a heroku postgres database to my local machine (thanks Render docs). No problem!
$ heroku pg:backups:capture --app my-heroku-app |
Then I tried to restore it to the new host location, railway:
$ pg_restore --verbose --no-acl --no-owner -d postgresql://USER:PASS@containers-us-east-9999.railway.app:5432/railway latest.dump |
And there the errors began…
$ pg_restore --verbose --no-acl --no-owner -d postgresql://USER:PASS@containers-us-east-9999.railway.app:5432/railway latest.dump |
How I fixed it
Some StackOverflowing indicated that pg_restore
might be outdated. What the heck version was I even on?
$ which pg_restore |
A clue! I’ve used the pg_restore
tools with Postgres version 11 to capture & download the database dump, but the Heroku database I’ve backed up from is running version 12, and railway is using version 13. The latest is version 14. What a mess.
My next step was to upgrade my local Postgres version to 12, to at least be compatible with the heroku source:
$ brew install postgresql@12 |
This took about 25 minutes to run all of the updates 🙀 Eventually it finished and I added the new version to PATH in my bash profile:
If you need to have postgresql@12 first in your PATH, run: |
Then I tried restoring the db dump to Railway again…and it worked!:
$ pg_restore --verbose --no-acl --no-owner -d postgresql://USER:PASS@containers-us-east-9999.railway.app:5432/railway latest.dump |
Testing New Database Host
I connected my locally running app the the new host and verified that all the controllers still worked, and that all ties had successfully been severed from Heroku 😢
BUT THEN I noticed that you can’t download your data from Railway. What the heck! Well you probably can—hopefully you can by installing their CLI and doing a dump that way. They’ve also created a plugin that you can run on your own server to automatically send backups to an AWS S3 bucket. This is supposed to be low maintenance though…
Since this particular app isn’t public facing at all, I decided to just run it entirely locally going forward. Make it as simple as possible.
Omg even localhost
isn’t working 😭
Of course it’s not that easy! If I’m going to run it locally I need to get my local PG up & running. But I’m getting a big fat connection error connecting to localhost
in Postico. Ugh.
If I’m basically starting from the beginning I may as well get completely up to date, so I install postgres 14 first…
$ brew install postgresql@14 |
…and update PATH again to get the correct version (may as well keep all versions in case needs be…):
# Postgres Installations |
With the bash profile updated and re-sourced, I’m now finally up to date:
$ postgres --version |
So I should be able to open the postgres server command line, right? Nope…!
$ psql |
Ok so no postgres service is actually running. So I start a service, but to no avail:
$ brew services start postgresql |
I took it’s warning and tried again:
$ brew services start postgresql@14 |
So the service is running, it’s just not working! Lovely! I stopped it (brew services stop postgresql
) and looked around the /usr/local/var
directory to see what might be causing conflict. There were a few postgres versions and support files there:
drwx------ 25 user role 800B May 31 2020 postgres |
Then I tried restarting the postgres service, but it still didn’t launch successfully:
$ brew services restart -vvv postgresql |
I googled this error and tried running brew postgresql-upgrade-database
, thinking this felt vaguely familiar, and might get v14 into the state it needs to be. But this gave an error as well:
$ brew postgresql-upgrade-database |
So then I googled this error and lo and behold! if my own post about this very issue didn’t come up as the 2nd result. Thanks old me! I took the advice of my risky former self and deleted the postgres.old
directory and tried running it again:
$ rm -rf /usr/local/var/postgres.old |
Update October 2023:
On encountering the same error again, the magic command wasrm /usr/local/var/postgres/postmaster.pid
as described in this StackOverflow post
OMG I’M IN!
It worked! I can access the postgres server command line, and connect in Postico. Turns out there wasn’t even any data in the old versions…I could have just scrapped it all and reinstalled from the beginning to save myself the time. Oh well, better safe than sorry!
Now to learn local Postgres again…