LoginSignup
1
1

More than 5 years have passed since last update.

既存のpostgresから初期設定のDBを作りたいんです

Posted at

http://xoyip.hatenablog.com/entry/2014/05/18/200000
この手が良さそうだと思った。やはりDBからdumpが簡単そうなので。DBの汎化が必要になったらいろいろと考えれば良いと思う。

ファイルフォーマット的にはここまでの作業で問題なくなったが、余計なデータが一種類入っている。それはschema_migrationのデータだ。

とのことだったので一発でいけないか調査。一次情報を当たる。
http://www.postgresql.org/docs/9.3/static/app-pgdump.html

--exclude-tableを使えば良い。

pg_dump mydatabase --data-only --inserts --exclude-table=schema_migrations > seeds.sql

そんなわけでこれで準備OK。

./db/seeds.sqlとして./db/seeds.rbに以下を記述

sql = open(File.join(Rails.root, "db", "seeds.sql")).read
ActiveRecord::Base.connection.execute sql

あとはdbの準備をしたら下記を実行でOK。

rake db:migrate
rake db:seed
1
1
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
1
1