Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

database
rails6
sqlite3

Rails6: Switching SQlite3 to Postgres

Ammar Ahmad Farid

A rails app uses SQlite3 as a built-in database.

The following command will create a new rails project with SQlite3:

rails new project

If you want to change the databasePostgres, DB2, MYSQL, and SQL Server, replace dbname with your selected db in the command below:

rails new project -d dbname

Switch SQlite3 to Postgres

  1. Install Postgresql (if not already installed in the project directory):
gem install pg
  1. Update Gemfile:
cd Project
nano Gemfile

Replace the following line:

gem 'sqlite3'

With:

gem 'pg'

Once all changes are made, run bundle install to update the Gemfile.lock file.

  1. Update the database.yml file

The databse.yml file has the following commands:

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

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

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

production:
  <<: *default
  database: db/production.sqlite3
  1. Remove all the above lines from the database.yml.
  • set postgresql as adapter

  • set unicode as encoding

  • set your application name as database name

  • set pool = 5

The final development version will be:


development:
  adapter: postgresql
  encoding: unicode
  database: yourApp_development
  pool: 5
  username: username
  password:
  timeout: 5000

Then, repeat all the steps for test:

test:
adapter: postgresql
encoding: unicode
database: youApp_test
pool: 5
username: username
password:
timeout: 5000
  1. After making all changes, run the following commands:
rake db:setup
...
...some messages...
...
rake db:migrate

Try all the above commands, one by one, in the terminal below:

Terminal 1
Terminal

RELATED TAGS

database
rails6
sqlite3

CONTRIBUTOR

Ammar Ahmad Farid
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring