[RUBY] Change from SQLite3 to PostgreSQL


When creating a project

rails new sample_app -d postgresql

Where to say

rails new sample_app -D postgresql

PostgreSQL was not set because it was.

In Gemfile

# Use postgresql as the database for Active Record
gem 'pg'

I want you to be

# Use sqlite3 as the database for Active Record
gem 'sqlite3'

It was.

I wonder if sqlite3 is the default one.

Change from SQLite3 to PostgreSQL

https://qiita.com/rubys8arks/items/0749d6fa73e88d3d381c Let's change it with reference to this.

Since PostgreSQL itself is installed, At sqlite3 in the Gemfile

# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'

Change to and do bundle install.

In this state, rails db: create cannot be done

bin/rails db:create
rails aborted!
LoadError: Error loading the 'sqlite3' Active Record adapter. Missing a gem it depends on? sqlite3 is not part of the bundle. Add it to your Gemfile.

Edit database.yml


# SQLite version 3.x
#   gem install sqlite3
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

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

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
  <<: *default
  database: db/test.sqlite3

  <<: *default
  database: db/production.sqlite3


default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

  <<: *default
  database: sample_app_development

  <<: *default
  database: sample_app_test

  <<: *default
  database: sample_app_production
  username: sample_app

Password to .env

Omitted here

Creating a database

-> % bin/rails db:create
Created database 'sample_app_development'
Created database 'sample_app_test'

Recommended Posts

Change from SQLite3 to PostgreSQL
Change DB from SQLite to MySQL
Connect from Java to PostgreSQL
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
How to change from HTML to Haml
Change ownCloud database from default SQLite to MariaDB etc. (CentOS7)
Job change from SIer to Web engineer
I want to write quickly from java to sqlite
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Migrate from JUnit 4 to JUnit 5
From Java to Ruby !!
How to change from Oracle Java 8 to Adopt Open JDK 9
[Ruby on Rails] From MySQL construction to database change
How to change a TERASOLUNA 5.x blank project to support PostgreSQL
Moved from iBATIS to MyBatis3
Try Spring Boot from 0 to 100.
Migration from Cobol to JAVA
Switch from slim3-gen to slim3-gen-jsr269
[JDBC] I tried to access the SQLite3 database from Java.
New features from Java7 to Java8
Migrating from vargrant to docker
Convert from ○ months to ○ years ○ months
Change only one character from the String type string to uppercase
From Ineffective Java to Effective Java
How to migrate from JUnit4 to JUnit5
If you want to change the Java development environment from Eclipse
PostgreSQL environment construction with Docker (from setup to just before development)
I am studying to change jobs from inexperienced to a web engineer.
How to push from Tarminal to GitHub
protocol buffeer migration from 2.x to 3.x
[Note] Download from S3, upload to S3
Stop resending from client to server
Ubuntu Desktop upgrade from 18.0.4 (?) To 20.04.1 (focal)
How to set Spring Boot + PostgreSQL
How to change kube-proxy to ipvs mode.
Migrate from on-premise Pukiwiki to esa.io \ (⁰⊖⁰) /
From introduction to use of ActiveHash
Switch from JSP + JSTL to Thymeleaf
From Java to VB.NET-Writing Contrast Memo-
Introduction to Ruby (from other languages)
Java, interface to start from beginner
Notes on migrating from CircleCI 1.0 to 2.0
Addicted to project imports from GitHub
Upgrade spring boot from 1.5 series to 2.0 series
We aim to update daily from 2021
Switch from Eclipse to VS Code
The road from JavaScript to Java
Rails DB PostgreSQL changed to MySQL
Memorandum Poem (updated from time to time)
[Java] Conversion from array to List
Update MySQL from 5.7 to 8.0 with Docker
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
Change the half-width space of STS (Spring Tool Suite) from "u" to "・"