Rails6: Switching SQlite3 to Postgres
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 dbname with your selected db in the command below:
rails new project -d dbname
Switch SQlite3 to Postgres
- Install Postgresql (if not already installed in the project directory):
gem install pg
- Update Gemfile:
cd Project
nano Gemfile
Replace the following line:
gem 'sqlite3'
With:
gem 'pg'
Once all changes are made, run
bundle installto update theGemfile.lockfile.
- Update the
database.ymlfile
The
databse.ymlfile 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
- Remove all the above lines from the
database.yml.
-
set
postgresqlas 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
- 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:
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved