Convert SQLite to Postgre in Rails

Uriah Harston
2 min readJan 15, 2021
Photo by Markus Winkler on Unsplash

So you want to convert your SQLite database in Rails to Postgre? That is why you are here right? Let’s get started. 😊

Check Postgre Version

First, you have to check if you have Postgre via the command line. If you have Ubuntu then Postgre should come installed by default.

psql -V

If nothing comes up then you will have to install Postgre.

For New Projects

If you are creating a new Rails project, you can specify on creating that you will opt-out of the standard SQLite setup in favor of Postgre.

rails new my_app --database=postgresql

This should take care of all your woes. If it doesn’t then read on.

For Existing Projects

First, you will need to comment out gem 'sqlite' . Then add Postgre to your Gemfile and bundle install .

1. gem 'pg' 2. bundle install

You will need to modify your database.yml file to pull from Postgre for all three environments.

development:adapter: postgresqldatabase: my_database_developmentusername: my_usernamepassword: my_passwordpool: 5timeout: 5000
test:adapter: postgresqldatabase: my_database_testusername: my_usernamepassword: my_passwordpool: 5timeout: 5000production:adapter: postgresqldatabase: my_database_productionusername: my_usernamepassword: my_passwordpool: 5timeout: 5000

With Postgre, each environment has its own separate database, username, and password. This was an ah-ha moment for me. It is something you do not think about when you are coming from using SQLite.

Conclusion

This should take care of it. It is a best practice to store your username and password inside a .env variable. You should be able to rails db:migrate any changes no problem now!

--

--