データベースの操作メモ

やり方を忘れないようにするためのメモです。

Postgres

Docker上のPostgresのバックアップ

bash
docker exec -i [container_id or name] pg_dumpall -U [username] > [filename].sql
bash
docker exec -i [container_id or name] pg_dump -Fc -h localhost -p 5432 -U [username] -d [database] > backup.dump

Docker上のPostgresのリストア

bash
cat [filename].sql | docker exec -i [container_id or name] psql -h localhost -p 5432 -U [username] [database]
bash
cat [filename].dump | sudo docker exec -i [container_id or name] pg_restore --clean -h localhost -p 5432 -U [username] -d [database]

ソースのPostgresのバックアップ

bash
vim ~/.pgpass ホスト名:ポート:データベース名:ユーザ:パスワード 例):localhost:5432:postgres:user:password pg_dump -h localhost -p 5432 -U [username] [db name] > [filename].sql

ソースのPostgresのリストア

bash
pg_restore -h localhost -p 5432 -U [username] [db name] < [filename].sql
ランキング