kun432's blog

Alexaなどスマートスピーカーの話題中心に、Voiceflowの日本語情報を発信してます。たまにAWSやkubernetesなど。

〜スマートスピーカーやVoiceflowの記事は右メニューのカテゴリからどうぞ。〜

SQLite3のデータベースをPostgreSQLに移行する

メモです。あ、あけましておめでとうございます。今年もよろしくおねがいします。

故あって、SQLite3のデータベースをPostgreSQLに移行する必要が出ました。いろいろ調べてみるとダンプして取り込むよりはsequelを使うほうが楽、ということでやってみました。

本来はORMやDBI的に使うみたいですが、コマンドラインからも使えるようです。Ruby製のツールということでうちの環境はこんな感じです。

$ ruby -v
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-darwin21]

gemでインストールします。SQLite3とPostgreSQLにつなぐためのgemも必要です。

$ gem install sequel
$ gem install pg
$ gem install sqlite3

では移行します。あらかじめPostgreSQL側にはデータベースを作成しておいてください。

$ sequel -C sqlite://sample.db postgres://kun432@localhost/sample

実行中はこんな感じ。

Databases connections successful
Migrations dumped successfully
Tables created
Begin copying data
Begin copying records for table: sample1
Status: 5402 records copied
Status: 10865 records copied
Status: 16396 records copied
Status: 21604 records copied
Status: 26834 records copied
Status: 31849 records copied
Status: 35739 records copied
Finished copying 39718 records for table: sample1
Begin copying records for table: sample2
Status: 6587 records copied
Status: 13316 records copied
Status: 19638 records copied
Status: 26111 records copied
Status: 33362 records copied
(snip)
Finished copying data
Begin creating indexes
Finished creating indexes
Begin adding foreign key constraints
Finished adding foreign key constraints
Primary key sequences reset successfully
Database copy finished in 1624.627201 seconds

これでOKです。

ためしにMetabaseからつないでみたけど、SQLite3だと非常に重たかったのがクイックに使えてやっぱりRDBはいいなと再認識。