Skip to content

Instantly share code, notes, and snippets.

@deepakmahakale
Last active October 7, 2024 11:02
Show Gist options
  • Save deepakmahakale/46477c0c57d0c3bdb180ed18a577356c to your computer and use it in GitHub Desktop.
Save deepakmahakale/46477c0c57d0c3bdb180ed18a577356c to your computer and use it in GitHub Desktop.
Switching Kamal 2.0 app from SQLite 3 to PostgreSQL

.env

KAMAL_REGISTRY_PASSWORD=dckr_pat_*******************o
POSTGRES_PASSWORD=blog_*******************_password

.kamal/secrets

KAMAL_REGISTRY_PASSWORD=$KAMAL_REGISTRY_PASSWORD
POSTGRES_PASSWORD=$POSTGRES_PASSWORD

RAILS_MASTER_KEY=$(cat config/master.key)

Dockerfile

 # Install base packages
 RUN apt-get update -qq && \
-    apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 && \
+    apt-get install --no-install-recommends -y curl libjemalloc2 libvips postgresql-client && \
     rm -rf /var/lib/apt/lists /var/cache/apt/archives
 

 # Install packages needed to build gems
 RUN apt-get update -qq && \
-    apt-get install --no-install-recommends -y build-essential git pkg-config && \
+    apt-get install --no-install-recommends -y build-essential git libpq-dev pkg-config && \
     rm -rf /var/lib/apt/lists /var/cache/apt/archives

Gemfile

-# Use sqlite3 as the database for Active Record
-gem "sqlite3", ">= 2.1"
+# Use pg as the database for Active Record
+gem "pg", "~> 1.1"

config/database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: blog_development

test:
  <<: *default
  database: blog_test

production:
  primary: &primary_production
    <<: *default
    host: <%= ENV["DB_HOST"] %>
    database: blog_production
    username: blog
    password: <%= ENV["POSTGRES_PASSWORD"] %>
  cache:
    <<: *primary_production
    database: blog_production_cache
    migrations_paths: db/cache_migrate
  queue:
    <<: *primary_production
    database: blog_production_queue
    migrations_paths: db/queue_migrate
  cable:
    <<: *primary_production
    database: blog_production_cable
    migrations_paths: db/cable_migrate

config/deploy.yml

env:
  secret:
    - RAILS_MASTER_KEY
    - POSTGRES_PASSWORD
  clear:
    DB_HOST: kamal-demo-blog-db
    POSTGRES_USER: blog
    POSTGRES_DB: blog_production
    SOLID_QUEUE_IN_PUMA: true

accessories:
  db:
    image: postgres:16
    host: 12.34.56.78
    port: 5432
    env:
      clear:
        DB_HOST: kamal-demo-blog-db
        POSTGRES_USER: blog
        POSTGRES_DB: blog_production
      secret:
        - POSTGRES_PASSWORD
    files:
      - db/production.sql:/docker-entrypoint-initdb.d/setup.sql
    directories:
      - data:/var/lib/postgresql/data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment