Choc-Net!
PostgreSQLのバックアップリストア手順
最終更新:
choc-net
-
view
【Linux】PostgreSQLのバックアップリストア手順
バックアップ
データベースのバックアップ方法です。
- postgress のルートユーザでログイン
コマンド # su - <PostgreSQLルートユーザ>
例) # su -postgres
- バックアップ
postgres$ pg_dump -U postgres -F c -f HOGE_DB_BACK.car HOGE_DB
データベースのリストア
データベースごとリストアする方法です。
- postgress のルートユーザでログイン
コマンド # su - <PostgreSQLルートユーザ>
例) # su -postgres
- 旧データベース削除(必要に応じて)
リストア先の情報が残っている場合は、先に削除。
postgres$ dropdb <データベース名>
例) postgres$ dropdbHOGE_DB
- データベース作成
postgres$ createdb -U <データベースユーザ名> HOGE_DB --encoding=EUC_JP
例) postgres$ createdb -Uhoge0usr1HOGE_DB--encoding=EUC_JP(*1)
- リストアの実行
postgres$ pg_restore -U postgres -d <データベース名> -F c <バックアップファイル名>
例) postgres$ pg_restore -Uhoge0usr1 -dHOGE_DB -F cHOGE_DB_BACK.car
テーブルのリストア
特定のテーブルをリストアする方法です。
- postgress のルートユーザでログイン
コマンド # su - <PostgreSQLルートユーザ>
例) # su -postgres
- 旧テーブル削除(必要に応じて)
リストア先の情報が残っている場合は、先に削除。
postgres$ psql <データベース名> HOGE_DB=# DROP TABLE <テーブル名> HOGE_DB=# \q
例) postgres$ psqlHOGE_DB
postgres$ DROP TABLEPAGE_MST
postgres$ \q
postgres$ DROP TABLEPAGE_MST
postgres$ \q
- リストアの実行
postgres$ pg_restore -U <データベースユーザ名> -d <データベース名> -t <テーブル名> -F c <バックアップファイル名>
例) postgres$ pg_restore -Uhoge0usr1 -dHOGE_DB -tPAGE_TBL -F cHOGE_DB_BACK.car