Switching from Sqlite3 to Postgres
/ 2 min read
Let me tell you, I had been avoiding making this update for months. It seemed hard and scary and I was afraid I would break something. But you know what?
It was kind of a mess, but not as much of a mess as I thought it would be.
There are a lot of tutorials on switching your Ruby on Rails (Rails) database from sqlite3 to postgres. However, I did run into problems that were not covered in the tutorial I was reading at the time and have to find another one or a one-off solution.
There may be others who run into issues like I have, so I wanted to document the issues I had and possible fixes.
Postgres just won’t install
??♀️
The first step in most tutorials I followed was to remove the sqlite3 from the Gemfile in your backend and replace it with Postgres.
In my Gemfile, it looked like this.
gem 'sqlite3', '~> 1.4'
I’d replace it with this.
gem 'pg'
Then run bundle install
to install the gem.
Except, it just wouldn’t install. I kept getting a wall or red or yellow console logs, starting with something like this.
Failed to build native extensions...
Yikes.
I tried doing a global install of the gem, but got a similar error.
After several months of avoiding trying to find a solution for this, I asked for help and got a link to a Stackoverflow question that echoed the same issue I was having.
Installing PG gem on OS X - failure to build native extension
And the most upvoted answer to install postgres with homebrew worked for me!
Here are the steps.
- Install another build of Xcode Tools (typing
brew update
in the terminal will prompt you to update the Xcode build tools) brew update
brew install postgresql
After that, running bundle install
ran successfully.
Bundle complete! 13 Gemfile dependencies, 58 gems now installed.