LoginSignup
7
9

More than 5 years have passed since last update.

RailsでSQLite3からPostgreSQLに乗り換えたいときにやること

Last updated at Posted at 2019-02-17

デフォルトのSQLite3のまま開発してて、サーバーHerokuにしたいからPostgreSQLに切り替えとこと思ったときにやった乗り換え手順です。

環境

CentOS7

[root@localhost app]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 

PostgreSQL側の設定

リポジトリの追加

yum -y localinstall https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm

POstgresSQLインストール

yum -y install postgresql10-server

インストールできてるかバーション表示して確認

[root@localhost app]# /usr/pgsql-10/bin/postgres --version
postgres (PostgreSQL) 10.7

DBの初期化

[root@localhost app]#  /usr/pgsql-10/bin/postgresql-10-setup initdb
Initializing database ... OK

サービス登録と起動

サービス登録して

[root@localhost app]#  systemctl enable postgresql-10

起動

[root@localhost app]# systemctl start postgresql-10

起動できてるか確認

[root@localhost app]# systemctl status postgresql-10
● postgresql-10.service - PostgreSQL 10 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-10.service; enabled; vendor preset: disabled)
   Active: active (running) since �� 2019-02-17 09:26:45 CET; 28s ago

ユーザー作成

ログイン

[root@localhost app]# sudo -u postgres psql -U postgres

スーパーユーザーを作成
以下ではhogehogeユーザーをhogehogeパスワードで作成している

postgres=# create role hogehoge with superuser login password 'hogehoge';

確認

postgres=# \du
                                                                         ロール一覧
       ロール名       |                                 属性                                 |                           メンバー                           
----------------------+----------------------------------------------------------------------+--------------------------------------------------------------
 hogehoge             | スーパーユーザ                                                       | {}
 pg_monitor           | ログインできない                                                     | {pg_read_all_settings,pg_read_all_stats,pg_stat_scan_tables}
 pg_read_all_settings | ログインできない                                                     | {}
 pg_read_all_stats    | ログインできない                                                     | {}
 pg_signal_backend    | ログインできない                                                     | {}
 pg_stat_scan_tables  | ログインできない                                                     | {}
 postgres             | スーパーユーザ, ロールを作成できる, DBを作成できる, レプリケーション | {}

認証方式変更

初期のままだとOSのユーザーを使ってログインするようになっていると思うので、DB側に設定したユーザーとパスワードでログインするようにMethodをmd5に変更する。

/var/lib/pgsql/10/data/pg_hba.conf
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

設定反映させるためリスタート

[root@localhost app]# systemctl restart postgresql-10

Rails側

pg gemを追加する

sqlite3は使わないのでコメントアウトした

# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
gem 'pg', '~> 0.18.4'

バンドルインストールする

[root@localhost app]# bundle install

database.ymlの編集

database.ymlを以下のように書き換える。元からあったsqlite3の設定は使わないので消す。

config/database.yml
default: &default
  adapter: postgresql
  encoding: EUC_JP
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: hogehoge
  password: hogehoge
  host: localhost
  timeout: 5000

development:
  <<: *default
# データベースの名前。アプリケーションの名前_環境にする
  database: applicationname_development

# 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.
test:
  <<: *default
# データベースの名前。アプリケーションの名前_環境にする
  database: applicationname_test

production:
  <<: *default
# データベースの名前。アプリケーションの名前_環境にする
  database: applicationname_production

データベースを作成する

[root@localhost app]# rails db:create
Created database 'hogehoge_development'
Created database 'hogehoge_test'

マイグレートしてテーブルを作成する

[root@localhost app]# rails db:migrate

これで完了

追記

このままRailsを動かすと保存するときにエラーが起きるので、
encodingをutf8に変えたら正常に戻る。

ActiveRecord::StatementInvalid (PG::CharacterNotInRepertoire: ERROR:  ��沽����"EUC_JP"���Ф��������ʥХ�����Ǥ�: 0x96 0xe7
config/database.yml
default: &default
  adapter: postgresql
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: hogehoge
  password: hogehoge
  host: localhost
  timeout: 5000
7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9