kei0425tan’s blog

技術的なことを主に

PostgreSQLのtemplate0,template1,postgresとかバックアップとか

仕事でPostgreSQLを利用しているのですが、いろいろと理解できていなかったところがあったので、その中でもtemplate0,template1,postgresについて

template0とtemplate1の違いについて

template1

createdb実行時に-Tで指定しない場合はデフォルトで雛形として利用されます。
雛形のため、template1を変更しても、変更以前に作成されたDBにはその変更は適用されません。
変更後に作成したDBには同じ変更が適用されます。

主に、コードセットの設定や、拡張コマンドなどをいれるとよいでしょう。
テーブルなども作成しておくことは可能ですが、あまり有効な利用方法は思いつきません。

template0

PostgreSQLの最低限必要もののみが設定されています。
変更はできません。
余計な変更が入っていると困る場合は、createdb実行時に-T template0を指定します。

posgres(DB名)について

デフォルトのDB名。qsqlなどコマンドで接続先を指定しない場合に利用されます。
サードパーティのユーティリティも、postgresDBに接続していろいろなコマンドを実行することが多いです。
削除はできません。

initdbについて

initdbを実行すると、まずtemplate1が作成されて、それをtemplate0とpostgresにコピーします。
マニュアルを読むと、template0を雛形にしてtemplate1が作成されるとありますが、実際には上記の順番で作成。
ただし、実質initdbした直後はtemplate0とtemplate1は同一のため、それほど気にする必要はなさそうです。

initdb /tmp/posttestを実行したログ

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /tmp/posttest ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
creating configuration files ... ok
creating template1 database in /tmp/posttest/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or

    • auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

/usr/local/pgsql/bin/postgres -D /tmp/posttest
or
/usr/local/pgsql/bin/pg_ctl -D /tmp/posttest -l logfile start

バックアップ pg_dump について

バックアップファイルには、そのDB作成時に利用したテンプレートの内容も含まれます。

https://www.postgresql.jp/document/9.3/html/backup-dump.html#BACKUP-DUMP-RESTORE

重要項目: pg_dumpで作成されるダンプはtemplate0と相対関係にあります。 つまりtemplate1を経由して追加されたあらゆる言語、プロシージャなどもpg_dumpによりダンプされます。 その結果としてリストアする際に、カスタマイズされたtemplate1を使用している場合は、上記の例のように、template0から空のデータベースを作成する必要があります。

DBをリストアする場合に、事前にDBを作成してリストアする場合には
template0を利用しないと、template1の変更点が重複するため、template0を利用するのがおすすめのようです。

https://www.postgresql.jp/document/9.3/html/app-pgrestore.html

template1データベースに対し独自の変更を行っている場合、pg_restoreの出力は、確実に空のデータベースにロードするよう注意してください。 そうしないと、おそらく追加されたオブジェクトの重複定義によってエラーが発生します。 独自の追加が反映されていない空のデータベースを作成するには、template1ではなくtemplate0をコピーしてください。 以下に例を示します。

リストア時には、新規データベースにて、template1で使えるようにした拡張コマンドがそのままでは利用できなくなります。

バックアップ pg_dumpallについて

pg_dumpallでは、以下をバックアップします。

  • 各ロール、ユーザ
  • 各ユーザ追加DB
  • template1の変更点
  • postgres(DB)の変更点
各ユーザ追加DB

バックアップファイルには、create database -T template0で作成するようになっています。
そのため、リストアした新規データベースでは、リストア前にtemplate1に対して行った変更は反映されません。

template1,postgres

バックアップファイルには、create databaseはなく、変更点のみになっています。
そのため、リストアした新規データベースでは、リストア前にtemplate1に対して行った変更はそのままで、さらに、バックアップ元で行った変更が行われます。
よって、拡張コマンドなどはそのまま利用できます。