LoginSignup
0
1

プリザンターで異なる環境へデータベース(PostgreSQL)をリストアしたい

Last updated at Posted at 2023-08-24

はじめに

環境Aで取得したバックアップを環境Bへもっていき、リストアを試みたがエラーとなり、うまくリストアが出来なかったため、その対応方法について備忘録として残します。

どんなエラー

"Implem.Pleasanter_Owner" が存在しない旨のエラーが出る。下記は一部抜粋。

$ pg_restore -d Implem.Pleasanter /backup/Implem.Pleasanter.dump
pg_restore: while PROCESSING TOC:
pg_restore: from TOC entry 210; 1259 16468 TABLE AutoNumberings Implem.Pleasanter_Owner
pg_restore: error: could not execute query: ERROR:  role "Implem.Pleasanter_Owner" does not exist
Command was: ALTER TABLE public."AutoNumberings" OWNER TO "Implem.Pleasanter_Owner";

バックアップ

環境Aで以下のコマンドでデータベースのバックアップを取得する。

su - postgres
psql -U postgres
pg_dump -Fc Implem.Pleasanter > /backup/Implem.Pleasanter.dump

上記のバックアップはデータベースのみのバックアップであるため、ロールもバックアップを取得する。

pg_dumpall --roles-only > /backup/Implem.Pleasanter_role.dump

環境Aで取得した2ファイル(Implem.Pleasanter.dump,Implem.Pleasanter_role.dump)を環境Bの/backup ディレクトリへコピーする。

リストア

環境Bでデータベースを作成する。

su - postgres
psql -U postgres
create database "Implem.Pleasanter";

データベースが作成されていることを確認。

postgres=# \l
                                      List of databases
       Name        |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-------------------+----------+----------+-------------+-------------+-----------------------
 Implem.Pleasanter | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 postgres          | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 template0         | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
                   |          |          |             |             | postgres=CTc/postgres
 template1         | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
                   |          |          |             |             | postgres=CTc/postgres
(4 rows)

postgres=#

以下のコマンドでロールおよび、データベースのリストアを行う。

psql -f /backup/Implem.Pleasanter_role.dump
pg_restore -d Implem.Pleasanter /backup/Implem.Pleasanter.dump

ロールおよび、データベースが復元されていることを確認する。

su - postgres
psql -U postgres
postgres=# \du
                                          List of roles
        Role name        |                         Attributes                         | Member of
-------------------------+------------------------------------------------------------+-----------
 Implem.Pleasanter_Owner | Password valid until infinity                              | {}
 Implem.Pleasanter_User  | Password valid until infinity                              | {}
 postgres                | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=#
postgres=# \l
                                      List of databases
       Name        |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-------------------+----------+----------+-------------+-------------+-----------------------
 Implem.Pleasanter | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 postgres          | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 template0         | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
                   |          |          |             |             | postgres=CTc/postgres
 template1         | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
                   |          |          |             |             | postgres=CTc/postgres
(4 rows)

postgres=#
0
1
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
0
1