既存のローカルデータをまるごとステージングのデータに入れ替えたい、といったケースを想定しています。
実施環境
- OS: OS X El Capitan (10.11.6)
- Vagrant: 1.8.5
- Virtualbox: 5.1.2 r108956
- PostgreSQL: 9.3.10
1. DBの再作成(DROP -> CREATE)
step1. ローカルのDBサーバに接続する
$ vagrant ssh <dbサーバ>
step2. 全テーブル(スキーマ)を削除する
!全テーブルが同じスキーマにある必要があります
$ sudo -u postgres psql -U postgres -d <DB>
<DB>=# DROP schema public cascade;
step3. スキーマを再作成する
<DB>=# CREATE schema public;
step4. 接続ユーザに権限を付与する
<DB>=# GRANT ALL ON schema public TO <user>;
2. レストア
step1. ローカルのDBサーバに接続する
$ vagrant ssh <dbサーバ>
step2. ステージング環境からデータをレストアする
# 直接挿入する場合
$ pg_dump -h <取得元> -d <DB> -n public -U <user> | psql [-h <挿入先>] -U <user> -d <DB>
# 一度ファイルに保存する場合
$ pg_dump -h <取得元> -d <DB> -n public -U <user> > <file>
$ psql [-h <挿入先>] -U <user> -d <DB> < <file>
参考
Drop all tables in postgresql?
Postgres INSERT ERROR: permission denied for schema public