Sqlite3 to PostgreSQL on Rails App
Switching project database from Sqlite3 to PostgreSQL
To deploy your Ruby on Rails Application to Heroku, you need one of three database management PostgreSQL, Redis or Apache Kafka, in this blog, we will go through how to switch the project database from SQLite3 to PostgreSQL.
Installing PostgreSQL
First, you need to make sure that PostgreSQL is installed and running in your machine, you can use this link to download it. and follow up on this video to install it.
Updating Gemfile
Remove or comment the Sqlite gem
and you’ll need to replace it with
Re-install your dependencies by running
bundle install
Update config/database.yml file
Your file looks should like this:
Change this file to look like this:
Note that the database name is your project name adding _development
, in my this it’s your_project_name_development
. And for the test is your_project_name_test
, you also may add your Mac name to the username
.
Create and Migrate
To create the database in your project sample run the below code in your terminal
rake db:setup
This will create the database and then migrate the table by running
rails db:migrate
You may check to ensure that everything is set up correctly by running the rails server.
rails server
At this point, your App will be running but no data in it, if you want to copy the data from the previous database (SQLite) to the new Database (Postgres). there are a few ways. below are some helpful links to do this migration
Migrate your data from SQLite to PostgreSQL
What is the best way to copy my SQLite database to PostgreSQL so I can deploy it to Heroku?