DEV Community

Michelle Loh
Michelle Loh

Posted on • Updated on

Ruby on Rails: Switch From sqlite3 to Postgres using asdf

This article will be using asdf to install postgres and then refer to the article Ruby on Rails: Switch From sqlite3 to Postgres by Brenden Thornton to switch from sqlite3 to Postgres in Ruby on Rails.


Install required libraries in Ubuntu

sudo apt-get install build-essential libssl-dev libreadline-dev zlib1g-dev libcurl4-openssl-dev uuid-dev
Enter fullscreen mode Exit fullscreen mode

If you have not installed asdf, kindly checkout Install asdf (ruby, nodejs and yarn) in WSL2 to install asdf, else just follow the following steps:

Add postgres plugin

$ asdf plugin add postgres
Enter fullscreen mode Exit fullscreen mode

Install postgres

asdf install postgres latest
Enter fullscreen mode Exit fullscreen mode
  • Then, check the version installed (the versions mentioned here is the latest version I downloaded)
$ asdf list
postgres
  14.2
Enter fullscreen mode Exit fullscreen mode
  • Add to your shell
asdf shell postgres 14.2
Enter fullscreen mode Exit fullscreen mode
  • Add to global
asdf global postgres 14.2
Enter fullscreen mode Exit fullscreen mode

Start postgres

pg_ctl start
Enter fullscreen mode Exit fullscreen mode

The rest refer Ruby on Rails: Switch From sqlite3 to Postgres from Update Gemfile to the end of the article.


NOTE:

  • For replacing sqlite3 to postgres in database.yml configuration, remember to switch the database and username to your database name and your username (check the code below with []). You can checkout GitHub on the configuration of database.yml. Note that the [database_name] should be different for different environment.
development:
  <<: *default
  database: [database_name_dev]

test:
  <<: *default
  database: [database_name_test]

production:
  <<: *default
  database: [database_name_prod]
Enter fullscreen mode Exit fullscreen mode
  • I use
rake db:create:all
Enter fullscreen mode Exit fullscreen mode

instead of

rake db:setup
Enter fullscreen mode Exit fullscreen mode

to create database since I also want to create production database. By default, rake db:setup will not create production database even though we have mentioned production in the database.yml. Checkout Stackoverflow to understand the differences of some of the commands.

  • rake db:create creates the database for that particular environment (either is development, test or production) but rake db:create:all creates the databases for all these 3 environments.
  • Remember to separate development database, test database and production database with different names!
  • Check out "Run" and "Stop" to run and stop postgres in asdf-postgres documentation

Cheers~ Happy Coding!


Top comments (0)