バージョンごとのドキュメント一覧

26.1. SQLによるダンプ #

<title><acronym>SQL</acronym> Dump</title>

The idea behind this dump method is to generate a file with SQL commands that, when fed back to the server, will recreate the database in the same state as it was at the time of the dump. <productname>PostgreSQL</productname> provides the utility program <xref linkend="app-pgdump"/> for this purpose. The basic usage of this command is: このダンプ方法の背景にはSQLコマンドでファイルを生成し、そのファイルをサーバが再度読み込みを行った時に、ダンプした時点と同じ状態が再構築されるという意図があります。 この目的のため、PostgreSQLpg_dumpユーティリティプログラムを提供しています。 このコマンドの基本となる使い方は以下の通りです。

pg_dump dbname > dumpfile

As you see, <application>pg_dump</application> writes its result to the standard output. We will see below how this can be useful. While the above command creates a text file, <application>pg_dump</application> can create files in other formats that allow for parallelism and more fine-grained control of object restoration. 見てわかる通り、pg_dumpは結果を標準出力に書き出します。 これがどのように活用できるかをこれから説明します。 上記のコマンドはテキストファイルを作成しますが、pg_dumpは並列処理を可能にしたり、オブジェクトのリストアをより細かく制御できる他のフォーマットでファイルを作れます。

<application>pg_dump</application> is a regular <productname>PostgreSQL</productname> client application (albeit a particularly clever one). This means that you can perform this backup procedure from any remote host that has access to the database. But remember that <application>pg_dump</application> does not operate with special permissions. In particular, it must have read access to all tables that you want to back up, so in order to back up the entire database you almost always have to run it as a database superuser. (If you do not have sufficient privileges to back up the entire database, you can still back up portions of the database to which you do have access using options such as <option>-n <replaceable>schema</replaceable></option> or <option>-t <replaceable>table</replaceable></option>.) pg_dumpは、PostgreSQLの通常のクライアントアプリケーションです(その中でも特に優れた機能を発揮するものですが)。 ということは、データベースに接続可能なあらゆるリモートホストからこのバックアップ手順を実行できます。 しかし、pg_dumpは特別な権限で実行される訳ではないことを忘れないでください。 特に、バックアップを行う全てのテーブルに対して読み取り権限が必要ですので、データベース全体のバックアップを実行する場合、ほとんど常にデータベースのスーパーユーザとして実行しなければなりません。 (もしデータベース全体のバックアップを取るのに十分な権限を持っていない場合には、-n schemaもしくは、-t tableのようなオプションを使って、データベースのアクセス権のある部分をバックアップできます。)

To specify which database server <application>pg_dump</application> should contact, use the command line options <option>-h <replaceable>host</replaceable></option> and <option>-p <replaceable>port</replaceable></option>. The default host is the local host or whatever your <envar>PGHOST</envar> environment variable specifies. Similarly, the default port is indicated by the <envar>PGPORT</envar> environment variable or, failing that, by the compiled-in default. (Conveniently, the server will normally have the same compiled-in default.) pg_dumpを行うデータベースサーバを特定するにはコマンドラインの-h hostオプションと-p portオプションを使用します。 デフォルトのホストはローカルホスト、またはPGHOST環境変数で指定したものです。 同様に、デフォルトのポートはPGPORT環境変数で指定されているか、うまく行かない場合にはコンパイル時の設定がデフォルトとなります(そこはうまくできていて、サーバは通常コンパイル時の設定をデフォルトとします)。

Like any other <productname>PostgreSQL</productname> client application, <application>pg_dump</application> will by default connect with the database user name that is equal to the current operating system user name. To override this, either specify the <option>-U</option> option or set the environment variable <envar>PGUSER</envar>. Remember that <application>pg_dump</application> connections are subject to the normal client authentication mechanisms (which are described in <xref linkend="client-authentication"/>). 他のPostgreSQLのクライアントアプリケーションのように、pg_dumpはデフォルトでオペレーティングシステムの現在のユーザ名と同じデータベースユーザ名で接続します。 これを書き換えるには-Uオプションを付けるかPGUSER環境変数を設定します。 pg_dumpの接続は(第21章で説明されている)通常のクライアント認証方法によることを思い出してください。

An important advantage of <application>pg_dump</application> over the other backup methods described later is that <application>pg_dump</application>'s output can generally be re-loaded into newer versions of <productname>PostgreSQL</productname>, whereas file-level backups and continuous archiving are both extremely server-version-specific. <application>pg_dump</application> is also the only method that will work when transferring a database to a different machine architecture, such as going from a 32-bit to a 64-bit server. 後で述べる他のバックアップ手法に対するpg_dumpの重要な利点は、pg_dumpの出力は一般に新しいバージョンのPostgreSQLに再ロードできるということです。 一方、ファイルレベルのバックアップと継続的アーカイブは両方とも非常にサーバ、バージョン依存です。 pg_dumpは、32ビットから64ビットのサーバに移行するなどの異なるマシンアーキテクチャにデータベースを移す場合に上手くいく唯一の方法でもあります。

Dumps created by <application>pg_dump</application> are internally consistent, meaning, the dump represents a snapshot of the database at the time <application>pg_dump</application> began running. <application>pg_dump</application> does not block other operations on the database while it is working. (Exceptions are those operations that need to operate with an exclusive lock, such as most forms of <command>ALTER TABLE</command>.) pg_dumpで作成されたダンプは、内部的に整合性があります。 つまり、ダンプはpg_dumpが開始された際のデータベースのスナップショットを示しています。 pg_dumpの操作はデータベースに対する他の作業を妨げません(ALTER TABLEのほとんどの形態であるような排他的ロックが必要な作業は例外です)。

26.1.1. ダンプのリストア #

<title>Restoring the Dump</title>

Text files created by <application>pg_dump</application> are intended to be read in by the <application>psql</application> program. The general command form to restore a dump is pg_dumpで作成されたテキストファイルはpsqlプログラムで読み込まれることを意図しています。 以下に、ダンプをリストアする一般的なコマンドを示します。

psql dbname < dumpfile

where <replaceable class="parameter">dumpfile</replaceable> is the file output by the <application>pg_dump</application> command. The database <replaceable class="parameter">dbname</replaceable> will not be created by this command, so you must create it yourself from <literal>template0</literal> before executing <application>psql</application> (e.g., with <literal>createdb -T template0 <replaceable class="parameter">dbname</replaceable></literal>). <application>psql</application> supports options similar to <application>pg_dump</application> for specifying the database server to connect to and the user name to use. See the <xref linkend="app-psql"/> reference page for more information. Non-text file dumps are restored using the <xref linkend="app-pgrestore"/> utility. ここでdumpfilepg_dumpコマンドにより出力されたファイルです。 dbnameデータベースはこのコマンドでは作成されません。 (例えばcreatedb -T template0 dbname のようにして)psqlを実行する前に自分でtemplate0から作成してください。 psqlpg_dumpと似たような、接続データベースサーバと使用するユーザ名を指定するオプションに対応しています。 詳細については、psqlのリファレンスページを参照してください。 テキスト形式ではないダンプファイルはpg_restore ユーティリティを使いリストアします。

Before restoring an SQL dump, all the users who own objects or were granted permissions on objects in the dumped database must already exist. If they do not, the restore will fail to recreate the objects with the original ownership and/or permissions. (Sometimes this is what you want, but usually it is not.) SQLダンプのリストアを実行する前に、ダンプされたデータベース内のオブジェクトを所有するユーザやそのオブジェクト上に権限を与えられたユーザも存在しなければなりません。 存在していない場合、リストアはそのオブジェクトの元々の所有権や付与された権限を再作成することができません (このようにしたい場合もあるでしょうが、通常そうではありません)。

By default, the <application>psql</application> script will continue to execute after an SQL error is encountered. You might wish to run <application>psql</application> with the <literal>ON_ERROR_STOP</literal> variable set to alter that behavior and have <application>psql</application> exit with an exit status of 3 if an SQL error occurs: デフォルトでpsqlスクリプトは、SQLエラーが起きた後も実行を継続します。 ON_ERROR_STOP変数を設定してpsqlを実行することで、その動作を変更し、SQLエラーが起きた場合にpsqlが、終了ステータス3で終了するようにしたいと思うかもしれません。

psql --set ON_ERROR_STOP=on dbname < dumpfile

Either way, you will only have a partially restored database. Alternatively, you can specify that the whole dump should be restored as a single transaction, so the restore is either fully completed or fully rolled back. This mode can be specified by passing the <option>-1</option> or <option>&#45;-single-transaction</option> command-line options to <application>psql</application>. When using this mode, be aware that even a minor error can rollback a restore that has already run for many hours. However, that might still be preferable to manually cleaning up a complex database after a partially restored dump. どちらにしても、部分的にリストアされたデータベースにしかなりません。 他に、ダンプ全体を1つのトランザクションとしてリストアするように指定することができます。 こうすれば、リストアが完全に終わるか、完全にロールバックされるかのどちらかになります。 このモードは、psqlのコマンドラインオプションに-1または--single-transactionを渡すことで指定できます。 このモードを使用する場合、数時間かけて実行していたリストアが軽微なエラーでロールバックしてしまうことに注意してください。 しかし、部分的にリストアされたダンプから手作業で複雑なデータベースを整理するよりまだましかもしれません。

The ability of <application>pg_dump</application> and <application>psql</application> to write to or read from pipes makes it possible to dump a database directly from one server to another, for example: pg_dumppsqlではパイプから読み書きができるので、あるサーバから別のサーバへデータベースを直接ダンプできます。 以下に例を示します。

pg_dump -h host1 dbname | psql -h host2 dbname

重要

The dumps produced by <application>pg_dump</application> are relative to <literal>template0</literal>. This means that any languages, procedures, etc. added via <literal>template1</literal> will also be dumped by <application>pg_dump</application>. As a result, when restoring, if you are using a customized <literal>template1</literal>, you must create the empty database from <literal>template0</literal>, as in the example above. pg_dumpで作成されるダンプはtemplate0と相対関係にあります。 つまりtemplate1を経由して追加されたあらゆる言語、プロシージャなどもpg_dumpによりダンプされます。 その結果としてリストアする際に、カスタマイズされたtemplate1を使用している場合は、上記の例のように、template0から空のデータベースを作成する必要があります。

After restoring a backup, it is wise to run <link linkend="sql-analyze"><command>ANALYZE</command></link> on each database so the query optimizer has useful statistics; see <xref linkend="vacuum-for-statistics"/> and <xref linkend="autovacuum"/> for more information. For more advice on how to load large amounts of data into <productname>PostgreSQL</productname> efficiently, refer to <xref linkend="populate"/>. バックアップをリストアした後、問い合わせオプティマイザが有用な統計情報を使用できるように、各データベースに対してANALYZEを実行することを勧めます。 より詳しくは、25.1.325.1.6を参照してください。 効率的に大規模なデータをPostgreSQLにロードする方法に関するより多くの勧告については、14.4を参照してください。

26.1.2. pg_dumpallの使用 #

<title>Using <application>pg_dumpall</application></title>

<application>pg_dump</application> dumps only a single database at a time, and it does not dump information about roles or tablespaces (because those are cluster-wide rather than per-database). To support convenient dumping of the entire contents of a database cluster, the <xref linkend="app-pg-dumpall"/> program is provided. <application>pg_dumpall</application> backs up each database in a given cluster, and also preserves cluster-wide data such as role and tablespace definitions. The basic usage of this command is: pg_dumpは一度に単一のデータベースのみをダンプします。 また、ロールやテーブル空間についての情報はダンプしません。 (これらはテーブル毎ではなくクラスタ全体のものだからです。) データベースクラスタの全内容の簡便なダンプをサポートするために、pg_dumpallプログラムが提供されています。 pg_dumpallは指定されたクラスタの各データベースのバックアップを行い、そして、ロールやテーブル空間定義などのクラスタ全体にわたるデータを保存します。 このコマンドの基本的な使用方法は

pg_dumpall > dumpfile

です。 The resulting dump can be restored with <application>psql</application>: ダンプの結果はpsqlでリストアできます。

psql -f dumpfile postgres

(Actually, you can specify any existing database name to start from, but if you are loading into an empty cluster then <literal>postgres</literal> should usually be used.) It is always necessary to have database superuser access when restoring a <application>pg_dumpall</application> dump, as that is required to restore the role and tablespace information. If you use tablespaces, make sure that the tablespace paths in the dump are appropriate for the new installation. (実際、開始時に任意の既存のデータベース名を指定することができますが、空のクラスタ内にロードする場合は、通常 postgres を使用すべきです。) ロールやテーブル空間の情報をリストアしなければならないので、pg_dumpallのダンプをリストアする時には、データベーススーパーユーザのアクセス権限を確実に必要とします。 テーブル空間を使用している場合、ダンプ内のテーブル空間のパスが新しいインストレーションで適切であることを確認してください。

<application>pg_dumpall</application> works by emitting commands to re-create roles, tablespaces, and empty databases, then invoking <application>pg_dump</application> for each database. This means that while each database will be internally consistent, the snapshots of different databases are not synchronized. pg_dumpallはコマンドを発令することによりロール、テーブル空間、およびデータベースを再作成し、それぞれのデータベースに対してpg_dumpを起動します。 このことは、それぞれのデータベースには内部的に矛盾がない一方、異なるデータベースのスナップショットは完全に同期しないことを示しています。

Cluster-wide data can be dumped alone using the <application>pg_dumpall</application> <option>&#45;-globals-only</option> option. This is necessary to fully backup the cluster if running the <application>pg_dump</application> command on individual databases. クラスタレベルでのデータはpg_dumpall--globals-only オプションを使用して出力することができます。 このコマンドは個々のデータベースにpg_dump コマンドを実行しつつ、フルバックアップを取得する際に必要です。

26.1.3. 大規模データベースの扱い #

<title>Handling Large Databases</title>

Some operating systems have maximum file size limits that cause problems when creating large <application>pg_dump</application> output files. Fortunately, <application>pg_dump</application> can write to the standard output, so you can use standard Unix tools to work around this potential problem. There are several possible methods: オペレーティングシステムの中には最大ファイルサイズに制限があるものがあり、大きなpg_dump出力ファイルを作成しているときに問題を引き起こします。 幸運なことに、pg_dumpは標準出力に書き出すことができますので、Unix標準のツールを使ってこの潜在的な問題を解決できます。 取りうる方法がいくつか存在します。

<title>Use compressed dumps.</title> 圧縮ダンプの使用.  You can use your favorite compression program, for example <application>gzip</application>: たとえば、自分が愛用しているgzipのような圧縮プログラムが使えます。

pg_dump dbname | gzip > filename.gz

Reload with: 元に戻すには次のようにします。

gunzip -c filename.gz | psql dbname

or: あるいは次のようにもできます。

cat filename.gz | gunzip | psql dbname

<title>Use <command>split</command>.</title> splitの使用.  The <command>split</command> command allows you to split the output into smaller files that are acceptable in size to the underlying file system. For example, to make 2 gigabyte chunks: splitコマンドで結果を使用しているファイルシステムが受け付けられる大きさに分割することができます。 例えば2メガバイトずつに分割するには次のようにします。

pg_dump dbname | split -b 2G - filename

Reload with: 元に戻すには次のようにします。

cat filename* | psql dbname

If using GNU <application>split</application>, it is possible to use it and <application>gzip</application> together: GNU splitを使用している場合は、次のようにgzipを一緒に使うことでファイルの圧縮が出来ます。

pg_dump dbname | split -b 2G --filter='gzip > $FILE.gz'

It can be restored using <command>zcat</command>. 圧縮されたファイルはzcatを使ってリストア出来ます。

<title>Use <application>pg_dump</application>'s custom dump format.</title> pg_dumpのカスタムダンプ書式の使用.  If <productname>PostgreSQL</productname> was built on a system with the <application>zlib</application> compression library installed, the custom dump format will compress data as it writes it to the output file. This will produce dump file sizes similar to using <command>gzip</command>, but it has the added advantage that tables can be restored selectively. The following command dumps a database using the custom dump format: もしPostgreSQLzlib圧縮ライブラリインストール済みのシステム上で構築されたのなら、カスタムダンプ書式では出力ファイルに書き出す時にデータを圧縮します。 gzipを使用した時と似通ったダンプサイズとなりますが、テーブルの復元を部分的に行えるという点で優れていると言えます。 以下のコマンドは、カスタムダンプ書式でのデータベースのダンプを行います。

pg_dump -Fc dbname > filename

A custom-format dump is not a script for <application>psql</application>, but instead must be restored with <application>pg_restore</application>, for example: カスタム書式のダンプはpsql用のスクリプトではありませんので、代わりにpg_restoreでリストアしなければなりません。 例えば以下のようにします。

pg_restore -d dbname filename

See the <xref linkend="app-pgdump"/> and <xref linkend="app-pgrestore"/> reference pages for details. 詳細はpg_dumppg_restoreのリファレンスページを参照してください。

For very large databases, you might need to combine <command>split</command> with one of the other two approaches. 巨大なデータベースに対しては、そのほかの2つの手法のうちの1つと一緒にsplitを組み合わせる必要があるかもしれません。

<title>Use <application>pg_dump</application>'s parallel dump feature.</title> pg_dumpの並列実行.  To speed up the dump of a large database, you can use <application>pg_dump</application>'s parallel mode. This will dump multiple tables at the same time. You can control the degree of parallelism with the <command>-j</command> parameter. Parallel dumps are only supported for the "directory" archive format. pg_dumpを並列実行することで、大きなデータベースのダンプを高速に実行することができます。 これは同時に複数テーブルのダンプを実行します。 並列度は-jパラメータを指定することで制御できます。 並列ダンプはディレクトリダンプ書式のみサポートします。

pg_dump -j num -F d -f out.dir dbname

You can use <command>pg_restore -j</command> to restore a dump in parallel. This will work for any archive of either the "custom" or the "directory" archive mode, whether or not it has been created with <command>pg_dump -j</command>. pg_restore -jコマンドでダンプファイルを並列でリストアすることができます。 これはpg_dump -jでダンプファイルが作成されたか、否かにかかわらず、カスタムもしくはディレクトリダンプ書式で作成されたダンプファイルに使用できます。