22
31

More than 5 years have passed since last update.

sqliteからpostgresへの移行

Posted at

ローカルの開発環境で作成していたWebサービスをHeroku上で動かすため、
sqliteのデータベースをpostgresに変換したので、その時にしたことをメモ。
sqliteのデータベースの中身をそのまま移行するというより、
データベースのテーブル作成等の手間を楽にする為の手順(だと思う)。

ローカルにpostgresの環境構築

postgresqlをダウンロードしてインストール

環境変数の変更

.bash_rcに以下を追加

export PATH="/Applications/Postgres93.app/Contents/MacOS/bin:$PATH"

postgresに慣れておく

  • ドットインストールでpostgresの基本操作を一通り覚える

参考URL

sqliteのデータベースをpostgresのデータベースへ変換

sqliteのデータベースに対する操作をダンプ

sqlite3 .dump > sqlite_dumpfile.sql

型の修正

  • id integer primary keyを変更

id integer primary key -> id serial primary key

postgresのデータベースの作成

  • データベースの作成

$createdb "dbname"

  • 作成したデータベースに接続

$psql "dbname"

  • sqlite3でダンプしたファイルを読み込む

=#\i ./sqlite_dumpfile.sql

heroku上のデータベース初期化

  • heroku上のデータベースに接続

heroku pg:psql

  • ローカルと同様にダンプしたファイルを読み込む

=#\i ./sqlite_dumpfile.sql

22
31
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
22
31