
前提
1️⃣ 前PCで DB をダンプ
ターミナルで以下を実行します
pg_dump -U <ユーザー名> -h localhost -F c -b -v -f myapp.dump <DB名>
<ユーザー名>:Postgresユーザー名<DB名>:Railsプロジェクトの DB 名(config/database.ymlの development/production)-F c:カスタム形式(復元が簡単)-b:バイナリデータも含める-v:詳細ログ出力
例
pg_dump -U postgres -h localhost -F c -b -v -f meetour.dump hpt8_development
Password:
pg_dump: last built-in OID is 16383
pg_dump: reading extensions
pg_dump: identifying extension members
pg_dump: reading schemas
pg_dump: reading user-defined tables
pg_dump: reading user-defined functions
pg_dump: reading user-defined types
pg_dump: reading procedural languages
pg_dump: reading user-defined aggregate functions
pg_dump: reading user-defined operators
pg_dump: reading user-defined access methods
pg_dump: reading user-defined operator classes
pg_dump: reading user-defined operator families
pg_dump: reading user-defined text search parsers
pg_dump: reading user-defined text search templates
pg_dump: reading user-defined text search dictionaries
pg_dump: reading user-defined text search configurations
pg_dump: reading user-defined foreign-data wrappers
pg_dump: reading user-defined foreign servers
pg_dump: reading default privileges
pg_dump: reading user-defined collations
pg_dump: reading user-defined conversions
pg_dump: reading type casts
pg_dump: reading transforms
pg_dump: reading table inheritance information
pg_dump: reading event triggers
pg_dump: finding extension tables
pg_dump: finding inheritance relationships
pg_dump: reading column info for interesting tables
pg_dump: finding table default expressions
pg_dump: flagging inherited columns in subtables
pg_dump: reading partitioning data
pg_dump: reading indexes
pg_dump: flagging indexes in partitioned tables
pg_dump: reading extended statistics
pg_dump: reading constraints
pg_dump: reading triggers
pg_dump: reading rewrite rules
pg_dump: reading policies
pg_dump: reading row-level security policies
pg_dump: reading publications
pg_dump: reading publication membership of tables
pg_dump: reading publication membership of schemas
pg_dump: reading subscriptions
pg_dump: reading subscription membership of tables
pg_dump: reading large objects
pg_dump: reading dependency data
pg_dump: saving encoding = UTF8
pg_dump: saving "standard_conforming_strings = on"
pg_dump: saving "search_path = "
pg_dump: saving database definition
pg_dump: dumping contents of table "public.action_text_rich_texts"
pg_dump: dumping contents of table "public.active_storage_attachments"
pg_dump: dumping contents of table "public.active_storage_blobs"
pg_dump: dumping contents of table "public.active_storage_variant_records"
pg_dump: dumping contents of table "public.addresses"
pg_dump: dumping contents of table "public.ar_internal_metadata"
pg_dump: dumping contents of table "public.categories"
pg_dump: dumping contents of table "public.conversations"
pg_dump: dumping contents of table "public.event_orders"
pg_dump: dumping contents of table "public.event_pricings"
pg_dump: dumping contents of table "public.event_reviews"
pg_dump: dumping contents of table "public.events"
pg_dump: dumping contents of table "public.gigs"
pg_dump: dumping contents of table "public.local_government_taxes"
pg_dump: dumping contents of table "public.local_governments"
pg_dump: dumping contents of table "public.lodging_reviews"
pg_dump: dumping contents of table "public.messages"
pg_dump: dumping contents of table "public.notifications"
pg_dump: dumping contents of table "public.orders"
pg_dump: dumping contents of table "public.pricings"
pg_dump: dumping contents of table "public.reservations"
pg_dump: dumping contents of table "public.reviews"
pg_dump: dumping contents of table "public.roles"
pg_dump: dumping contents of table "public.room_availabilities"
pg_dump: dumping contents of table "public.rooms"
pg_dump: dumping contents of table "public.schema_migrations"
pg_dump: dumping contents of table "public.user_roles"
pg_dump: dumping contents of table "public.users"
2️⃣ ダンプファイルを新PCにコピー
USB、クラウド、ネットワークなどで myapp.dump を新PCに移動。
3️⃣ 新PCで DB を一度削除して作り直す
Rails で作った空DBにデータを追加する方法もあるが、外部キーやユニーク制約で失敗することが多いので DBを一度削除して復元するのが安全。
rails db:drop
rails db:create
これで完全に空の DB が作成されます
4️⃣ ダンプから復元
pg_restore -U <ユーザー名> -d <DB名> -v myapp.dump
例
pg_restore -U postgres -d hpt8_development -v meetour.dump
💡 まとめ
- 前PCで
pg_dumpでダンプ - ダンプを新PCにコピー
- 新PCで DB を削除 & 作成
pg_restoreで復元- Rails コンソールでデータ確認
これだけで、Rails 8.1 + PostgreSQL の環境移行は安全に完了します。
082p
作業お疲れ様でした!