Forem

Michelle Loh
Michelle Loh

Posted on • Edited on

3 2

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!


Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay