LoginSignup
1
2

More than 3 years have passed since last update.

PostgresのDBバックアップ・リストア簡易検証手順

Posted at

docker-compose.ymlを使ってPostgresインスタンス起動。

  • mydb1: バックアップ対象のインスタンス
  • mydb2: リストア先のインスタンス
docker-compose.yml
version: '3.1'

services:
  mydb1:
    image: postgres:9.6
    restart: always
    environment:
      POSTGRES_DB: example1
      POSTGRES_USER: example
      POSTGRES_PASSWORD: example
    ports:
      - '15432:5432'
  mydb2:
    image: postgres:9.6
    environment:
      POSTGRES_DB: example2
      POSTGRES_USER: example
      POSTGRES_PASSWORD: example
    ports:
      - '25432:5432'

Postgresインスタンスの起動

docker-compose up

テスト用にデータを投入する。

psql -U example -h localhost -p 15432 -d example1

> create table users(id serial primary key, name varchar);
> insert into users (name) values ('hanako'), ('taro');

mydb1をバックアップする

pg_dump -U example -h localhost -p 15432 -d example1 > exmple1.dump

mydb2にリストアする

psql -U example -h localhost -p 25432 -d example2 -f exmple1.dump

リストア後の確認

psql -U example -h localhost -p 25432 -d example2

> select * from users;
|id|name  |
|--|------|
| 1|hanako|
| 2|taro  |
1
2
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
2