Ruby on Rails: Change database from SQLite to PostgreSQL.

As a junior developer, i discovered that Heroku does not support the SQLite database just as I was deploying my application. If you're lucky, you might have known this already and so you set up your application using a PostgreSQL database from the beginning using the command below:

rails new appName --database=postgresql

However, if you're like me, you've probably been in this situation. And no, you dont need to delete everything and start afresh. All you need to do is reconfigure just a few things. So lets get into it.

1. Edit the Gemfile.

The first thing you need to do, is remove the sqlite gem from the gemfile. Replace that with the gem “pg”. This is what your file should look like:

next, go to your terminal and run: `bundle install`. This should remove the sqlite gem and add the pg (postgresql) gem.

bundle install

2. Edit config/database.yml file

By default, the database is configured as a sqlite database. This is what your file looks like:

default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000

development:
<<: *default
database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3

production:
<<: *default
database: db/production.sqlite3

Now, edit the database.yml file to look something like this:

default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: postgres #username you provided when installing postgresql. By default its postgres(if you did not provide any)
password: xxxx #password you provided when installing postgresql

development:
<<: *default
database: hello_rails_development #name of your database

test:
<<: *default
database: hello_rails_test #name of your database

production:
<<: *default
database: Hello_production
username: Hello
password: <%= ENV["FRIENDLIST_DATABASE_PASSWORD"] %>

3. Create the database.

Finally, create database by running the following command:

rails db:create

This should create your database, and you're good to go.

Its literally that simple!

--

--

Technical Writer with a knack for explaining hard things in an easy way. All things tech!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
TechNotes by Abby

Technical Writer with a knack for explaining hard things in an easy way. All things tech!