DEV Community

radin reth
radin reth

Posted on

2

Update postgres to sqlite3 for rails 6 using Docker Compose

Today I am gonna show how to switch database from postgres to sqlite3 in rails application.

  1. Update Gemfile
gem 'sqlite3', '~> 1.3', '>= 1.3.3'
Enter fullscreen mode Exit fullscreen mode
  1. Open Dockerfile and install sqlite3 package
FROM ruby:3.0.0

LABEL maintainer="Radin <radin@instedd.org>"

RUN curl -sL https://deb.nodesource.com/setup_14.x | bash - && \
  echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
  curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
  apt-get update -qq && \
  apt-get install -y vim nodejs sqlite3 libsqlite3-dev yarn && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN mkdir /app
WORKDIR /app

COPY Gemfile* package.json yarn.lock /app/

RUN gem install bundler -v 2.1.4 && \
  bundle install --jobs 20

COPY . /app
Enter fullscreen mode Exit fullscreen mode
  1. Update docker-compose.yml
version: '3'

volumes:
  bundle:
  db:

services:
  db:
    image: nouchka/sqlite3:latest
    volumes:
      - db:/root/db
    stdin_open: true
    tty: true

  web: &rails
    build:
      context: .
      dockerfile: Dockerfile
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -b '0.0.0.0'"
    volumes:
      - .:/app
      - bundle:/usr/local/bundle
      - ./node_modules:/app/node_modules
    tmpfs: /app/tmp
    depends_on:
      - db
    env_file: app.env
    tty: true
    stdin_open: true
    ports:
      - "3000:3000"
Enter fullscreen mode Exit fullscreen mode
  1. And finally, Update config/database.yml
default: &default
  adapter: sqlite3
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

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

test:
  <<: *default
  database: db/mydb_test.sqlite3
Enter fullscreen mode Exit fullscreen mode

Cheer!

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up