{"kind": "tool", "major": "18", "item": {"slug": "pg-rewind", "name": "pg_rewind", "name_zh": "", "category": "Server applications", "summary": "pg_rewind \u2014 synchronize a PostgreSQL data directory with another data directory that was forked from it", "aliases": ["pg_rewind"], "content_hash": "972142a26f177cc984d85aa8249d432ea7952b400317e35aa322a96423eb951e", "versions": {"10": {"facts": [{"label": "Documented executable", "value": "pg_rewind"}, {"label": "Executable version", "value": "10.23"}, {"label": "Reference inventory", "value": "Server applications"}, {"label": "Option definition groups", "value": "8"}], "tables": [{"key": "options", "rows": [{"summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": {"url": "/docs/10/app-pgrewind.html", "text": "-D directory --target-pgdata= directory"}}, {"summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": {"url": "/docs/10/app-pgrewind.html", "text": "--source-pgdata= directory"}}, {"summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with superuser access. This option requires the source server to be running and not in recovery mode.", "signature": {"url": "/docs/10/app-pgrewind.html", "text": "--source-server= connstr"}}, {"summary": "Do everything except actually modifying the target directory.", "signature": {"url": "/docs/10/app-pgrewind.html", "text": "-n --dry-run"}}, {"summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": {"url": "/docs/10/app-pgrewind.html", "text": "-P --progress"}}, {"summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": {"url": "/docs/10/app-pgrewind.html", "text": "--debug"}}, {"summary": "Display version information, then exit.", "signature": {"url": "/docs/10/app-pgrewind.html", "text": "-V --version"}}, {"summary": "Show help, then exit.", "signature": {"url": "/docs/10/app-pgrewind.html", "text": "-? --help"}}], "title": "Documented options", "columns": [{"key": "signature", "label": "Option and arguments"}, {"key": "summary", "label": "Description"}]}], "options": [{"names": ["-D directory", "--target-pgdata= directory"], "summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": "-D directory --target-pgdata= directory", "source_url": "/docs/10/app-pgrewind.html", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": "--source-pgdata= directory", "source_url": "/docs/10/app-pgrewind.html", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with superuser access. This option requires the source server to be running and not in recovery mode.", "signature": "--source-server= connstr", "source_url": "/docs/10/app-pgrewind.html", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with superuser access. This option requires the source server to be running and not in recovery mode."}, {"names": ["-n", "--dry-run"], "summary": "Do everything except actually modifying the target directory.", "signature": "-n --dry-run", "source_url": "/docs/10/app-pgrewind.html", "description": "Do everything except actually modifying the target directory."}, {"names": ["-P", "--progress"], "summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": "-P --progress", "source_url": "/docs/10/app-pgrewind.html", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["--debug"], "summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": "--debug", "source_url": "/docs/10/app-pgrewind.html", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["-V", "--version"], "summary": "Display version information, then exit.", "signature": "-V --version", "source_url": "/docs/10/app-pgrewind.html", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "summary": "Show help, then exit.", "signature": "-? --help", "source_url": "/docs/10/app-pgrewind.html", "description": "Show help, then exit."}], "related": [], "release": {"ref": "https://ftp.postgresql.org/pub/source/v10.23/postgresql-10.23.tar.bz2", "label": "10.23", "major": "10", "channel": "historical", "manifest": {"pdf": {"A4": {"url": "/files/documentation/pdf/10/postgresql-10-A4.pdf", "bytes": 12631706, "pages": 2591, "sha256": "34497ab9efb45c5bdf1bd11b9016b5451354feb462fa029cf74557a5fa5fbbc1", "built_at": "2026-09-26"}, "US": {"url": "/files/documentation/pdf/10/postgresql-10-US.pdf", "bytes": 12531684, "pages": 2724, "sha256": "429cc7133ddf4f97c560aa466dc9caea4b718721a8321e293357037cf0af4733", "built_at": "2026-09-26"}}, "tree": "10", "index": "index.html", "major": "10", "pages": 1085, "release": "10.23", "source_url": "https://ftp.postgresql.org/pub/source/v10.23/postgresql-10.23.tar.bz2", "svg_assets": 0, "source_mode": "en HTML verified against the pinned official archive", "source_sha256": "94a4b2528372458e5662c18d406629266667c437198160a18cdfd2c4a4d6eee9"}, "revision": "f9301feba91e2e2566038a36b8cee0e4402db68989538f88670fddd20b1f78ea", "evidence_kind": "English manual and source declarations", "source_sha256": "94a4b2528372458e5662c18d406629266667c437198160a18cdfd2c4a4d6eee9"}, "sources": [{"url": "/docs/10/app-pgrewind.html", "file": "app-pgrewind.html", "label": "10.23 English manual \u00b7 app-pgrewind.html", "anchor": "", "sha256": "c4cd0b0186e43941690458e578d637cd6be90805bfe55db4b07ed509c6ff7a28"}, {"url": "/docs/10/reference-server.html", "file": "reference-server.html", "label": "Server applications inventory", "anchor": "", "sha256": "2a6043b88501763a31348758a370a42cd7f9a179c7fdf3ef0e353c82e95b33e9"}], "sections": [], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "signature": "pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }", "description": ["pg_rewind \u2014 synchronize a PostgreSQL data directory with another data directory that was forked from it"], "environment": [], "manual_html": "<div><div class=\"refentry\" id=\"APP-PGREWIND\">\n<div class=\"titlepage\"></div>\n\n<div class=\"refnamediv\">\n<h2><span class=\"refentrytitle\"><span class=\"application\">pg_rewind</span></span></h2>\n<p>pg_rewind \u2014 synchronize a <span class=\"productname\">PostgreSQL</span> data directory with another data directory that was forked from it</p>\n</div>\n<div class=\"refsynopsisdiv\">\n<h2>Synopsis</h2>\n<div class=\"cmdsynopsis\">\n<p id=\"id-1.9.5.8.4.1\"><code class=\"command\">pg_rewind</code> [<em class=\"replaceable\"><code>option</code></em>...] { <code class=\"option\">-D</code> | <code class=\"option\">--target-pgdata</code> } <em class=\"replaceable\"><code>directory</code></em> { <code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code> | <code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code> }</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.8.5\">\n<h2>Description</h2>\n<p><span class=\"application\">pg_rewind</span> is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old master server back online after failover as a standby that follows the new master.</p>\n<p>The result is equivalent to replacing the target data directory with the source one. Only changed blocks from relation files are copied; all other files are copied in full, including configuration files. The advantage of <span class=\"application\">pg_rewind</span> over taking a new base backup, or tools like <span class=\"application\">rsync</span>, is that <span class=\"application\">pg_rewind</span> does not require reading through unchanged blocks in the cluster. This makes it a lot faster when the database is large and only a small fraction of blocks differ between the clusters.</p>\n<p><span class=\"application\">pg_rewind</span> examines the timeline histories of the source and target clusters to determine the point where they diverged, and expects to find WAL in the target cluster's <code class=\"filename\">pg_wal</code> directory reaching all the way back to the point of divergence. The point of divergence can be found either on the target timeline, the source timeline, or their common ancestor. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem, but if the target cluster ran for a long time after the divergence, the old WAL files might no longer be present. In that case, they can be manually copied from the WAL archive to the <code class=\"filename\">pg_wal</code> directory. The use of <span class=\"application\">pg_rewind</span> is not limited to failover, e.g., a standby server can be promoted, run some write transactions, and then rewound to become a standby again.</p>\n<p>When the target server is started for the first time after running <span class=\"application\">pg_rewind</span>, it will go into recovery mode and replay all WAL generated in the source server after the point of divergence. If some of the WAL was no longer available in the source server when <span class=\"application\">pg_rewind</span> was run, and therefore could not be copied by the <span class=\"application\">pg_rewind</span> session, it must be made available when the target server is started. This can be done by creating a <code class=\"filename\">recovery.conf</code> file in the target data directory with a suitable <code class=\"varname\">restore_command</code>.</p>\n<p><span class=\"application\">pg_rewind</span> requires that the target server either has the <a class=\"xref\" href=\"/docs/10/runtime-config-wal.html#GUC-WAL-LOG-HINTS\">wal_log_hints</a> option enabled in <code class=\"filename\">postgresql.conf</code> or data checksums enabled when the cluster was initialized with <span class=\"application\">initdb</span>. Neither of these are currently on by default. <a class=\"xref\" href=\"/docs/10/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\">full_page_writes</a> must also be set to <code class=\"literal\">on</code>, but is enabled by default.</p>\n<div class=\"warning\">\n<h3 class=\"title\">Warning</h3>\n<p>If <span class=\"application\">pg_rewind</span> fails while processing, then the data folder of the target is likely not in a state that can be recovered. In such a case, taking a new fresh backup is recommended.</p>\n<p><span class=\"application\">pg_rewind</span> will fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates. If such files are present on the target server it is recommended to remove them before running <span class=\"application\">pg_rewind</span>. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind.</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.8.6\">\n<h2>Options</h2>\n<p><span class=\"application\">pg_rewind</span> accepts the following command-line arguments:</p>\n<div class=\"variablelist\">\n<dl class=\"variablelist\">\n<dt><span class=\"term\"><code class=\"option\">-D <em class=\"replaceable\"><code>directory</code></em></code><br></span><span class=\"term\"><code class=\"option\">--target-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running <span class=\"application\">pg_rewind</span></p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code></span></dt>\n<dd>\n<p>Specifies a libpq connection string to connect to the source <span class=\"productname\">PostgreSQL</span> server to synchronize the target with. The connection must be a normal (non-replication) connection with superuser access. This option requires the source server to be running and not in recovery mode.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-n</code><br></span><span class=\"term\"><code class=\"option\">--dry-run</code></span></dt>\n<dd>\n<p>Do everything except actually modifying the target directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-P</code><br></span><span class=\"term\"><code class=\"option\">--progress</code></span></dt>\n<dd>\n<p>Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--debug</code></span></dt>\n<dd>\n<p>Print verbose debugging output that is mostly useful for developers debugging <span class=\"application\">pg_rewind</span>.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-V</code><br></span><span class=\"term\"><code class=\"option\">--version</code></span></dt>\n<dd>\n<p>Display version information, then exit.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-?</code><br></span><span class=\"term\"><code class=\"option\">--help</code></span></dt>\n<dd>\n<p>Show help, then exit.</p>\n</dd>\n</dl>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.8.7\">\n<h2>Environment</h2>\n<p>When <code class=\"option\">--source-server</code> option is used, <span class=\"application\">pg_rewind</span> also uses the environment variables supported by <span class=\"application\">libpq</span> (see <a class=\"xref\" href=\"/docs/10/libpq-envars.html\" title=\"33.14.\u00a0Environment Variables\">Section\u00a033.14</a>).</p>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.8.8\">\n<h2>Notes</h2>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source which has been recently promoted, it is necessary to execute a <code class=\"command\">CHECKPOINT</code> after promotion so as its control file reflects up-to-date timeline information, which is used by <span class=\"application\">pg_rewind</span> to check if the target cluster can be rewound using the designated source cluster.</p>\n<div class=\"refsect2\" id=\"id-1.9.5.8.8.3\">\n<h3>How it works</h3>\n<p>The basic idea is to copy all file system-level changes from the source cluster to the target cluster:</p>\n<div class=\"procedure\">\n<ol class=\"procedure\">\n<li class=\"step\">\n<p>Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's timeline history forked off from the target cluster. For each WAL record, record each data block that was touched. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off.</p>\n</li>\n<li class=\"step\">\n<p>Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (<code class=\"option\">--source-pgdata</code>) or SQL (<code class=\"option\">--source-server</code>).</p>\n</li>\n<li class=\"step\">\n<p>Copy all other files such as <code class=\"filename\">pg_xact</code> and configuration files from the source cluster to the target cluster (everything except the relation files).</p>\n</li>\n<li class=\"step\">\n<p>Apply the WAL from the source cluster, starting from the checkpoint created at failover. (Strictly speaking, <span class=\"application\">pg_rewind</span> doesn't apply the WAL, it just creates a backup label file that makes <span class=\"productname\">PostgreSQL</span> start by replaying all WAL from that checkpoint forward.)</p>\n</li>\n</ol>\n</div>\n</div>\n</div>\n</div></div>", "manual_path": "app-pgrewind.html", "comparison_data": {"options": [{"names": ["-D directory", "--target-pgdata= directory"], "signature": "-D directory --target-pgdata= directory", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "signature": "--source-pgdata= directory", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "signature": "--source-server= connstr", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with superuser access. This option requires the source server to be running and not in recovery mode."}, {"names": ["-n", "--dry-run"], "signature": "-n --dry-run", "description": "Do everything except actually modifying the target directory."}, {"names": ["-P", "--progress"], "signature": "-P --progress", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["--debug"], "signature": "--debug", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["-V", "--version"], "signature": "-V --version", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "signature": "-? --help", "description": "Show help, then exit."}], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "environment": []}, "comparison_hash": "96b5183f2b390e61e852c65532a9411aa43692006bddfabbd5856dd42b08e8f4"}, "11": {"facts": [{"label": "Documented executable", "value": "pg_rewind"}, {"label": "Executable version", "value": "11.22"}, {"label": "Reference inventory", "value": "Server applications"}, {"label": "Option definition groups", "value": "8"}], "tables": [{"key": "options", "rows": [{"summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": {"url": "/docs/11/app-pgrewind.html", "text": "-D directory --target-pgdata= directory"}}, {"summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": {"url": "/docs/11/app-pgrewind.html", "text": "--source-pgdata= directory"}}, {"summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode.", "signature": {"url": "/docs/11/app-pgrewind.html", "text": "--source-server= connstr"}}, {"summary": "Do everything except actually modifying the target directory.", "signature": {"url": "/docs/11/app-pgrewind.html", "text": "-n --dry-run"}}, {"summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": {"url": "/docs/11/app-pgrewind.html", "text": "-P --progress"}}, {"summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": {"url": "/docs/11/app-pgrewind.html", "text": "--debug"}}, {"summary": "Display version information, then exit.", "signature": {"url": "/docs/11/app-pgrewind.html", "text": "-V --version"}}, {"summary": "Show help, then exit.", "signature": {"url": "/docs/11/app-pgrewind.html", "text": "-? --help"}}], "title": "Documented options", "columns": [{"key": "signature", "label": "Option and arguments"}, {"key": "summary", "label": "Description"}]}], "options": [{"names": ["-D directory", "--target-pgdata= directory"], "summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": "-D directory --target-pgdata= directory", "source_url": "/docs/11/app-pgrewind.html", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": "--source-pgdata= directory", "source_url": "/docs/11/app-pgrewind.html", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode.", "signature": "--source-server= connstr", "source_url": "/docs/11/app-pgrewind.html", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode."}, {"names": ["-n", "--dry-run"], "summary": "Do everything except actually modifying the target directory.", "signature": "-n --dry-run", "source_url": "/docs/11/app-pgrewind.html", "description": "Do everything except actually modifying the target directory."}, {"names": ["-P", "--progress"], "summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": "-P --progress", "source_url": "/docs/11/app-pgrewind.html", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["--debug"], "summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": "--debug", "source_url": "/docs/11/app-pgrewind.html", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["-V", "--version"], "summary": "Display version information, then exit.", "signature": "-V --version", "source_url": "/docs/11/app-pgrewind.html", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "summary": "Show help, then exit.", "signature": "-? --help", "source_url": "/docs/11/app-pgrewind.html", "description": "Show help, then exit."}], "related": [], "release": {"ref": "https://ftp.postgresql.org/pub/source/v11.22/postgresql-11.22.tar.bz2", "label": "11.22", "major": "11", "channel": "historical", "manifest": {"pdf": {"A4": {"url": "/files/documentation/pdf/11/postgresql-11-A4.pdf", "bytes": 13057499, "pages": 2732, "sha256": "41d75855e610d0d9b8802b87dc5b080bef8d7e91c7cf69401fbf8d3cf801b4b5", "built_at": "2026-09-26"}, "US": {"url": "/files/documentation/pdf/11/postgresql-11-US.pdf", "bytes": 12961189, "pages": 2883, "sha256": "6a8899ef36935a5b7ed2b436207564a567a4ef1eb1c2200195876b8de4d4fa30", "built_at": "2026-09-26"}}, "tree": "11", "index": "index.html", "major": "11", "pages": 1125, "release": "11.22", "source_url": "https://ftp.postgresql.org/pub/source/v11.22/postgresql-11.22.tar.bz2", "svg_assets": 0, "source_mode": "en HTML verified against the pinned official archive", "source_sha256": "2cb7c97d7a0d7278851bbc9c61f467b69c094c72b81740b751108e7892ebe1f0"}, "revision": "53315ddaf3b3f9e6669fd1edc096bbb2cd4a65ea3e836c89e542b43af9ba3a7f", "evidence_kind": "English manual and source declarations", "source_sha256": "2cb7c97d7a0d7278851bbc9c61f467b69c094c72b81740b751108e7892ebe1f0"}, "sources": [{"url": "/docs/11/app-pgrewind.html", "file": "app-pgrewind.html", "label": "11.22 English manual \u00b7 app-pgrewind.html", "anchor": "", "sha256": "59e6b61d8848111c710f8973305e3a2b04482bc683f1196d1b91c3bc12671f60"}, {"url": "/docs/11/reference-server.html", "file": "reference-server.html", "label": "Server applications inventory", "anchor": "", "sha256": "e940a8505b6742c717a1683d19bf8f759aad245633a59db9e83a345ae7c5709a"}], "sections": [], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "signature": "pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }", "description": ["pg_rewind \u2014 synchronize a PostgreSQL data directory with another data directory that was forked from it"], "environment": [], "manual_html": "<div><div class=\"refentry\" id=\"APP-PGREWIND\">\n<div class=\"titlepage\"></div>\n<div class=\"refnamediv\">\n<h2><span class=\"refentrytitle\"><span class=\"application\">pg_rewind</span></span></h2>\n<p>pg_rewind \u2014 synchronize a <span class=\"productname\">PostgreSQL</span> data directory with another data directory that was forked from it</p>\n</div>\n<div class=\"refsynopsisdiv\">\n<h2>Synopsis</h2>\n<div class=\"cmdsynopsis\">\n<p id=\"id-1.9.5.8.4.1\"><code class=\"command\">pg_rewind</code> [<em class=\"replaceable\"><code>option</code></em>...] { <code class=\"option\">-D</code> | <code class=\"option\">--target-pgdata</code> } <em class=\"replaceable\"><code>directory</code></em> { <code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code> | <code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code> }</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.8.5\">\n<h2>Description</h2>\n<p><span class=\"application\">pg_rewind</span> is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old master server back online after failover as a standby that follows the new master.</p>\n<p>The result is equivalent to replacing the target data directory with the source one. Only changed blocks from relation files are copied; all other files are copied in full, including configuration files. The advantage of <span class=\"application\">pg_rewind</span> over taking a new base backup, or tools like <span class=\"application\">rsync</span>, is that <span class=\"application\">pg_rewind</span> does not require reading through unchanged blocks in the cluster. This makes it a lot faster when the database is large and only a small fraction of blocks differ between the clusters.</p>\n<p><span class=\"application\">pg_rewind</span> examines the timeline histories of the source and target clusters to determine the point where they diverged, and expects to find WAL in the target cluster's <code class=\"filename\">pg_wal</code> directory reaching all the way back to the point of divergence. The point of divergence can be found either on the target timeline, the source timeline, or their common ancestor. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem, but if the target cluster ran for a long time after the divergence, the old WAL files might no longer be present. In that case, they can be manually copied from the WAL archive to the <code class=\"filename\">pg_wal</code> directory. The use of <span class=\"application\">pg_rewind</span> is not limited to failover, e.g., a standby server can be promoted, run some write transactions, and then rewound to become a standby again.</p>\n<p>When the target server is started for the first time after running <span class=\"application\">pg_rewind</span>, it will go into recovery mode and replay all WAL generated in the source server after the point of divergence. If some of the WAL was no longer available in the source server when <span class=\"application\">pg_rewind</span> was run, and therefore could not be copied by the <span class=\"application\">pg_rewind</span> session, it must be made available when the target server is started. This can be done by creating a <code class=\"filename\">recovery.conf</code> file in the target data directory with a suitable <code class=\"varname\">restore_command</code>.</p>\n<p><span class=\"application\">pg_rewind</span> requires that the target server either has the <a class=\"xref\" href=\"/docs/11/runtime-config-wal.html#GUC-WAL-LOG-HINTS\">wal_log_hints</a> option enabled in <code class=\"filename\">postgresql.conf</code> or data checksums enabled when the cluster was initialized with <span class=\"application\">initdb</span>. Neither of these are currently on by default. <a class=\"xref\" href=\"/docs/11/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\">full_page_writes</a> must also be set to <code class=\"literal\">on</code>, but is enabled by default.</p>\n<div class=\"warning\">\n<h3 class=\"title\">Warning</h3>\n<p>If <span class=\"application\">pg_rewind</span> fails while processing, then the data folder of the target is likely not in a state that can be recovered. In such a case, taking a new fresh backup is recommended.</p>\n<p><span class=\"application\">pg_rewind</span> will fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates. If such files are present on the target server it is recommended to remove them before running <span class=\"application\">pg_rewind</span>. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind.</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.8.6\">\n<h2>Options</h2>\n<p><span class=\"application\">pg_rewind</span> accepts the following command-line arguments:</p>\n<div class=\"variablelist\">\n<dl class=\"variablelist\">\n<dt><span class=\"term\"><code class=\"option\">-D <em class=\"replaceable\"><code>directory</code></em></code><br></span><span class=\"term\"><code class=\"option\">--target-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running <span class=\"application\">pg_rewind</span></p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code></span></dt>\n<dd>\n<p>Specifies a libpq connection string to connect to the source <span class=\"productname\">PostgreSQL</span> server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-n</code><br></span><span class=\"term\"><code class=\"option\">--dry-run</code></span></dt>\n<dd>\n<p>Do everything except actually modifying the target directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-P</code><br></span><span class=\"term\"><code class=\"option\">--progress</code></span></dt>\n<dd>\n<p>Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--debug</code></span></dt>\n<dd>\n<p>Print verbose debugging output that is mostly useful for developers debugging <span class=\"application\">pg_rewind</span>.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-V</code><br></span><span class=\"term\"><code class=\"option\">--version</code></span></dt>\n<dd>\n<p>Display version information, then exit.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-?</code><br></span><span class=\"term\"><code class=\"option\">--help</code></span></dt>\n<dd>\n<p>Show help, then exit.</p>\n</dd>\n</dl>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.8.7\">\n<h2>Environment</h2>\n<p>When <code class=\"option\">--source-server</code> option is used, <span class=\"application\">pg_rewind</span> also uses the environment variables supported by <span class=\"application\">libpq</span> (see <a class=\"xref\" href=\"/docs/11/libpq-envars.html\" title=\"34.14.\u00a0Environment Variables\">Section\u00a034.14</a>).</p>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.8.8\">\n<h2>Notes</h2>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source, a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source cluster can be used instead of a superuser. Here is how to create such a role, named <code class=\"literal\">rewind_user</code> here:</p>\n<pre class=\"programlisting\">CREATE USER rewind_user LOGIN;\nGRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;\n</pre>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source which has been recently promoted, it is necessary to execute a <code class=\"command\">CHECKPOINT</code> after promotion so as its control file reflects up-to-date timeline information, which is used by <span class=\"application\">pg_rewind</span> to check if the target cluster can be rewound using the designated source cluster.</p>\n<div class=\"refsect2\" id=\"id-1.9.5.8.8.4\">\n<h3>How it works</h3>\n<p>The basic idea is to copy all file system-level changes from the source cluster to the target cluster:</p>\n<div class=\"procedure\">\n<ol class=\"procedure\">\n<li class=\"step\">\n<p>Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's timeline history forked off from the target cluster. For each WAL record, record each data block that was touched. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off.</p>\n</li>\n<li class=\"step\">\n<p>Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (<code class=\"option\">--source-pgdata</code>) or SQL (<code class=\"option\">--source-server</code>).</p>\n</li>\n<li class=\"step\">\n<p>Copy all other files such as <code class=\"filename\">pg_xact</code> and configuration files from the source cluster to the target cluster (everything except the relation files). Similarly to base backups, the contents of the directories <code class=\"filename\">pg_dynshmem/</code>, <code class=\"filename\">pg_notify/</code>, <code class=\"filename\">pg_replslot/</code>, <code class=\"filename\">pg_serial/</code>, <code class=\"filename\">pg_snapshots/</code>, <code class=\"filename\">pg_stat_tmp/</code>, and <code class=\"filename\">pg_subtrans/</code> are omitted from the data copied from the source cluster. Any file or directory beginning with <code class=\"filename\">pgsql_tmp</code> is omitted, as well as are <code class=\"filename\">backup_label</code>, <code class=\"filename\">tablespace_map</code>, <code class=\"filename\">pg_internal.init</code>, <code class=\"filename\">postmaster.opts</code> and <code class=\"filename\">postmaster.pid</code>.</p>\n</li>\n<li class=\"step\">\n<p>Apply the WAL from the source cluster, starting from the checkpoint created at failover. (Strictly speaking, <span class=\"application\">pg_rewind</span> doesn't apply the WAL, it just creates a backup label file that makes <span class=\"productname\">PostgreSQL</span> start by replaying all WAL from that checkpoint forward.)</p>\n</li>\n</ol>\n</div>\n</div>\n</div>\n</div></div>", "manual_path": "app-pgrewind.html", "comparison_data": {"options": [{"names": ["-D directory", "--target-pgdata= directory"], "signature": "-D directory --target-pgdata= directory", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "signature": "--source-pgdata= directory", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "signature": "--source-server= connstr", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode."}, {"names": ["-n", "--dry-run"], "signature": "-n --dry-run", "description": "Do everything except actually modifying the target directory."}, {"names": ["-P", "--progress"], "signature": "-P --progress", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["--debug"], "signature": "--debug", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["-V", "--version"], "signature": "-V --version", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "signature": "-? --help", "description": "Show help, then exit."}], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "environment": []}, "comparison_hash": "092da9dcb834c249ec28f1d17f7622dbc7c0cd824f86be2b0853485eeeab35ea"}, "12": {"facts": [{"label": "Documented executable", "value": "pg_rewind"}, {"label": "Executable version", "value": "12.22"}, {"label": "Reference inventory", "value": "Server applications"}, {"label": "Option definition groups", "value": "9"}], "tables": [{"key": "options", "rows": [{"summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": {"url": "/docs/12/app-pgrewind.html", "text": "-D directory --target-pgdata= directory"}}, {"summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": {"url": "/docs/12/app-pgrewind.html", "text": "--source-pgdata= directory"}}, {"summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode.", "signature": {"url": "/docs/12/app-pgrewind.html", "text": "--source-server= connstr"}}, {"summary": "Do everything except actually modifying the target directory.", "signature": {"url": "/docs/12/app-pgrewind.html", "text": "-n --dry-run"}}, {"summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used when creating a production installation.", "signature": {"url": "/docs/12/app-pgrewind.html", "text": "-N --no-sync"}}, {"summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": {"url": "/docs/12/app-pgrewind.html", "text": "-P --progress"}}, {"summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": {"url": "/docs/12/app-pgrewind.html", "text": "--debug"}}, {"summary": "Display version information, then exit.", "signature": {"url": "/docs/12/app-pgrewind.html", "text": "-V --version"}}, {"summary": "Show help, then exit.", "signature": {"url": "/docs/12/app-pgrewind.html", "text": "-? --help"}}], "title": "Documented options", "columns": [{"key": "signature", "label": "Option and arguments"}, {"key": "summary", "label": "Description"}]}], "options": [{"names": ["-D directory", "--target-pgdata= directory"], "summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": "-D directory --target-pgdata= directory", "source_url": "/docs/12/app-pgrewind.html", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": "--source-pgdata= directory", "source_url": "/docs/12/app-pgrewind.html", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode.", "signature": "--source-server= connstr", "source_url": "/docs/12/app-pgrewind.html", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode."}, {"names": ["-n", "--dry-run"], "summary": "Do everything except actually modifying the target directory.", "signature": "-n --dry-run", "source_url": "/docs/12/app-pgrewind.html", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used when creating a production installation.", "signature": "-N --no-sync", "source_url": "/docs/12/app-pgrewind.html", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used when creating a production installation."}, {"names": ["-P", "--progress"], "summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": "-P --progress", "source_url": "/docs/12/app-pgrewind.html", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["--debug"], "summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": "--debug", "source_url": "/docs/12/app-pgrewind.html", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["-V", "--version"], "summary": "Display version information, then exit.", "signature": "-V --version", "source_url": "/docs/12/app-pgrewind.html", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "summary": "Show help, then exit.", "signature": "-? --help", "source_url": "/docs/12/app-pgrewind.html", "description": "Show help, then exit."}], "related": [], "release": {"ref": "https://ftp.postgresql.org/pub/source/v12.22/postgresql-12.22.tar.bz2", "label": "12.22", "major": "12", "channel": "historical", "manifest": {"pdf": {"A4": {"url": "/files/documentation/pdf/12/postgresql-12-A4.pdf", "bytes": 13424351, "pages": 2803, "sha256": "7422cf53fd1939e7a3d2925231a88b0330e468afa5393627cac0ff86158a0621", "built_at": "2026-09-26"}, "US": {"url": "/files/documentation/pdf/12/postgresql-12-US.pdf", "bytes": 13322565, "pages": 2958, "sha256": "51f3b04e72907fc38512685946223452ea65f84419c60c66241dfa3396035e77", "built_at": "2026-09-26"}}, "tree": "12", "index": "index.html", "major": "12", "pages": 1131, "release": "12.22", "source_url": "https://ftp.postgresql.org/pub/source/v12.22/postgresql-12.22.tar.bz2", "svg_assets": 2, "source_mode": "en HTML verified against the pinned official archive", "source_sha256": "8df3c0474782589d3c6f374b5133b1bd14d168086edbc13c6e72e67dd4527a3b"}, "revision": "7a827e97cbfacd7febff75f34443e2043e40723de5b6d8c9d81a9430b65a37e4", "evidence_kind": "English manual and source declarations", "source_sha256": "8df3c0474782589d3c6f374b5133b1bd14d168086edbc13c6e72e67dd4527a3b"}, "sources": [{"url": "/docs/12/app-pgrewind.html", "file": "app-pgrewind.html", "label": "12.22 English manual \u00b7 app-pgrewind.html", "anchor": "", "sha256": "9c37a508ca2d478d748aebf470365424f39abcc001aee5dfb3bacd16edab5719"}, {"url": "/docs/12/reference-server.html", "file": "reference-server.html", "label": "Server applications inventory", "anchor": "", "sha256": "7f6ae1fadc1b0bffa606551460e62aacdc2508a3fb66566ddae861928ca6080b"}], "sections": [], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "signature": "pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }", "description": ["pg_rewind \u2014 synchronize a PostgreSQL data directory with another data directory that was forked from it"], "environment": [], "manual_html": "<div><div class=\"refentry\" id=\"APP-PGREWIND\">\n<div class=\"titlepage\"></div>\n<div class=\"refnamediv\">\n<h2><span class=\"refentrytitle\"><span class=\"application\">pg_rewind</span></span></h2>\n<p>pg_rewind \u2014 synchronize a <span class=\"productname\">PostgreSQL</span> data directory with another data directory that was forked from it</p>\n</div>\n<div class=\"refsynopsisdiv\">\n<h2>Synopsis</h2>\n<div class=\"cmdsynopsis\">\n<p id=\"id-1.9.5.9.4.1\"><code class=\"command\">pg_rewind</code> [<em class=\"replaceable\"><code>option</code></em>...] { <code class=\"option\">-D</code> | <code class=\"option\">--target-pgdata</code> } <em class=\"replaceable\"><code>directory</code></em> { <code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code> | <code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code> }</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.5\">\n<h2>Description</h2>\n<p><span class=\"application\">pg_rewind</span> is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old master server back online after failover as a standby that follows the new master.</p>\n<p>The result is equivalent to replacing the target data directory with the source one. Only changed blocks from relation files are copied; all other files are copied in full, including configuration files. The advantage of <span class=\"application\">pg_rewind</span> over taking a new base backup, or tools like <span class=\"application\">rsync</span>, is that <span class=\"application\">pg_rewind</span> does not require reading through unchanged blocks in the cluster. This makes it a lot faster when the database is large and only a small fraction of blocks differ between the clusters.</p>\n<p><span class=\"application\">pg_rewind</span> examines the timeline histories of the source and target clusters to determine the point where they diverged, and expects to find WAL in the target cluster's <code class=\"filename\">pg_wal</code> directory reaching all the way back to the point of divergence. The point of divergence can be found either on the target timeline, the source timeline, or their common ancestor. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem, but if the target cluster ran for a long time after the divergence, the old WAL files might no longer be present. In that case, they can be manually copied from the WAL archive to the <code class=\"filename\">pg_wal</code> directory. The use of <span class=\"application\">pg_rewind</span> is not limited to failover, e.g., a standby server can be promoted, run some write transactions, and then rewound to become a standby again.</p>\n<p>When the target server is started for the first time after running <span class=\"application\">pg_rewind</span>, it will go into recovery mode and replay all WAL generated in the source server after the point of divergence. If some of the WAL was no longer available in the source server when <span class=\"application\">pg_rewind</span> was run, and therefore could not be copied by the <span class=\"application\">pg_rewind</span> session, it must be made available when the target server is started. This can be done by creating a <code class=\"filename\">recovery.signal</code> file in the target data directory and configuring suitable <a class=\"xref\" href=\"/docs/12/runtime-config-wal.html#GUC-RESTORE-COMMAND\">restore_command</a> in <code class=\"filename\">postgresql.conf</code>.</p>\n<p><span class=\"application\">pg_rewind</span> requires that the target server either has the <a class=\"xref\" href=\"/docs/12/runtime-config-wal.html#GUC-WAL-LOG-HINTS\">wal_log_hints</a> option enabled in <code class=\"filename\">postgresql.conf</code> or data checksums enabled when the cluster was initialized with <span class=\"application\">initdb</span>. Neither of these are currently on by default. <a class=\"xref\" href=\"/docs/12/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\">full_page_writes</a> must also be set to <code class=\"literal\">on</code>, but is enabled by default.</p>\n<div class=\"warning\">\n<h3 class=\"title\">Warning</h3>\n<p>If <span class=\"application\">pg_rewind</span> fails while processing, then the data folder of the target is likely not in a state that can be recovered. In such a case, taking a new fresh backup is recommended.</p>\n<p><span class=\"application\">pg_rewind</span> will fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates. If such files are present on the target server it is recommended to remove them before running <span class=\"application\">pg_rewind</span>. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind.</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.6\">\n<h2>Options</h2>\n<p><span class=\"application\">pg_rewind</span> accepts the following command-line arguments:</p>\n<div class=\"variablelist\">\n<dl class=\"variablelist\">\n<dt><span class=\"term\"><code class=\"option\">-D <em class=\"replaceable\"><code>directory</code></em></code><br></span><span class=\"term\"><code class=\"option\">--target-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running <span class=\"application\">pg_rewind</span></p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code></span></dt>\n<dd>\n<p>Specifies a libpq connection string to connect to the source <span class=\"productname\">PostgreSQL</span> server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-n</code><br></span><span class=\"term\"><code class=\"option\">--dry-run</code></span></dt>\n<dd>\n<p>Do everything except actually modifying the target directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-N</code><br></span><span class=\"term\"><code class=\"option\">--no-sync</code></span></dt>\n<dd>\n<p>By default, <code class=\"command\">pg_rewind</code> will wait for all files to be written safely to disk. This option causes <code class=\"command\">pg_rewind</code> to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used when creating a production installation.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-P</code><br></span><span class=\"term\"><code class=\"option\">--progress</code></span></dt>\n<dd>\n<p>Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--debug</code></span></dt>\n<dd>\n<p>Print verbose debugging output that is mostly useful for developers debugging <span class=\"application\">pg_rewind</span>.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-V</code><br></span><span class=\"term\"><code class=\"option\">--version</code></span></dt>\n<dd>\n<p>Display version information, then exit.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-?</code><br></span><span class=\"term\"><code class=\"option\">--help</code></span></dt>\n<dd>\n<p>Show help, then exit.</p>\n</dd>\n</dl>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.7\">\n<h2>Environment</h2>\n<p>When <code class=\"option\">--source-server</code> option is used, <span class=\"application\">pg_rewind</span> also uses the environment variables supported by <span class=\"application\">libpq</span> (see <a class=\"xref\" href=\"/docs/12/libpq-envars.html\" title=\"33.14.\u00a0Environment Variables\">Section\u00a033.14</a>).</p>\n<p>The environment variable <code class=\"envar\">PG_COLOR</code> specifies whether to use color in diagnostic messages. Possible values are <code class=\"literal\">always</code>, <code class=\"literal\">auto</code> and <code class=\"literal\">never</code>.</p>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.8\">\n<h2>Notes</h2>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source, a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source cluster can be used instead of a superuser. Here is how to create such a role, named <code class=\"literal\">rewind_user</code> here:</p>\n<pre class=\"programlisting\">CREATE USER rewind_user LOGIN;\nGRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;\n</pre>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source which has been recently promoted, it is necessary to execute a <code class=\"command\">CHECKPOINT</code> after promotion so as its control file reflects up-to-date timeline information, which is used by <span class=\"application\">pg_rewind</span> to check if the target cluster can be rewound using the designated source cluster.</p>\n<div class=\"refsect2\" id=\"id-1.9.5.9.8.4\">\n<h3>How It Works</h3>\n<p>The basic idea is to copy all file system-level changes from the source cluster to the target cluster:</p>\n<div class=\"procedure\">\n<ol class=\"procedure\">\n<li class=\"step\">\n<p>Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's timeline history forked off from the target cluster. For each WAL record, record each data block that was touched. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off.</p>\n</li>\n<li class=\"step\">\n<p>Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (<code class=\"option\">--source-pgdata</code>) or SQL (<code class=\"option\">--source-server</code>).</p>\n</li>\n<li class=\"step\">\n<p>Copy all other files such as <code class=\"filename\">pg_xact</code> and configuration files from the source cluster to the target cluster (everything except the relation files). Similarly to base backups, the contents of the directories <code class=\"filename\">pg_dynshmem/</code>, <code class=\"filename\">pg_notify/</code>, <code class=\"filename\">pg_replslot/</code>, <code class=\"filename\">pg_serial/</code>, <code class=\"filename\">pg_snapshots/</code>, <code class=\"filename\">pg_stat_tmp/</code>, and <code class=\"filename\">pg_subtrans/</code> are omitted from the data copied from the source cluster. Any file or directory beginning with <code class=\"filename\">pgsql_tmp</code> is omitted, as well as are <code class=\"filename\">backup_label</code>, <code class=\"filename\">tablespace_map</code>, <code class=\"filename\">pg_internal.init</code>, <code class=\"filename\">postmaster.opts</code> and <code class=\"filename\">postmaster.pid</code>.</p>\n</li>\n<li class=\"step\">\n<p>Apply the WAL from the source cluster, starting from the checkpoint created at failover. (Strictly speaking, <span class=\"application\">pg_rewind</span> doesn't apply the WAL, it just creates a backup label file that makes <span class=\"productname\">PostgreSQL</span> start by replaying all WAL from that checkpoint forward.)</p>\n</li>\n</ol>\n</div>\n</div>\n</div>\n</div></div>", "manual_path": "app-pgrewind.html", "comparison_data": {"options": [{"names": ["-D directory", "--target-pgdata= directory"], "signature": "-D directory --target-pgdata= directory", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "signature": "--source-pgdata= directory", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "signature": "--source-server= connstr", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode."}, {"names": ["-n", "--dry-run"], "signature": "-n --dry-run", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "signature": "-N --no-sync", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used when creating a production installation."}, {"names": ["-P", "--progress"], "signature": "-P --progress", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["--debug"], "signature": "--debug", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["-V", "--version"], "signature": "-V --version", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "signature": "-? --help", "description": "Show help, then exit."}], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "environment": []}, "comparison_hash": "9d1017a212b05e2ae1aa412c9332ef1ba556d6c390ebdd79bd23343b584004fc"}, "13": {"facts": [{"label": "Documented executable", "value": "pg_rewind"}, {"label": "Executable version", "value": "13.23"}, {"label": "Reference inventory", "value": "Server applications"}, {"label": "Option definition groups", "value": "12"}], "tables": [{"key": "options", "rows": [{"summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": {"url": "/docs/13/app-pgrewind.html", "text": "-D directory --target-pgdata= directory"}}, {"summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": {"url": "/docs/13/app-pgrewind.html", "text": "--source-pgdata= directory"}}, {"summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode.", "signature": {"url": "/docs/13/app-pgrewind.html", "text": "--source-server= connstr"}}, {"summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option.", "signature": {"url": "/docs/13/app-pgrewind.html", "text": "-R --write-recovery-conf"}}, {"summary": "Do everything except actually modifying the target directory.", "signature": {"url": "/docs/13/app-pgrewind.html", "text": "-n --dry-run"}}, {"summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": {"url": "/docs/13/app-pgrewind.html", "text": "-N --no-sync"}}, {"summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": {"url": "/docs/13/app-pgrewind.html", "text": "-P --progress"}}, {"summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": {"url": "/docs/13/app-pgrewind.html", "text": "-c --restore-target-wal"}}, {"summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": {"url": "/docs/13/app-pgrewind.html", "text": "--debug"}}, {"summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": {"url": "/docs/13/app-pgrewind.html", "text": "--no-ensure-shutdown"}}, {"summary": "Display version information, then exit.", "signature": {"url": "/docs/13/app-pgrewind.html", "text": "-V --version"}}, {"summary": "Show help, then exit.", "signature": {"url": "/docs/13/app-pgrewind.html", "text": "-? --help"}}], "title": "Documented options", "columns": [{"key": "signature", "label": "Option and arguments"}, {"key": "summary", "label": "Description"}]}], "options": [{"names": ["-D directory", "--target-pgdata= directory"], "summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": "-D directory --target-pgdata= directory", "source_url": "/docs/13/app-pgrewind.html", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": "--source-pgdata= directory", "source_url": "/docs/13/app-pgrewind.html", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode.", "signature": "--source-server= connstr", "source_url": "/docs/13/app-pgrewind.html", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode."}, {"names": ["-R", "--write-recovery-conf"], "summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option.", "signature": "-R --write-recovery-conf", "source_url": "/docs/13/app-pgrewind.html", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "summary": "Do everything except actually modifying the target directory.", "signature": "-n --dry-run", "source_url": "/docs/13/app-pgrewind.html", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": "-N --no-sync", "source_url": "/docs/13/app-pgrewind.html", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": "-P --progress", "source_url": "/docs/13/app-pgrewind.html", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": "-c --restore-target-wal", "source_url": "/docs/13/app-pgrewind.html", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--debug"], "summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": "--debug", "source_url": "/docs/13/app-pgrewind.html", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": "--no-ensure-shutdown", "source_url": "/docs/13/app-pgrewind.html", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["-V", "--version"], "summary": "Display version information, then exit.", "signature": "-V --version", "source_url": "/docs/13/app-pgrewind.html", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "summary": "Show help, then exit.", "signature": "-? --help", "source_url": "/docs/13/app-pgrewind.html", "description": "Show help, then exit."}], "related": [], "release": {"ref": "https://ftp.postgresql.org/pub/source/v13.23/postgresql-13.23.tar.bz2", "label": "13.23", "major": "13", "channel": "historical", "manifest": {"pdf": {"A4": {"url": "/files/documentation/pdf/13/postgresql-13-A4.pdf", "bytes": 13843239, "pages": 2826, "sha256": "171cc09f90936dbc1cbd503a98ff07ae72ea58ab9771ff051313f1217737799c", "built_at": "2026-09-26"}, "US": {"url": "/files/documentation/pdf/13/postgresql-13-US.pdf", "bytes": 13739572, "pages": 2984, "sha256": "48d09c6e197d9db4220f41afe83efe848a8661b1b168d3b89419751ecaf6c24d", "built_at": "2026-09-26"}}, "tree": "13", "index": "index.html", "major": "13", "pages": 1139, "release": "13.23", "source_url": "https://ftp.postgresql.org/pub/source/v13.23/postgresql-13.23.tar.bz2", "svg_assets": 3, "source_mode": "en HTML verified against the pinned official archive", "source_sha256": "6ec3c82726af92b7dec873fa1cdf881eca92a4219787dfad05acb6b10e041fd6"}, "revision": "614ee3133270254e476c118cdf6f72af78d4e4b8bbd9b28ed9ab1e14e4e9c0f6", "evidence_kind": "English manual and source declarations", "source_sha256": "6ec3c82726af92b7dec873fa1cdf881eca92a4219787dfad05acb6b10e041fd6"}, "sources": [{"url": "/docs/13/app-pgrewind.html", "file": "app-pgrewind.html", "label": "13.23 English manual \u00b7 app-pgrewind.html", "anchor": "", "sha256": "872413952b6c6f71ae9e8800961efa72be6e04c03cd117e301ebf4ac38a48d4e"}, {"url": "/docs/13/reference-server.html", "file": "reference-server.html", "label": "Server applications inventory", "anchor": "", "sha256": "074ef8577fd368272a2360309e1226e2d0b1e570a3e1da62dfcca63ac9ee5539"}], "sections": [], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "signature": "pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }", "description": ["pg_rewind \u2014 synchronize a PostgreSQL data directory with another data directory that was forked from it"], "environment": [], "manual_html": "<div><div class=\"refentry\" id=\"APP-PGREWIND\">\n<div class=\"titlepage\"></div>\n<div class=\"refnamediv\">\n<h2><span class=\"refentrytitle\"><span class=\"application\">pg_rewind</span></span></h2>\n<p>pg_rewind \u2014 synchronize a <span class=\"productname\">PostgreSQL</span> data directory with another data directory that was forked from it</p>\n</div>\n<div class=\"refsynopsisdiv\">\n<h2>Synopsis</h2>\n<div class=\"cmdsynopsis\">\n<p id=\"id-1.9.5.9.4.1\"><code class=\"command\">pg_rewind</code> [<em class=\"replaceable\"><code>option</code></em>...] { <code class=\"option\">-D</code> | <code class=\"option\">--target-pgdata</code> } <em class=\"replaceable\"><code>directory</code></em> { <code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code> | <code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code> }</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.5\">\n<h2>Description</h2>\n<p><span class=\"application\">pg_rewind</span> is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old master server back online after failover as a standby that follows the new master.</p>\n<p>After a successful rewind, the state of the target data directory is analogous to a base backup of the source data directory. Unlike taking a new base backup or using a tool like <span class=\"application\">rsync</span>, <span class=\"application\">pg_rewind</span> does not require comparing or copying unchanged relation blocks in the cluster. Only changed blocks from existing relation files are copied; all other files, including new relation files, configuration files, and WAL segments, are copied in full. As such the rewind operation is significantly faster than other approaches when the database is large and only a small fraction of blocks differ between the clusters.</p>\n<p><span class=\"application\">pg_rewind</span> examines the timeline histories of the source and target clusters to determine the point where they diverged, and expects to find WAL in the target cluster's <code class=\"filename\">pg_wal</code> directory reaching all the way back to the point of divergence. The point of divergence can be found either on the target timeline, the source timeline, or their common ancestor. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem, but if the target cluster ran for a long time after the divergence, its old WAL files might no longer be present. In this case, you can manually copy them from the WAL archive to the <code class=\"filename\">pg_wal</code> directory, or run <span class=\"application\">pg_rewind</span> with the <code class=\"literal\">-c</code> option to automatically retrieve them from the WAL archive. The use of <span class=\"application\">pg_rewind</span> is not limited to failover, e.g., a standby server can be promoted, run some write transactions, and then rewound to become a standby again.</p>\n<p>After running <span class=\"application\">pg_rewind</span>, WAL replay needs to complete for the data directory to be in a consistent state. When the target server is started again it will enter archive recovery and replay all WAL generated in the source server from the last checkpoint before the point of divergence. If some of the WAL was no longer available in the source server when <span class=\"application\">pg_rewind</span> was run, and therefore could not be copied by the <span class=\"application\">pg_rewind</span> session, it must be made available when the target server is started. This can be done by creating a <code class=\"filename\">recovery.signal</code> file in the target data directory and by configuring a suitable <a class=\"xref\" href=\"/docs/13/runtime-config-wal.html#GUC-RESTORE-COMMAND\">restore_command</a> in <code class=\"filename\">postgresql.conf</code>.</p>\n<p><span class=\"application\">pg_rewind</span> requires that the target server either has the <a class=\"xref\" href=\"/docs/13/runtime-config-wal.html#GUC-WAL-LOG-HINTS\">wal_log_hints</a> option enabled in <code class=\"filename\">postgresql.conf</code> or data checksums enabled when the cluster was initialized with <span class=\"application\">initdb</span>. Neither of these are currently on by default. <a class=\"xref\" href=\"/docs/13/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\">full_page_writes</a> must also be set to <code class=\"literal\">on</code>, but is enabled by default.</p>\n<div class=\"warning\">\n<h3 class=\"title\">Warning</h3>\n<p>If <span class=\"application\">pg_rewind</span> fails while processing, then the data folder of the target is likely not in a state that can be recovered. In such a case, taking a new fresh backup is recommended.</p>\n<p>As <span class=\"application\">pg_rewind</span> copies configuration files entirely from the source, it may be required to correct the configuration used for recovery before restarting the target server, especially if the target is reintroduced as a standby of the source. If you restart the server after the rewind operation has finished but without configuring recovery, the target may again diverge from the primary.</p>\n<p><span class=\"application\">pg_rewind</span> will fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates. If such files are present on the target server it is recommended to remove them before running <span class=\"application\">pg_rewind</span>. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind.</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.6\">\n<h2>Options</h2>\n<p><span class=\"application\">pg_rewind</span> accepts the following command-line arguments:</p>\n<div class=\"variablelist\">\n<dl class=\"variablelist\">\n<dt><span class=\"term\"><code class=\"option\">-D <em class=\"replaceable\"><code>directory</code></em></code><br></span><span class=\"term\"><code class=\"option\">--target-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running <span class=\"application\">pg_rewind</span></p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code></span></dt>\n<dd>\n<p>Specifies a libpq connection string to connect to the source <span class=\"productname\">PostgreSQL</span> server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-R</code><br></span><span class=\"term\"><code class=\"option\">--write-recovery-conf</code></span></dt>\n<dd>\n<p>Create <code class=\"filename\">standby.signal</code> and append connection settings to <code class=\"filename\">postgresql.auto.conf</code> in the output directory. <code class=\"literal\">--source-server</code> is mandatory with this option.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-n</code><br></span><span class=\"term\"><code class=\"option\">--dry-run</code></span></dt>\n<dd>\n<p>Do everything except actually modifying the target directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-N</code><br></span><span class=\"term\"><code class=\"option\">--no-sync</code></span></dt>\n<dd>\n<p>By default, <code class=\"command\">pg_rewind</code> will wait for all files to be written safely to disk. This option causes <code class=\"command\">pg_rewind</code> to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-P</code><br></span><span class=\"term\"><code class=\"option\">--progress</code></span></dt>\n<dd>\n<p>Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-c</code><br></span><span class=\"term\"><code class=\"option\">--restore-target-wal</code></span></dt>\n<dd>\n<p>Use <code class=\"varname\">restore_command</code> defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the <code class=\"filename\">pg_wal</code> directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--debug</code></span></dt>\n<dd>\n<p>Print verbose debugging output that is mostly useful for developers debugging <span class=\"application\">pg_rewind</span>.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--no-ensure-shutdown</code></span></dt>\n<dd>\n<p><span class=\"application\">pg_rewind</span> requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, <span class=\"application\">pg_rewind</span> starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, <span class=\"application\">pg_rewind</span> skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-V</code><br></span><span class=\"term\"><code class=\"option\">--version</code></span></dt>\n<dd>\n<p>Display version information, then exit.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-?</code><br></span><span class=\"term\"><code class=\"option\">--help</code></span></dt>\n<dd>\n<p>Show help, then exit.</p>\n</dd>\n</dl>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.7\">\n<h2>Environment</h2>\n<p>When <code class=\"option\">--source-server</code> option is used, <span class=\"application\">pg_rewind</span> also uses the environment variables supported by <span class=\"application\">libpq</span> (see <a class=\"xref\" href=\"/docs/13/libpq-envars.html\" title=\"33.14.\u00a0Environment Variables\">Section\u00a033.14</a>).</p>\n<p>The environment variable <code class=\"envar\">PG_COLOR</code> specifies whether to use color in diagnostic messages. Possible values are <code class=\"literal\">always</code>, <code class=\"literal\">auto</code> and <code class=\"literal\">never</code>.</p>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.8\">\n<h2>Notes</h2>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source, a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source cluster can be used instead of a superuser. Here is how to create such a role, named <code class=\"literal\">rewind_user</code> here:</p>\n<pre class=\"programlisting\">CREATE USER rewind_user LOGIN;\nGRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;\n</pre>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source which has been recently promoted, it is necessary to execute a <code class=\"command\">CHECKPOINT</code> after promotion such that its control file reflects up-to-date timeline information, which is used by <span class=\"application\">pg_rewind</span> to check if the target cluster can be rewound using the designated source cluster.</p>\n<div class=\"refsect2\" id=\"id-1.9.5.9.8.4\">\n<h3>How It Works</h3>\n<p>The basic idea is to copy all file system-level changes from the source cluster to the target cluster:</p>\n<div class=\"procedure\">\n<ol class=\"procedure\">\n<li class=\"step\">\n<p>Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's timeline history forked off from the target cluster. For each WAL record, record each data block that was touched. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off. If some of the WAL files are no longer available, try re-running <span class=\"application\">pg_rewind</span> with the <code class=\"option\">-c</code> option to search for the missing files in the WAL archive.</p>\n</li>\n<li class=\"step\">\n<p>Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (<code class=\"option\">--source-pgdata</code>) or SQL (<code class=\"option\">--source-server</code>). Relation files are now in a state equivalent to the moment of the last completed checkpoint prior to the point at which the WAL timelines of the source and target diverged plus the current state on the source of any blocks changed on the target after that divergence.</p>\n</li>\n<li class=\"step\">\n<p>Copy all other files, including new relation files, WAL segments, <code class=\"filename\">pg_xact</code>, and configuration files from the source cluster to the target cluster. Similarly to base backups, the contents of the directories <code class=\"filename\">pg_dynshmem/</code>, <code class=\"filename\">pg_notify/</code>, <code class=\"filename\">pg_replslot/</code>, <code class=\"filename\">pg_serial/</code>, <code class=\"filename\">pg_snapshots/</code>, <code class=\"filename\">pg_stat_tmp/</code>, and <code class=\"filename\">pg_subtrans/</code> are omitted from the data copied from the source cluster. The files <code class=\"filename\">backup_label</code>, <code class=\"filename\">tablespace_map</code>, <code class=\"filename\">pg_internal.init</code>, <code class=\"filename\">postmaster.opts</code>, and <code class=\"filename\">postmaster.pid</code>, as well as any file or directory beginning with <code class=\"filename\">pgsql_tmp</code>, are omitted.</p>\n</li>\n<li class=\"step\">\n<p>Create a <code class=\"filename\">backup_label</code> file to begin WAL replay at the checkpoint created at failover and configure the <code class=\"filename\">pg_control</code> file with a minimum consistency LSN defined as the result of <code class=\"literal\">pg_current_wal_insert_lsn()</code> when rewinding from a live source or the last checkpoint LSN when rewinding from a stopped source.</p>\n</li>\n<li class=\"step\">\n<p>When starting the target, <span class=\"productname\">PostgreSQL</span> replays all the required WAL, resulting in a data directory in a consistent state.</p>\n</li>\n</ol>\n</div>\n</div>\n</div>\n</div></div>", "manual_path": "app-pgrewind.html", "comparison_data": {"options": [{"names": ["-D directory", "--target-pgdata= directory"], "signature": "-D directory --target-pgdata= directory", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "signature": "--source-pgdata= directory", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "signature": "--source-server= connstr", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode."}, {"names": ["-R", "--write-recovery-conf"], "signature": "-R --write-recovery-conf", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "signature": "-n --dry-run", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "signature": "-N --no-sync", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "signature": "-P --progress", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "signature": "-c --restore-target-wal", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--debug"], "signature": "--debug", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "signature": "--no-ensure-shutdown", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["-V", "--version"], "signature": "-V --version", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "signature": "-? --help", "description": "Show help, then exit."}], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "environment": []}, "comparison_hash": "6c7bbd834621ae8ce0bc3b5048f5c1e21016d03979db8199510aa413ee8d0be1"}, "14": {"facts": [{"label": "Documented executable", "value": "pg_rewind"}, {"label": "Executable version", "value": "14.24"}, {"label": "Reference inventory", "value": "Server applications"}, {"label": "Option definition groups", "value": "12"}], "tables": [{"key": "options", "rows": [{"summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": {"url": "/docs/14/app-pgrewind.html", "text": "-D directory --target-pgdata= directory"}}, {"summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": {"url": "/docs/14/app-pgrewind.html", "text": "--source-pgdata= directory"}}, {"summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": {"url": "/docs/14/app-pgrewind.html", "text": "--source-server= connstr"}}, {"summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option.", "signature": {"url": "/docs/14/app-pgrewind.html", "text": "-R --write-recovery-conf"}}, {"summary": "Do everything except actually modifying the target directory.", "signature": {"url": "/docs/14/app-pgrewind.html", "text": "-n --dry-run"}}, {"summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": {"url": "/docs/14/app-pgrewind.html", "text": "-N --no-sync"}}, {"summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": {"url": "/docs/14/app-pgrewind.html", "text": "-P --progress"}}, {"summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": {"url": "/docs/14/app-pgrewind.html", "text": "-c --restore-target-wal"}}, {"summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": {"url": "/docs/14/app-pgrewind.html", "text": "--debug"}}, {"summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": {"url": "/docs/14/app-pgrewind.html", "text": "--no-ensure-shutdown"}}, {"summary": "Display version information, then exit.", "signature": {"url": "/docs/14/app-pgrewind.html", "text": "-V --version"}}, {"summary": "Show help, then exit.", "signature": {"url": "/docs/14/app-pgrewind.html", "text": "-? --help"}}], "title": "Documented options", "columns": [{"key": "signature", "label": "Option and arguments"}, {"key": "summary", "label": "Description"}]}], "options": [{"names": ["-D directory", "--target-pgdata= directory"], "summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": "-D directory --target-pgdata= directory", "source_url": "/docs/14/app-pgrewind.html", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": "--source-pgdata= directory", "source_url": "/docs/14/app-pgrewind.html", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": "--source-server= connstr", "source_url": "/docs/14/app-pgrewind.html", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option.", "signature": "-R --write-recovery-conf", "source_url": "/docs/14/app-pgrewind.html", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "summary": "Do everything except actually modifying the target directory.", "signature": "-n --dry-run", "source_url": "/docs/14/app-pgrewind.html", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": "-N --no-sync", "source_url": "/docs/14/app-pgrewind.html", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": "-P --progress", "source_url": "/docs/14/app-pgrewind.html", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": "-c --restore-target-wal", "source_url": "/docs/14/app-pgrewind.html", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--debug"], "summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": "--debug", "source_url": "/docs/14/app-pgrewind.html", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": "--no-ensure-shutdown", "source_url": "/docs/14/app-pgrewind.html", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["-V", "--version"], "summary": "Display version information, then exit.", "signature": "-V --version", "source_url": "/docs/14/app-pgrewind.html", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "summary": "Show help, then exit.", "signature": "-? --help", "source_url": "/docs/14/app-pgrewind.html", "description": "Show help, then exit."}], "related": [], "release": {"ref": "https://ftp.postgresql.org/pub/source/v14.24/postgresql-14.24.tar.bz2", "label": "14.24", "major": "14", "channel": "stable", "manifest": {"pdf": {"A4": {"url": "/files/documentation/pdf/14/postgresql-14-A4.pdf", "bytes": 14354704, "pages": 2944, "sha256": "8bc6b9dd7b246888bb77f0a5e8c39a2eae52e9926569832669c7d1600becb27c", "built_at": "2026-09-26"}, "US": {"url": "/files/documentation/pdf/14/postgresql-14-US.pdf", "bytes": 14242178, "pages": 3102, "sha256": "b7ecb5a5f62d8b9f73a69e25a7d26ba285373bc53168a553bff7b77955506db4", "built_at": "2026-09-26"}}, "tree": "14", "index": "index.html", "major": "14", "pages": 1158, "release": "14.24", "source_url": "https://ftp.postgresql.org/pub/source/v14.24/postgresql-14.24.tar.bz2", "svg_assets": 3, "source_mode": "en HTML verified against the pinned official archive", "source_sha256": "a7fa7ed3d558172355f51406097a7bd4f6b473be80f311ef7cda96bf383d8897"}, "revision": "588700d46356d8837c77c7c0a4e71e645fe6535983b8f1830aa8f2e4e41c40ae", "evidence_kind": "English manual and source declarations", "source_sha256": "a7fa7ed3d558172355f51406097a7bd4f6b473be80f311ef7cda96bf383d8897"}, "sources": [{"url": "/docs/14/app-pgrewind.html", "file": "app-pgrewind.html", "label": "14.24 English manual \u00b7 app-pgrewind.html", "anchor": "", "sha256": "c39a3345fa1f6830b9fca6b7369a8b8331483b00bdd5999b180e0b492bf83b48"}, {"url": "/docs/14/reference-server.html", "file": "reference-server.html", "label": "Server applications inventory", "anchor": "", "sha256": "db0cdccaa8d3c65ad3b2a07d4956eb5a783a18505bfa0f9d9626eef4ab94fe44"}], "sections": [], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "signature": "pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }", "description": ["pg_rewind \u2014 synchronize a PostgreSQL data directory with another data directory that was forked from it"], "environment": [], "manual_html": "<div><div class=\"refentry\" id=\"APP-PGREWIND\">\n<div class=\"titlepage\"></div>\n<div class=\"refnamediv\">\n<h2><span class=\"refentrytitle\"><span class=\"application\">pg_rewind</span></span></h2>\n<p>pg_rewind \u2014 synchronize a <span class=\"productname\">PostgreSQL</span> data directory with another data directory that was forked from it</p>\n</div>\n<div class=\"refsynopsisdiv\">\n<h2>Synopsis</h2>\n<div class=\"cmdsynopsis\">\n<p id=\"id-1.9.5.9.4.1\"><code class=\"command\">pg_rewind</code> [<em class=\"replaceable\"><code>option</code></em>...] { <code class=\"option\">-D</code> | <code class=\"option\">--target-pgdata</code> } <em class=\"replaceable\"><code>directory</code></em> { <code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code> | <code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code> }</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.5\">\n<h2>Description</h2>\n<p><span class=\"application\">pg_rewind</span> is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old primary server back online after failover as a standby that follows the new primary.</p>\n<p>After a successful rewind, the state of the target data directory is analogous to a base backup of the source data directory. Unlike taking a new base backup or using a tool like <span class=\"application\">rsync</span>, <span class=\"application\">pg_rewind</span> does not require comparing or copying unchanged relation blocks in the cluster. Only changed blocks from existing relation files are copied; all other files, including new relation files, configuration files, and WAL segments, are copied in full. As such the rewind operation is significantly faster than other approaches when the database is large and only a small fraction of blocks differ between the clusters.</p>\n<p><span class=\"application\">pg_rewind</span> examines the timeline histories of the source and target clusters to determine the point where they diverged, and expects to find WAL in the target cluster's <code class=\"filename\">pg_wal</code> directory reaching all the way back to the point of divergence. The point of divergence can be found either on the target timeline, the source timeline, or their common ancestor. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem, but if the target cluster ran for a long time after the divergence, its old WAL files might no longer be present. In this case, you can manually copy them from the WAL archive to the <code class=\"filename\">pg_wal</code> directory, or run <span class=\"application\">pg_rewind</span> with the <code class=\"literal\">-c</code> option to automatically retrieve them from the WAL archive. The use of <span class=\"application\">pg_rewind</span> is not limited to failover, e.g., a standby server can be promoted, run some write transactions, and then rewound to become a standby again.</p>\n<p>After running <span class=\"application\">pg_rewind</span>, WAL replay needs to complete for the data directory to be in a consistent state. When the target server is started again it will enter archive recovery and replay all WAL generated in the source server from the last checkpoint before the point of divergence. If some of the WAL was no longer available in the source server when <span class=\"application\">pg_rewind</span> was run, and therefore could not be copied by the <span class=\"application\">pg_rewind</span> session, it must be made available when the target server is started. This can be done by creating a <code class=\"filename\">recovery.signal</code> file in the target data directory and by configuring a suitable <a class=\"xref\" href=\"/docs/14/runtime-config-wal.html#GUC-RESTORE-COMMAND\">restore_command</a> in <code class=\"filename\">postgresql.conf</code>.</p>\n<p><span class=\"application\">pg_rewind</span> requires that the target server either has the <a class=\"xref\" href=\"/docs/14/runtime-config-wal.html#GUC-WAL-LOG-HINTS\">wal_log_hints</a> option enabled in <code class=\"filename\">postgresql.conf</code> or data checksums enabled when the cluster was initialized with <span class=\"application\">initdb</span>. Neither of these are currently on by default. <a class=\"xref\" href=\"/docs/14/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\">full_page_writes</a> must also be set to <code class=\"literal\">on</code>, but is enabled by default.</p>\n<div class=\"warning\">\n<h3 class=\"title\">Warning</h3>\n<p>If <span class=\"application\">pg_rewind</span> fails while processing, then the data folder of the target is likely not in a state that can be recovered. In such a case, taking a new fresh backup is recommended.</p>\n<p>As <span class=\"application\">pg_rewind</span> copies configuration files entirely from the source, it may be required to correct the configuration used for recovery before restarting the target server, especially if the target is reintroduced as a standby of the source. If you restart the server after the rewind operation has finished but without configuring recovery, the target may again diverge from the primary.</p>\n<p><span class=\"application\">pg_rewind</span> will fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates. If such files are present on the target server it is recommended to remove them before running <span class=\"application\">pg_rewind</span>. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind.</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.6\">\n<h2>Options</h2>\n<p><span class=\"application\">pg_rewind</span> accepts the following command-line arguments:</p>\n<div class=\"variablelist\">\n<dl class=\"variablelist\">\n<dt><span class=\"term\"><code class=\"option\">-D <em class=\"replaceable\"><code>directory</code></em></code><br></span><span class=\"term\"><code class=\"option\">--target-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running <span class=\"application\">pg_rewind</span></p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code></span></dt>\n<dd>\n<p>Specifies a libpq connection string to connect to the source <span class=\"productname\">PostgreSQL</span> server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-R</code><br></span><span class=\"term\"><code class=\"option\">--write-recovery-conf</code></span></dt>\n<dd>\n<p>Create <code class=\"filename\">standby.signal</code> and append connection settings to <code class=\"filename\">postgresql.auto.conf</code> in the output directory. <code class=\"literal\">--source-server</code> is mandatory with this option.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-n</code><br></span><span class=\"term\"><code class=\"option\">--dry-run</code></span></dt>\n<dd>\n<p>Do everything except actually modifying the target directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-N</code><br></span><span class=\"term\"><code class=\"option\">--no-sync</code></span></dt>\n<dd>\n<p>By default, <code class=\"command\">pg_rewind</code> will wait for all files to be written safely to disk. This option causes <code class=\"command\">pg_rewind</code> to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-P</code><br></span><span class=\"term\"><code class=\"option\">--progress</code></span></dt>\n<dd>\n<p>Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-c</code><br></span><span class=\"term\"><code class=\"option\">--restore-target-wal</code></span></dt>\n<dd>\n<p>Use <code class=\"varname\">restore_command</code> defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the <code class=\"filename\">pg_wal</code> directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--debug</code></span></dt>\n<dd>\n<p>Print verbose debugging output that is mostly useful for developers debugging <span class=\"application\">pg_rewind</span>.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--no-ensure-shutdown</code></span></dt>\n<dd>\n<p><span class=\"application\">pg_rewind</span> requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, <span class=\"application\">pg_rewind</span> starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, <span class=\"application\">pg_rewind</span> skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-V</code><br></span><span class=\"term\"><code class=\"option\">--version</code></span></dt>\n<dd>\n<p>Display version information, then exit.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-?</code><br></span><span class=\"term\"><code class=\"option\">--help</code></span></dt>\n<dd>\n<p>Show help, then exit.</p>\n</dd>\n</dl>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.7\">\n<h2>Environment</h2>\n<p>When <code class=\"option\">--source-server</code> option is used, <span class=\"application\">pg_rewind</span> also uses the environment variables supported by <span class=\"application\">libpq</span> (see <a class=\"xref\" href=\"/docs/14/libpq-envars.html\" title=\"34.15.\u00a0Environment Variables\">Section\u00a034.15</a>).</p>\n<p>The environment variable <code class=\"envar\">PG_COLOR</code> specifies whether to use color in diagnostic messages. Possible values are <code class=\"literal\">always</code>, <code class=\"literal\">auto</code> and <code class=\"literal\">never</code>.</p>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.8\">\n<h2>Notes</h2>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source, a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source cluster can be used instead of a superuser. Here is how to create such a role, named <code class=\"literal\">rewind_user</code> here:</p>\n<pre class=\"programlisting\">CREATE USER rewind_user LOGIN;\nGRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;\n</pre>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source which has been recently promoted, it is necessary to execute a <code class=\"command\">CHECKPOINT</code> after promotion such that its control file reflects up-to-date timeline information, which is used by <span class=\"application\">pg_rewind</span> to check if the target cluster can be rewound using the designated source cluster.</p>\n<div class=\"refsect2\" id=\"id-1.9.5.9.8.4\">\n<h3>How It Works</h3>\n<p>The basic idea is to copy all file system-level changes from the source cluster to the target cluster:</p>\n<div class=\"procedure\">\n<ol class=\"procedure\">\n<li class=\"step\">\n<p>Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's timeline history forked off from the target cluster. For each WAL record, record each data block that was touched. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off. If some of the WAL files are no longer available, try re-running <span class=\"application\">pg_rewind</span> with the <code class=\"option\">-c</code> option to search for the missing files in the WAL archive.</p>\n</li>\n<li class=\"step\">\n<p>Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (<code class=\"option\">--source-pgdata</code>) or SQL (<code class=\"option\">--source-server</code>). Relation files are now in a state equivalent to the moment of the last completed checkpoint prior to the point at which the WAL timelines of the source and target diverged plus the current state on the source of any blocks changed on the target after that divergence.</p>\n</li>\n<li class=\"step\">\n<p>Copy all other files, including new relation files, WAL segments, <code class=\"filename\">pg_xact</code>, and configuration files from the source cluster to the target cluster. Similarly to base backups, the contents of the directories <code class=\"filename\">pg_dynshmem/</code>, <code class=\"filename\">pg_notify/</code>, <code class=\"filename\">pg_replslot/</code>, <code class=\"filename\">pg_serial/</code>, <code class=\"filename\">pg_snapshots/</code>, <code class=\"filename\">pg_stat_tmp/</code>, and <code class=\"filename\">pg_subtrans/</code> are omitted from the data copied from the source cluster. The files <code class=\"filename\">backup_label</code>, <code class=\"filename\">tablespace_map</code>, <code class=\"filename\">pg_internal.init</code>, <code class=\"filename\">postmaster.opts</code>, and <code class=\"filename\">postmaster.pid</code>, as well as any file or directory beginning with <code class=\"filename\">pgsql_tmp</code>, are omitted.</p>\n</li>\n<li class=\"step\">\n<p>Create a <code class=\"filename\">backup_label</code> file to begin WAL replay at the checkpoint created at failover and configure the <code class=\"filename\">pg_control</code> file with a minimum consistency LSN defined as the result of <code class=\"literal\">pg_current_wal_insert_lsn()</code> when rewinding from a live source or the last checkpoint LSN when rewinding from a stopped source.</p>\n</li>\n<li class=\"step\">\n<p>When starting the target, <span class=\"productname\">PostgreSQL</span> replays all the required WAL, resulting in a data directory in a consistent state.</p>\n</li>\n</ol>\n</div>\n</div>\n</div>\n</div></div>", "manual_path": "app-pgrewind.html", "comparison_data": {"options": [{"names": ["-D directory", "--target-pgdata= directory"], "signature": "-D directory --target-pgdata= directory", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "signature": "--source-pgdata= directory", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "signature": "--source-server= connstr", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "signature": "-R --write-recovery-conf", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "signature": "-n --dry-run", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "signature": "-N --no-sync", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "signature": "-P --progress", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "signature": "-c --restore-target-wal", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--debug"], "signature": "--debug", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "signature": "--no-ensure-shutdown", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["-V", "--version"], "signature": "-V --version", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "signature": "-? --help", "description": "Show help, then exit."}], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "environment": []}, "comparison_hash": "b3eb33077ff8ca1f2ef6abd13d7ef8e6a4f283b603a3c2d90805bcad7f7f9a8f"}, "15": {"facts": [{"label": "Documented executable", "value": "pg_rewind"}, {"label": "Executable version", "value": "15.19"}, {"label": "Reference inventory", "value": "Server applications"}, {"label": "Option definition groups", "value": "13"}], "tables": [{"key": "options", "rows": [{"summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": {"url": "/docs/15/app-pgrewind.html", "text": "-D directory --target-pgdata= directory"}}, {"summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": {"url": "/docs/15/app-pgrewind.html", "text": "--source-pgdata= directory"}}, {"summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": {"url": "/docs/15/app-pgrewind.html", "text": "--source-server= connstr"}}, {"summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option.", "signature": {"url": "/docs/15/app-pgrewind.html", "text": "-R --write-recovery-conf"}}, {"summary": "Do everything except actually modifying the target directory.", "signature": {"url": "/docs/15/app-pgrewind.html", "text": "-n --dry-run"}}, {"summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": {"url": "/docs/15/app-pgrewind.html", "text": "-N --no-sync"}}, {"summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": {"url": "/docs/15/app-pgrewind.html", "text": "-P --progress"}}, {"summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": {"url": "/docs/15/app-pgrewind.html", "text": "-c --restore-target-wal"}}, {"summary": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery).", "signature": {"url": "/docs/15/app-pgrewind.html", "text": "--config-file= filename"}}, {"summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": {"url": "/docs/15/app-pgrewind.html", "text": "--debug"}}, {"summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": {"url": "/docs/15/app-pgrewind.html", "text": "--no-ensure-shutdown"}}, {"summary": "Display version information, then exit.", "signature": {"url": "/docs/15/app-pgrewind.html", "text": "-V --version"}}, {"summary": "Show help, then exit.", "signature": {"url": "/docs/15/app-pgrewind.html", "text": "-? --help"}}], "title": "Documented options", "columns": [{"key": "signature", "label": "Option and arguments"}, {"key": "summary", "label": "Description"}]}], "options": [{"names": ["-D directory", "--target-pgdata= directory"], "summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": "-D directory --target-pgdata= directory", "source_url": "/docs/15/app-pgrewind.html", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": "--source-pgdata= directory", "source_url": "/docs/15/app-pgrewind.html", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": "--source-server= connstr", "source_url": "/docs/15/app-pgrewind.html", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option.", "signature": "-R --write-recovery-conf", "source_url": "/docs/15/app-pgrewind.html", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "summary": "Do everything except actually modifying the target directory.", "signature": "-n --dry-run", "source_url": "/docs/15/app-pgrewind.html", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": "-N --no-sync", "source_url": "/docs/15/app-pgrewind.html", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": "-P --progress", "source_url": "/docs/15/app-pgrewind.html", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": "-c --restore-target-wal", "source_url": "/docs/15/app-pgrewind.html", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--config-file= filename"], "summary": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery).", "signature": "--config-file= filename", "source_url": "/docs/15/app-pgrewind.html", "description": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery)."}, {"names": ["--debug"], "summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": "--debug", "source_url": "/docs/15/app-pgrewind.html", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": "--no-ensure-shutdown", "source_url": "/docs/15/app-pgrewind.html", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["-V", "--version"], "summary": "Display version information, then exit.", "signature": "-V --version", "source_url": "/docs/15/app-pgrewind.html", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "summary": "Show help, then exit.", "signature": "-? --help", "source_url": "/docs/15/app-pgrewind.html", "description": "Show help, then exit."}], "related": [], "release": {"ref": "https://ftp.postgresql.org/pub/source/v15.19/postgresql-15.19.tar.bz2", "label": "15.19", "major": "15", "channel": "stable", "manifest": {"pdf": {"A4": {"url": "/files/documentation/pdf/15/postgresql-15-A4.pdf", "bytes": 14609140, "pages": 2987, "sha256": "66228564a4d16efb47d6a914085716ecfe22ca1566db56bc7c3d82f790646d72", "built_at": "2026-09-26"}, "US": {"url": "/files/documentation/pdf/15/postgresql-15-US.pdf", "bytes": 14495690, "pages": 3153, "sha256": "645a498c2390d47a6223ec74770631185807a19c484edb0fc5a295d9f460bc02", "built_at": "2026-09-26"}}, "tree": "15", "index": "index.html", "major": "15", "pages": 1168, "release": "15.19", "source_url": "https://ftp.postgresql.org/pub/source/v15.19/postgresql-15.19.tar.bz2", "svg_assets": 3, "source_mode": "en HTML verified against the pinned official archive", "source_sha256": "e1a64a87a46b825b88c082e4518161a47aab53c45694964f8ba1df28f7859f89"}, "revision": "6af958c6520151fc7697d56e0616b03fb00522c4568158674d452ae0644ba545", "evidence_kind": "English manual and source declarations", "source_sha256": "e1a64a87a46b825b88c082e4518161a47aab53c45694964f8ba1df28f7859f89"}, "sources": [{"url": "/docs/15/app-pgrewind.html", "file": "app-pgrewind.html", "label": "15.19 English manual \u00b7 app-pgrewind.html", "anchor": "", "sha256": "dda1f5f230ff9ad8bb72961bcf86367ce739afb3b91c5f62d5b349867a43e7ab"}, {"url": "/docs/15/reference-server.html", "file": "reference-server.html", "label": "Server applications inventory", "anchor": "", "sha256": "1240cfbb13ebfd22aac8d5ae40c62a9015b246f7774bcf0bce97355ce06350a0"}], "sections": [], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "signature": "pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }", "description": ["pg_rewind \u2014 synchronize a PostgreSQL data directory with another data directory that was forked from it"], "environment": [], "manual_html": "<div><div class=\"refentry\" id=\"APP-PGREWIND\">\n<div class=\"titlepage\"></div>\n<div class=\"refnamediv\">\n<h2><span class=\"refentrytitle\"><span class=\"application\">pg_rewind</span></span></h2>\n<p>pg_rewind \u2014 synchronize a <span class=\"productname\">PostgreSQL</span> data directory with another data directory that was forked from it</p>\n</div>\n<div class=\"refsynopsisdiv\">\n<h2>Synopsis</h2>\n<div class=\"cmdsynopsis\">\n<p id=\"id-1.9.5.9.4.1\"><code class=\"command\">pg_rewind</code> [<em class=\"replaceable\"><code>option</code></em>...] { <code class=\"option\">-D</code> | <code class=\"option\">--target-pgdata</code> } <em class=\"replaceable\"><code>directory</code></em> { <code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code> | <code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code> }</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.5\">\n<h2>Description</h2>\n<p><span class=\"application\">pg_rewind</span> is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old primary server back online after failover as a standby that follows the new primary.</p>\n<p>After a successful rewind, the state of the target data directory is analogous to a base backup of the source data directory. Unlike taking a new base backup or using a tool like <span class=\"application\">rsync</span>, <span class=\"application\">pg_rewind</span> does not require comparing or copying unchanged relation blocks in the cluster. Only changed blocks from existing relation files are copied; all other files, including new relation files, configuration files, and WAL segments, are copied in full. As such the rewind operation is significantly faster than other approaches when the database is large and only a small fraction of blocks differ between the clusters.</p>\n<p><span class=\"application\">pg_rewind</span> examines the timeline histories of the source and target clusters to determine the point where they diverged, and expects to find WAL in the target cluster's <code class=\"filename\">pg_wal</code> directory reaching all the way back to the point of divergence. The point of divergence can be found either on the target timeline, the source timeline, or their common ancestor. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem, but if the target cluster ran for a long time after the divergence, its old WAL files might no longer be present. In this case, you can manually copy them from the WAL archive to the <code class=\"filename\">pg_wal</code> directory, or run <span class=\"application\">pg_rewind</span> with the <code class=\"literal\">-c</code> option to automatically retrieve them from the WAL archive. The use of <span class=\"application\">pg_rewind</span> is not limited to failover, e.g., a standby server can be promoted, run some write transactions, and then rewound to become a standby again.</p>\n<p>After running <span class=\"application\">pg_rewind</span>, WAL replay needs to complete for the data directory to be in a consistent state. When the target server is started again it will enter archive recovery and replay all WAL generated in the source server from the last checkpoint before the point of divergence. If some of the WAL was no longer available in the source server when <span class=\"application\">pg_rewind</span> was run, and therefore could not be copied by the <span class=\"application\">pg_rewind</span> session, it must be made available when the target server is started. This can be done by creating a <code class=\"filename\">recovery.signal</code> file in the target data directory and by configuring a suitable <a class=\"xref\" href=\"/docs/15/runtime-config-wal.html#GUC-RESTORE-COMMAND\">restore_command</a> in <code class=\"filename\">postgresql.conf</code>.</p>\n<p><span class=\"application\">pg_rewind</span> requires that the target server either has the <a class=\"xref\" href=\"/docs/15/runtime-config-wal.html#GUC-WAL-LOG-HINTS\">wal_log_hints</a> option enabled in <code class=\"filename\">postgresql.conf</code> or data checksums enabled when the cluster was initialized with <span class=\"application\">initdb</span>. Neither of these are currently on by default. <a class=\"xref\" href=\"/docs/15/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\">full_page_writes</a> must also be set to <code class=\"literal\">on</code>, but is enabled by default.</p>\n<div class=\"warning\">\n<h3 class=\"title\">Warning</h3>\n<p>If <span class=\"application\">pg_rewind</span> fails while processing, then the data folder of the target is likely not in a state that can be recovered. In such a case, taking a new fresh backup is recommended.</p>\n<p>As <span class=\"application\">pg_rewind</span> copies configuration files entirely from the source, it may be required to correct the configuration used for recovery before restarting the target server, especially if the target is reintroduced as a standby of the source. If you restart the server after the rewind operation has finished but without configuring recovery, the target may again diverge from the primary.</p>\n<p><span class=\"application\">pg_rewind</span> will fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates. If such files are present on the target server it is recommended to remove them before running <span class=\"application\">pg_rewind</span>. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind.</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.6\">\n<h2>Options</h2>\n<p><span class=\"application\">pg_rewind</span> accepts the following command-line arguments:</p>\n<div class=\"variablelist\">\n<dl class=\"variablelist\">\n<dt><span class=\"term\"><code class=\"option\">-D <em class=\"replaceable\"><code>directory</code></em></code><br></span><span class=\"term\"><code class=\"option\">--target-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running <span class=\"application\">pg_rewind</span></p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code></span></dt>\n<dd>\n<p>Specifies a libpq connection string to connect to the source <span class=\"productname\">PostgreSQL</span> server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-R</code><br></span><span class=\"term\"><code class=\"option\">--write-recovery-conf</code></span></dt>\n<dd>\n<p>Create <code class=\"filename\">standby.signal</code> and append connection settings to <code class=\"filename\">postgresql.auto.conf</code> in the output directory. <code class=\"literal\">--source-server</code> is mandatory with this option.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-n</code><br></span><span class=\"term\"><code class=\"option\">--dry-run</code></span></dt>\n<dd>\n<p>Do everything except actually modifying the target directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-N</code><br></span><span class=\"term\"><code class=\"option\">--no-sync</code></span></dt>\n<dd>\n<p>By default, <code class=\"command\">pg_rewind</code> will wait for all files to be written safely to disk. This option causes <code class=\"command\">pg_rewind</code> to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-P</code><br></span><span class=\"term\"><code class=\"option\">--progress</code></span></dt>\n<dd>\n<p>Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-c</code><br></span><span class=\"term\"><code class=\"option\">--restore-target-wal</code></span></dt>\n<dd>\n<p>Use <code class=\"varname\">restore_command</code> defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the <code class=\"filename\">pg_wal</code> directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--config-file=<em class=\"replaceable\"><code>filename</code></em></code></span></dt>\n<dd>\n<p>Use the specified main server configuration file for the target cluster. This affects <span class=\"application\">pg_rewind</span> when it uses internally the <span class=\"application\">postgres</span> command for the rewind operation on this cluster (when retrieving <code class=\"varname\">restore_command</code> with the option <code class=\"option\">-c/--restore-target-wal</code> and when forcing a completion of crash recovery).</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--debug</code></span></dt>\n<dd>\n<p>Print verbose debugging output that is mostly useful for developers debugging <span class=\"application\">pg_rewind</span>.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--no-ensure-shutdown</code></span></dt>\n<dd>\n<p><span class=\"application\">pg_rewind</span> requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, <span class=\"application\">pg_rewind</span> starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, <span class=\"application\">pg_rewind</span> skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-V</code><br></span><span class=\"term\"><code class=\"option\">--version</code></span></dt>\n<dd>\n<p>Display version information, then exit.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-?</code><br></span><span class=\"term\"><code class=\"option\">--help</code></span></dt>\n<dd>\n<p>Show help, then exit.</p>\n</dd>\n</dl>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.7\">\n<h2>Environment</h2>\n<p>When <code class=\"option\">--source-server</code> option is used, <span class=\"application\">pg_rewind</span> also uses the environment variables supported by <span class=\"application\">libpq</span> (see <a class=\"xref\" href=\"/docs/15/libpq-envars.html\" title=\"34.15.\u00a0Environment Variables\">Section\u00a034.15</a>).</p>\n<p>The environment variable <code class=\"envar\">PG_COLOR</code> specifies whether to use color in diagnostic messages. Possible values are <code class=\"literal\">always</code>, <code class=\"literal\">auto</code> and <code class=\"literal\">never</code>.</p>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.8\">\n<h2>Notes</h2>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source, a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source cluster can be used instead of a superuser. Here is how to create such a role, named <code class=\"literal\">rewind_user</code> here:</p>\n<pre class=\"programlisting\">CREATE USER rewind_user LOGIN;\nGRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;\n</pre>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source which has been recently promoted, it is necessary to execute a <code class=\"command\">CHECKPOINT</code> after promotion such that its control file reflects up-to-date timeline information, which is used by <span class=\"application\">pg_rewind</span> to check if the target cluster can be rewound using the designated source cluster.</p>\n<div class=\"refsect2\" id=\"id-1.9.5.9.8.4\">\n<h3>How It Works</h3>\n<p>The basic idea is to copy all file system-level changes from the source cluster to the target cluster:</p>\n<div class=\"procedure\">\n<ol class=\"procedure\">\n<li class=\"step\">\n<p>Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's timeline history forked off from the target cluster. For each WAL record, record each data block that was touched. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off. If some of the WAL files are no longer available, try re-running <span class=\"application\">pg_rewind</span> with the <code class=\"option\">-c</code> option to search for the missing files in the WAL archive.</p>\n</li>\n<li class=\"step\">\n<p>Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (<code class=\"option\">--source-pgdata</code>) or SQL (<code class=\"option\">--source-server</code>). Relation files are now in a state equivalent to the moment of the last completed checkpoint prior to the point at which the WAL timelines of the source and target diverged plus the current state on the source of any blocks changed on the target after that divergence.</p>\n</li>\n<li class=\"step\">\n<p>Copy all other files, including new relation files, WAL segments, <code class=\"filename\">pg_xact</code>, and configuration files from the source cluster to the target cluster. Similarly to base backups, the contents of the directories <code class=\"filename\">pg_dynshmem/</code>, <code class=\"filename\">pg_notify/</code>, <code class=\"filename\">pg_replslot/</code>, <code class=\"filename\">pg_serial/</code>, <code class=\"filename\">pg_snapshots/</code>, <code class=\"filename\">pg_stat_tmp/</code>, and <code class=\"filename\">pg_subtrans/</code> are omitted from the data copied from the source cluster. The files <code class=\"filename\">backup_label</code>, <code class=\"filename\">tablespace_map</code>, <code class=\"filename\">pg_internal.init</code>, <code class=\"filename\">postmaster.opts</code>, <code class=\"filename\">postmaster.pid</code> and <code class=\"filename\">.DS_Store</code> as well as any file or directory beginning with <code class=\"filename\">pgsql_tmp</code>, are omitted.</p>\n</li>\n<li class=\"step\">\n<p>Create a <code class=\"filename\">backup_label</code> file to begin WAL replay at the checkpoint created at failover and configure the <code class=\"filename\">pg_control</code> file with a minimum consistency LSN defined as the result of <code class=\"literal\">pg_current_wal_insert_lsn()</code> when rewinding from a live source or the last checkpoint LSN when rewinding from a stopped source.</p>\n</li>\n<li class=\"step\">\n<p>When starting the target, <span class=\"productname\">PostgreSQL</span> replays all the required WAL, resulting in a data directory in a consistent state.</p>\n</li>\n</ol>\n</div>\n</div>\n</div>\n</div></div>", "manual_path": "app-pgrewind.html", "comparison_data": {"options": [{"names": ["-D directory", "--target-pgdata= directory"], "signature": "-D directory --target-pgdata= directory", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "signature": "--source-pgdata= directory", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "signature": "--source-server= connstr", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "signature": "-R --write-recovery-conf", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "signature": "-n --dry-run", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "signature": "-N --no-sync", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "signature": "-P --progress", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "signature": "-c --restore-target-wal", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--config-file= filename"], "signature": "--config-file= filename", "description": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery)."}, {"names": ["--debug"], "signature": "--debug", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "signature": "--no-ensure-shutdown", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["-V", "--version"], "signature": "-V --version", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "signature": "-? --help", "description": "Show help, then exit."}], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "environment": []}, "comparison_hash": "f912a281dbc53bb5c56ffa693dfd803e6e1f643eb7d467ea448bc9f4cf3e087e"}, "16": {"facts": [{"label": "Documented executable", "value": "pg_rewind"}, {"label": "Executable version", "value": "16.15"}, {"label": "Reference inventory", "value": "Server applications"}, {"label": "Option definition groups", "value": "13"}], "tables": [{"key": "options", "rows": [{"summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": {"url": "/docs/16/app-pgrewind.html", "text": "-D directory --target-pgdata= directory"}}, {"summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": {"url": "/docs/16/app-pgrewind.html", "text": "--source-pgdata= directory"}}, {"summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": {"url": "/docs/16/app-pgrewind.html", "text": "--source-server= connstr"}}, {"summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option.", "signature": {"url": "/docs/16/app-pgrewind.html", "text": "-R --write-recovery-conf"}}, {"summary": "Do everything except actually modifying the target directory.", "signature": {"url": "/docs/16/app-pgrewind.html", "text": "-n --dry-run"}}, {"summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": {"url": "/docs/16/app-pgrewind.html", "text": "-N --no-sync"}}, {"summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": {"url": "/docs/16/app-pgrewind.html", "text": "-P --progress"}}, {"summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": {"url": "/docs/16/app-pgrewind.html", "text": "-c --restore-target-wal"}}, {"summary": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery).", "signature": {"url": "/docs/16/app-pgrewind.html", "text": "--config-file= filename"}}, {"summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": {"url": "/docs/16/app-pgrewind.html", "text": "--debug"}}, {"summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": {"url": "/docs/16/app-pgrewind.html", "text": "--no-ensure-shutdown"}}, {"summary": "Display version information, then exit.", "signature": {"url": "/docs/16/app-pgrewind.html", "text": "-V --version"}}, {"summary": "Show help, then exit.", "signature": {"url": "/docs/16/app-pgrewind.html", "text": "-? --help"}}], "title": "Documented options", "columns": [{"key": "signature", "label": "Option and arguments"}, {"key": "summary", "label": "Description"}]}], "options": [{"names": ["-D directory", "--target-pgdata= directory"], "summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": "-D directory --target-pgdata= directory", "source_url": "/docs/16/app-pgrewind.html", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": "--source-pgdata= directory", "source_url": "/docs/16/app-pgrewind.html", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": "--source-server= connstr", "source_url": "/docs/16/app-pgrewind.html", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option.", "signature": "-R --write-recovery-conf", "source_url": "/docs/16/app-pgrewind.html", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "summary": "Do everything except actually modifying the target directory.", "signature": "-n --dry-run", "source_url": "/docs/16/app-pgrewind.html", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": "-N --no-sync", "source_url": "/docs/16/app-pgrewind.html", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": "-P --progress", "source_url": "/docs/16/app-pgrewind.html", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": "-c --restore-target-wal", "source_url": "/docs/16/app-pgrewind.html", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--config-file= filename"], "summary": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery).", "signature": "--config-file= filename", "source_url": "/docs/16/app-pgrewind.html", "description": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery)."}, {"names": ["--debug"], "summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": "--debug", "source_url": "/docs/16/app-pgrewind.html", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": "--no-ensure-shutdown", "source_url": "/docs/16/app-pgrewind.html", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["-V", "--version"], "summary": "Display version information, then exit.", "signature": "-V --version", "source_url": "/docs/16/app-pgrewind.html", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "summary": "Show help, then exit.", "signature": "-? --help", "source_url": "/docs/16/app-pgrewind.html", "description": "Show help, then exit."}], "related": [], "release": {"ref": "https://ftp.postgresql.org/pub/source/v16.15/postgresql-16.15.tar.bz2", "label": "16.15", "major": "16", "channel": "stable", "manifest": {"pdf": {"A4": {"url": "/files/documentation/pdf/16/postgresql-16-A4.pdf", "bytes": 15282337, "pages": 3055, "sha256": "4bb6c1f63deedac98736d8c4c7bc0fad0ac24e85b07e21ee10411872f06afd06", "built_at": "2026-09-26"}, "US": {"url": "/files/documentation/pdf/16/postgresql-16-US.pdf", "bytes": 15164148, "pages": 3220, "sha256": "5b6b6166c89991199e144bb0ae34c17a5f29826251dbf19db1abc0df3a3e771b", "built_at": "2026-09-26"}}, "tree": "16", "index": "index.html", "major": "16", "pages": 1169, "release": "16.15", "source_url": "https://ftp.postgresql.org/pub/source/v16.15/postgresql-16.15.tar.bz2", "svg_assets": 3, "source_mode": "en HTML verified against the pinned official archive", "source_sha256": "c1575341fa7bd40f5274ea465b34390f4dc64cdd0770af327005caaeb9f6b7ed"}, "revision": "3c21e58b35318021716440e67bb8bafd392b6a2b965a244d90e9e33c99e0bdef", "evidence_kind": "English manual and source declarations", "source_sha256": "c1575341fa7bd40f5274ea465b34390f4dc64cdd0770af327005caaeb9f6b7ed"}, "sources": [{"url": "/docs/16/app-pgrewind.html", "file": "app-pgrewind.html", "label": "16.15 English manual \u00b7 app-pgrewind.html", "anchor": "", "sha256": "947029346e76c330f2142fe2912ec7929bd4a0c5655eade70139d8398b055eda"}, {"url": "/docs/16/reference-server.html", "file": "reference-server.html", "label": "Server applications inventory", "anchor": "", "sha256": "8aaa4422eece8f26d84715f855d9d0f7f87b200f8b82898c73c865ee42c2c70f"}], "sections": [], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "signature": "pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }", "description": ["pg_rewind \u2014 synchronize a PostgreSQL data directory with another data directory that was forked from it"], "environment": [], "manual_html": "<div><div class=\"refentry\" id=\"APP-PGREWIND\">\n<div class=\"titlepage\"></div>\n<div class=\"refnamediv\">\n<h2><span class=\"refentrytitle\"><span class=\"application\">pg_rewind</span></span></h2>\n<p>pg_rewind \u2014 synchronize a <span class=\"productname\">PostgreSQL</span> data directory with another data directory that was forked from it</p>\n</div>\n<div class=\"refsynopsisdiv\">\n<h2>Synopsis</h2>\n<div class=\"cmdsynopsis\">\n<p id=\"id-1.9.5.9.4.1\"><code class=\"command\">pg_rewind</code> [<em class=\"replaceable\"><code>option</code></em>...] { <code class=\"option\">-D</code> | <code class=\"option\">--target-pgdata</code> } <em class=\"replaceable\"><code>directory</code></em> { <code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code> | <code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code> }</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.5\">\n<h2>Description</h2>\n<p><span class=\"application\">pg_rewind</span> is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old primary server back online after failover as a standby that follows the new primary.</p>\n<p>After a successful rewind, the state of the target data directory is analogous to a base backup of the source data directory. Unlike taking a new base backup or using a tool like <span class=\"application\">rsync</span>, <span class=\"application\">pg_rewind</span> does not require comparing or copying unchanged relation blocks in the cluster. Only changed blocks from existing relation files are copied; all other files, including new relation files, configuration files, and WAL segments, are copied in full. As such the rewind operation is significantly faster than other approaches when the database is large and only a small fraction of blocks differ between the clusters.</p>\n<p><span class=\"application\">pg_rewind</span> examines the timeline histories of the source and target clusters to determine the point where they diverged, and expects to find WAL in the target cluster's <code class=\"filename\">pg_wal</code> directory reaching all the way back to the point of divergence. The point of divergence can be found either on the target timeline, the source timeline, or their common ancestor. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem, but if the target cluster ran for a long time after the divergence, its old WAL files might no longer be present. In this case, you can manually copy them from the WAL archive to the <code class=\"filename\">pg_wal</code> directory, or run <span class=\"application\">pg_rewind</span> with the <code class=\"literal\">-c</code> option to automatically retrieve them from the WAL archive. The use of <span class=\"application\">pg_rewind</span> is not limited to failover, e.g., a standby server can be promoted, run some write transactions, and then rewound to become a standby again.</p>\n<p>After running <span class=\"application\">pg_rewind</span>, WAL replay needs to complete for the data directory to be in a consistent state. When the target server is started again it will enter archive recovery and replay all WAL generated in the source server from the last checkpoint before the point of divergence. If some of the WAL was no longer available in the source server when <span class=\"application\">pg_rewind</span> was run, and therefore could not be copied by the <span class=\"application\">pg_rewind</span> session, it must be made available when the target server is started. This can be done by creating a <code class=\"filename\">recovery.signal</code> file in the target data directory and by configuring a suitable <a class=\"xref\" href=\"/docs/16/runtime-config-wal.html#GUC-RESTORE-COMMAND\">restore_command</a> in <code class=\"filename\">postgresql.conf</code>.</p>\n<p><span class=\"application\">pg_rewind</span> requires that the target server either has the <a class=\"xref\" href=\"/docs/16/runtime-config-wal.html#GUC-WAL-LOG-HINTS\">wal_log_hints</a> option enabled in <code class=\"filename\">postgresql.conf</code> or data checksums enabled when the cluster was initialized with <span class=\"application\">initdb</span>. Neither of these are currently on by default. <a class=\"xref\" href=\"/docs/16/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\">full_page_writes</a> must also be set to <code class=\"literal\">on</code>, but is enabled by default.</p>\n<div class=\"warning\">\n<h3 class=\"title\">Warning: Failures While Rewinding</h3>\n<p>If <span class=\"application\">pg_rewind</span> fails while processing, then the data folder of the target is likely not in a state that can be recovered. In such a case, taking a new fresh backup is recommended.</p>\n<p>As <span class=\"application\">pg_rewind</span> copies configuration files entirely from the source, it may be required to correct the configuration used for recovery before restarting the target server, especially if the target is reintroduced as a standby of the source. If you restart the server after the rewind operation has finished but without configuring recovery, the target may again diverge from the primary.</p>\n<p><span class=\"application\">pg_rewind</span> will fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates. If such files are present on the target server it is recommended to remove them before running <span class=\"application\">pg_rewind</span>. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind.</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.6\">\n<h2>Options</h2>\n<p><span class=\"application\">pg_rewind</span> accepts the following command-line arguments:</p>\n<div class=\"variablelist\">\n<dl class=\"variablelist\">\n<dt><span class=\"term\"><code class=\"option\">-D <em class=\"replaceable\"><code>directory</code></em></code><br></span><span class=\"term\"><code class=\"option\">--target-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running <span class=\"application\">pg_rewind</span></p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code></span></dt>\n<dd>\n<p>Specifies a libpq connection string to connect to the source <span class=\"productname\">PostgreSQL</span> server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-R</code><br></span><span class=\"term\"><code class=\"option\">--write-recovery-conf</code></span></dt>\n<dd>\n<p>Create <code class=\"filename\">standby.signal</code> and append connection settings to <code class=\"filename\">postgresql.auto.conf</code> in the output directory. <code class=\"literal\">--source-server</code> is mandatory with this option.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-n</code><br></span><span class=\"term\"><code class=\"option\">--dry-run</code></span></dt>\n<dd>\n<p>Do everything except actually modifying the target directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-N</code><br></span><span class=\"term\"><code class=\"option\">--no-sync</code></span></dt>\n<dd>\n<p>By default, <code class=\"command\">pg_rewind</code> will wait for all files to be written safely to disk. This option causes <code class=\"command\">pg_rewind</code> to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-P</code><br></span><span class=\"term\"><code class=\"option\">--progress</code></span></dt>\n<dd>\n<p>Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-c</code><br></span><span class=\"term\"><code class=\"option\">--restore-target-wal</code></span></dt>\n<dd>\n<p>Use <code class=\"varname\">restore_command</code> defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the <code class=\"filename\">pg_wal</code> directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--config-file=<em class=\"replaceable\"><code>filename</code></em></code></span></dt>\n<dd>\n<p>Use the specified main server configuration file for the target cluster. This affects <span class=\"application\">pg_rewind</span> when it uses internally the <span class=\"application\">postgres</span> command for the rewind operation on this cluster (when retrieving <code class=\"varname\">restore_command</code> with the option <code class=\"option\">-c/--restore-target-wal</code> and when forcing a completion of crash recovery).</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--debug</code></span></dt>\n<dd>\n<p>Print verbose debugging output that is mostly useful for developers debugging <span class=\"application\">pg_rewind</span>.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--no-ensure-shutdown</code></span></dt>\n<dd>\n<p><span class=\"application\">pg_rewind</span> requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, <span class=\"application\">pg_rewind</span> starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, <span class=\"application\">pg_rewind</span> skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-V</code><br></span><span class=\"term\"><code class=\"option\">--version</code></span></dt>\n<dd>\n<p>Display version information, then exit.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-?</code><br></span><span class=\"term\"><code class=\"option\">--help</code></span></dt>\n<dd>\n<p>Show help, then exit.</p>\n</dd>\n</dl>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.7\">\n<h2>Environment</h2>\n<p>When <code class=\"option\">--source-server</code> option is used, <span class=\"application\">pg_rewind</span> also uses the environment variables supported by <span class=\"application\">libpq</span> (see <a class=\"xref\" href=\"/docs/16/libpq-envars.html\" title=\"34.15.\u00a0Environment Variables\">Section\u00a034.15</a>).</p>\n<p>The environment variable <code class=\"envar\">PG_COLOR</code> specifies whether to use color in diagnostic messages. Possible values are <code class=\"literal\">always</code>, <code class=\"literal\">auto</code> and <code class=\"literal\">never</code>.</p>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.9.8\">\n<h2>Notes</h2>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source, a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source cluster can be used instead of a superuser. Here is how to create such a role, named <code class=\"literal\">rewind_user</code> here:</p>\n<pre class=\"programlisting\">CREATE USER rewind_user LOGIN;\nGRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;\n</pre>\n<div class=\"refsect2\" id=\"id-1.9.5.9.8.3\">\n<h3>How It Works</h3>\n<p>The basic idea is to copy all file system-level changes from the source cluster to the target cluster:</p>\n<div class=\"procedure\">\n<ol class=\"procedure\">\n<li class=\"step\">\n<p>Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's timeline history forked off from the target cluster. For each WAL record, record each data block that was touched. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off. If some of the WAL files are no longer available, try re-running <span class=\"application\">pg_rewind</span> with the <code class=\"option\">-c</code> option to search for the missing files in the WAL archive.</p>\n</li>\n<li class=\"step\">\n<p>Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (<code class=\"option\">--source-pgdata</code>) or SQL (<code class=\"option\">--source-server</code>). Relation files are now in a state equivalent to the moment of the last completed checkpoint prior to the point at which the WAL timelines of the source and target diverged plus the current state on the source of any blocks changed on the target after that divergence.</p>\n</li>\n<li class=\"step\">\n<p>Copy all other files, including new relation files, WAL segments, <code class=\"filename\">pg_xact</code>, and configuration files from the source cluster to the target cluster. Similarly to base backups, the contents of the directories <code class=\"filename\">pg_dynshmem/</code>, <code class=\"filename\">pg_notify/</code>, <code class=\"filename\">pg_replslot/</code>, <code class=\"filename\">pg_serial/</code>, <code class=\"filename\">pg_snapshots/</code>, <code class=\"filename\">pg_stat_tmp/</code>, and <code class=\"filename\">pg_subtrans/</code> are omitted from the data copied from the source cluster. The files <code class=\"filename\">backup_label</code>, <code class=\"filename\">tablespace_map</code>, <code class=\"filename\">pg_internal.init</code>, <code class=\"filename\">postmaster.opts</code>, <code class=\"filename\">postmaster.pid</code> and <code class=\"filename\">.DS_Store</code> as well as any file or directory beginning with <code class=\"filename\">pgsql_tmp</code>, are omitted.</p>\n</li>\n<li class=\"step\">\n<p>Create a <code class=\"filename\">backup_label</code> file to begin WAL replay at the checkpoint created at failover and configure the <code class=\"filename\">pg_control</code> file with a minimum consistency LSN defined as the result of <code class=\"literal\">pg_current_wal_insert_lsn()</code> when rewinding from a live source or the last checkpoint LSN when rewinding from a stopped source.</p>\n</li>\n<li class=\"step\">\n<p>When starting the target, <span class=\"productname\">PostgreSQL</span> replays all the required WAL, resulting in a data directory in a consistent state.</p>\n</li>\n</ol>\n</div>\n</div>\n</div>\n</div></div>", "manual_path": "app-pgrewind.html", "comparison_data": {"options": [{"names": ["-D directory", "--target-pgdata= directory"], "signature": "-D directory --target-pgdata= directory", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "signature": "--source-pgdata= directory", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "signature": "--source-server= connstr", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "signature": "-R --write-recovery-conf", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "signature": "-n --dry-run", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "signature": "-N --no-sync", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "signature": "-P --progress", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "signature": "-c --restore-target-wal", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--config-file= filename"], "signature": "--config-file= filename", "description": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery)."}, {"names": ["--debug"], "signature": "--debug", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "signature": "--no-ensure-shutdown", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["-V", "--version"], "signature": "-V --version", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "signature": "-? --help", "description": "Show help, then exit."}], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "environment": []}, "comparison_hash": "f912a281dbc53bb5c56ffa693dfd803e6e1f643eb7d467ea448bc9f4cf3e087e"}, "17": {"facts": [{"label": "Documented executable", "value": "pg_rewind"}, {"label": "Executable version", "value": "17.11"}, {"label": "Reference inventory", "value": "Server applications"}, {"label": "Option definition groups", "value": "14"}], "tables": [{"key": "options", "rows": [{"summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": {"url": "/docs/17/app-pgrewind.html", "text": "-D directory --target-pgdata= directory"}}, {"summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": {"url": "/docs/17/app-pgrewind.html", "text": "--source-pgdata= directory"}}, {"summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": {"url": "/docs/17/app-pgrewind.html", "text": "--source-server= connstr"}}, {"summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option.", "signature": {"url": "/docs/17/app-pgrewind.html", "text": "-R --write-recovery-conf"}}, {"summary": "Do everything except actually modifying the target directory.", "signature": {"url": "/docs/17/app-pgrewind.html", "text": "-n --dry-run"}}, {"summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": {"url": "/docs/17/app-pgrewind.html", "text": "-N --no-sync"}}, {"summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": {"url": "/docs/17/app-pgrewind.html", "text": "-P --progress"}}, {"summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": {"url": "/docs/17/app-pgrewind.html", "text": "-c --restore-target-wal"}}, {"summary": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery).", "signature": {"url": "/docs/17/app-pgrewind.html", "text": "--config-file= filename"}}, {"summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": {"url": "/docs/17/app-pgrewind.html", "text": "--debug"}}, {"summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": {"url": "/docs/17/app-pgrewind.html", "text": "--no-ensure-shutdown"}}, {"summary": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.", "signature": {"url": "/docs/17/app-pgrewind.html", "text": "--sync-method= method"}}, {"summary": "Display version information, then exit.", "signature": {"url": "/docs/17/app-pgrewind.html", "text": "-V --version"}}, {"summary": "Show help, then exit.", "signature": {"url": "/docs/17/app-pgrewind.html", "text": "-? --help"}}], "title": "Documented options", "columns": [{"key": "signature", "label": "Option and arguments"}, {"key": "summary", "label": "Description"}]}], "options": [{"names": ["-D directory", "--target-pgdata= directory"], "summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": "-D directory --target-pgdata= directory", "source_url": "/docs/17/app-pgrewind.html", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": "--source-pgdata= directory", "source_url": "/docs/17/app-pgrewind.html", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": "--source-server= connstr", "source_url": "/docs/17/app-pgrewind.html", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option.", "signature": "-R --write-recovery-conf", "source_url": "/docs/17/app-pgrewind.html", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "summary": "Do everything except actually modifying the target directory.", "signature": "-n --dry-run", "source_url": "/docs/17/app-pgrewind.html", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": "-N --no-sync", "source_url": "/docs/17/app-pgrewind.html", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": "-P --progress", "source_url": "/docs/17/app-pgrewind.html", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": "-c --restore-target-wal", "source_url": "/docs/17/app-pgrewind.html", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--config-file= filename"], "summary": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery).", "signature": "--config-file= filename", "source_url": "/docs/17/app-pgrewind.html", "description": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery)."}, {"names": ["--debug"], "summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": "--debug", "source_url": "/docs/17/app-pgrewind.html", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": "--no-ensure-shutdown", "source_url": "/docs/17/app-pgrewind.html", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["--sync-method= method"], "summary": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.", "signature": "--sync-method= method", "source_url": "/docs/17/app-pgrewind.html", "description": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace. On Linux, syncfs may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See recovery_init_sync_method for information about the caveats to be aware of when using syncfs . This option has no effect when --no-sync is used."}, {"names": ["-V", "--version"], "summary": "Display version information, then exit.", "signature": "-V --version", "source_url": "/docs/17/app-pgrewind.html", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "summary": "Show help, then exit.", "signature": "-? --help", "source_url": "/docs/17/app-pgrewind.html", "description": "Show help, then exit."}], "related": [], "release": {"ref": "https://ftp.postgresql.org/pub/source/v17.11/postgresql-17.11.tar.bz2", "label": "17.11", "major": "17", "channel": "stable", "manifest": {"pdf": {"A4": {"url": "/files/documentation/pdf/17/postgresql-17-A4.pdf", "bytes": 15521293, "pages": 3099, "sha256": "1991354df0dc89e70ec39328c28988ef8b19c6a93671dab3893650b63e9f4e36", "built_at": "2026-09-26"}, "US": {"url": "/files/documentation/pdf/17/postgresql-17-US.pdf", "bytes": 15398150, "pages": 3270, "sha256": "07696c8f38abf31babf22d2db337093936e7c472d2af36d050b000c49bbcf52c", "built_at": "2026-09-26"}}, "tree": "17", "index": "index.html", "major": "17", "pages": 1143, "release": "17.11", "source_url": "https://ftp.postgresql.org/pub/source/v17.11/postgresql-17.11.tar.bz2", "svg_assets": 3, "source_mode": "en SGML built with pinned official archive", "source_sha256": "dd27f2b3c59e73ed14aa3324901242bf69a032a6347805f274e6260322d42979"}, "revision": "58419c9b0dd42cb34c8d53695bb025a7e582edf55ccd4c5bcb1c2c7c71a37487", "evidence_kind": "English manual and source declarations", "source_sha256": "dd27f2b3c59e73ed14aa3324901242bf69a032a6347805f274e6260322d42979"}, "sources": [{"url": "/docs/17/app-pgrewind.html", "file": "app-pgrewind.html", "label": "17.11 English manual \u00b7 app-pgrewind.html", "anchor": "", "sha256": "96988c7be34a3d3715b61d80b5421a3cb7d156bf4cf70f9590f4bf035adc64d1"}, {"url": "/docs/17/reference-server.html", "file": "reference-server.html", "label": "Server applications inventory", "anchor": "", "sha256": "01f07993ff020684f4c290d17cdafa26fb17ea5c1d476bc3a5efc68bf8ddd9b9"}], "sections": [], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "signature": "pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }", "description": ["pg_rewind \u2014 synchronize a PostgreSQL data directory with another data directory that was forked from it"], "environment": [], "manual_html": "<div><div class=\"refentry\" id=\"APP-PGREWIND\">\n<div class=\"titlepage\"></div>\n<div class=\"refnamediv\">\n<h2><span class=\"refentrytitle\"><span class=\"application\">pg_rewind</span></span></h2>\n<p>pg_rewind \u2014 synchronize a <span class=\"productname\">PostgreSQL</span> data directory with another data directory that was forked from it</p>\n</div>\n<div class=\"refsynopsisdiv\">\n<h2>Synopsis</h2>\n<div class=\"cmdsynopsis\">\n<p id=\"id-1.9.5.10.4.1\"><code class=\"command\">pg_rewind</code> [<em class=\"replaceable\"><code>option</code></em>...] { <code class=\"option\">-D</code> | <code class=\"option\">--target-pgdata</code> } <em class=\"replaceable\"><code>directory</code></em> { <code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code> | <code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code> }</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.5\">\n<h2>Description</h2>\n<p><span class=\"application\">pg_rewind</span> is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old primary server back online after failover as a standby that follows the new primary.</p>\n<p>After a successful rewind, the state of the target data directory is analogous to a base backup of the source data directory. Unlike taking a new base backup or using a tool like <span class=\"application\">rsync</span>, <span class=\"application\">pg_rewind</span> does not require comparing or copying unchanged relation blocks in the cluster. Only changed blocks from existing relation files are copied; all other files, including new relation files, configuration files, and WAL segments, are copied in full. As such the rewind operation is significantly faster than other approaches when the database is large and only a small fraction of blocks differ between the clusters.</p>\n<p><span class=\"application\">pg_rewind</span> examines the timeline histories of the source and target clusters to determine the point where they diverged, and expects to find WAL in the target cluster's <code class=\"filename\">pg_wal</code> directory reaching all the way back to the point of divergence. The point of divergence can be found either on the target timeline, the source timeline, or their common ancestor. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem, but if the target cluster ran for a long time after the divergence, its old WAL files might no longer be present. In this case, you can manually copy them from the WAL archive to the <code class=\"filename\">pg_wal</code> directory, or run <span class=\"application\">pg_rewind</span> with the <code class=\"literal\">-c</code> option to automatically retrieve them from the WAL archive. The use of <span class=\"application\">pg_rewind</span> is not limited to failover, e.g., a standby server can be promoted, run some write transactions, and then rewound to become a standby again.</p>\n<p>After running <span class=\"application\">pg_rewind</span>, WAL replay needs to complete for the data directory to be in a consistent state. When the target server is started again it will enter archive recovery and replay all WAL generated in the source server from the last checkpoint before the point of divergence. If some of the WAL was no longer available in the source server when <span class=\"application\">pg_rewind</span> was run, and therefore could not be copied by the <span class=\"application\">pg_rewind</span> session, it must be made available when the target server is started. This can be done by creating a <code class=\"filename\">recovery.signal</code> file in the target data directory and by configuring a suitable <a class=\"xref\" href=\"/docs/17/runtime-config-wal.html#GUC-RESTORE-COMMAND\">restore_command</a> in <code class=\"filename\">postgresql.conf</code>.</p>\n<p><span class=\"application\">pg_rewind</span> requires that the target server either has the <a class=\"xref\" href=\"/docs/17/runtime-config-wal.html#GUC-WAL-LOG-HINTS\">wal_log_hints</a> option enabled in <code class=\"filename\">postgresql.conf</code> or data checksums enabled when the cluster was initialized with <span class=\"application\">initdb</span>. Neither of these are currently on by default. <a class=\"xref\" href=\"/docs/17/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\">full_page_writes</a> must also be set to <code class=\"literal\">on</code>, but is enabled by default.</p>\n<div class=\"warning\">\n<h3 class=\"title\">Warning: Failures While Rewinding</h3>\n<p>If <span class=\"application\">pg_rewind</span> fails while processing, then the data folder of the target is likely not in a state that can be recovered. In such a case, taking a new fresh backup is recommended.</p>\n<p>As <span class=\"application\">pg_rewind</span> copies configuration files entirely from the source, it may be required to correct the configuration used for recovery before restarting the target server, especially if the target is reintroduced as a standby of the source. If you restart the server after the rewind operation has finished but without configuring recovery, the target may again diverge from the primary.</p>\n<p><span class=\"application\">pg_rewind</span> will fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates. If such files are present on the target server it is recommended to remove them before running <span class=\"application\">pg_rewind</span>. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind.</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.6\">\n<h2>Options</h2>\n<p><span class=\"application\">pg_rewind</span> accepts the following command-line arguments:</p>\n<div class=\"variablelist\">\n<dl class=\"variablelist\">\n<dt><span class=\"term\"><code class=\"option\">-D <em class=\"replaceable\"><code>directory</code></em></code><br></span><span class=\"term\"><code class=\"option\">--target-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running <span class=\"application\">pg_rewind</span></p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code></span></dt>\n<dd>\n<p>Specifies a libpq connection string to connect to the source <span class=\"productname\">PostgreSQL</span> server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-R</code><br></span><span class=\"term\"><code class=\"option\">--write-recovery-conf</code></span></dt>\n<dd>\n<p>Create <code class=\"filename\">standby.signal</code> and append connection settings to <code class=\"filename\">postgresql.auto.conf</code> in the output directory. <code class=\"literal\">--source-server</code> is mandatory with this option.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-n</code><br></span><span class=\"term\"><code class=\"option\">--dry-run</code></span></dt>\n<dd>\n<p>Do everything except actually modifying the target directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-N</code><br></span><span class=\"term\"><code class=\"option\">--no-sync</code></span></dt>\n<dd>\n<p>By default, <code class=\"command\">pg_rewind</code> will wait for all files to be written safely to disk. This option causes <code class=\"command\">pg_rewind</code> to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-P</code><br></span><span class=\"term\"><code class=\"option\">--progress</code></span></dt>\n<dd>\n<p>Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-c</code><br></span><span class=\"term\"><code class=\"option\">--restore-target-wal</code></span></dt>\n<dd>\n<p>Use <code class=\"varname\">restore_command</code> defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the <code class=\"filename\">pg_wal</code> directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--config-file=<em class=\"replaceable\"><code>filename</code></em></code></span></dt>\n<dd>\n<p>Use the specified main server configuration file for the target cluster. This affects <span class=\"application\">pg_rewind</span> when it uses internally the <span class=\"application\">postgres</span> command for the rewind operation on this cluster (when retrieving <code class=\"varname\">restore_command</code> with the option <code class=\"option\">-c/--restore-target-wal</code> and when forcing a completion of crash recovery).</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--debug</code></span></dt>\n<dd>\n<p>Print verbose debugging output that is mostly useful for developers debugging <span class=\"application\">pg_rewind</span>.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--no-ensure-shutdown</code></span></dt>\n<dd>\n<p><span class=\"application\">pg_rewind</span> requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, <span class=\"application\">pg_rewind</span> starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, <span class=\"application\">pg_rewind</span> skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--sync-method=<em class=\"replaceable\"><code>method</code></em></code></span></dt>\n<dd>\n<p>When set to <code class=\"literal\">fsync</code>, which is the default, <code class=\"command\">pg_rewind</code> will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.</p>\n<p>On Linux, <code class=\"literal\">syncfs</code> may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See <a class=\"xref\" href=\"/docs/17/runtime-config-error-handling.html#GUC-RECOVERY-INIT-SYNC-METHOD\">recovery_init_sync_method</a> for information about the caveats to be aware of when using <code class=\"literal\">syncfs</code>.</p>\n<p>This option has no effect when <code class=\"option\">--no-sync</code> is used.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-V</code><br></span><span class=\"term\"><code class=\"option\">--version</code></span></dt>\n<dd>\n<p>Display version information, then exit.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-?</code><br></span><span class=\"term\"><code class=\"option\">--help</code></span></dt>\n<dd>\n<p>Show help, then exit.</p>\n</dd>\n</dl>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.7\">\n<h2>Environment</h2>\n<p>When <code class=\"option\">--source-server</code> option is used, <span class=\"application\">pg_rewind</span> also uses the environment variables supported by <span class=\"application\">libpq</span> (see <a class=\"xref\" href=\"/docs/17/libpq-envars.html\" title=\"32.15.\u00a0Environment Variables\">Section\u00a032.15</a>).</p>\n<p>The environment variable <code class=\"envar\">PG_COLOR</code> specifies whether to use color in diagnostic messages. Possible values are <code class=\"literal\">always</code>, <code class=\"literal\">auto</code> and <code class=\"literal\">never</code>.</p>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.8\">\n<h2>Notes</h2>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source, a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source cluster can be used instead of a superuser. Here is how to create such a role, named <code class=\"literal\">rewind_user</code> here:</p>\n<pre class=\"programlisting\">CREATE USER rewind_user LOGIN;\nGRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;\n</pre>\n<div class=\"refsect2\" id=\"id-1.9.5.10.8.3\">\n<h3>How It Works</h3>\n<p>The basic idea is to copy all file system-level changes from the source cluster to the target cluster:</p>\n<div class=\"procedure\">\n<ol class=\"procedure\">\n<li class=\"step\">\n<p>Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's timeline history forked off from the target cluster. For each WAL record, record each data block that was touched. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off. If some of the WAL files are no longer available, try re-running <span class=\"application\">pg_rewind</span> with the <code class=\"option\">-c</code> option to search for the missing files in the WAL archive.</p>\n</li>\n<li class=\"step\">\n<p>Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (<code class=\"option\">--source-pgdata</code>) or SQL (<code class=\"option\">--source-server</code>). Relation files are now in a state equivalent to the moment of the last completed checkpoint prior to the point at which the WAL timelines of the source and target diverged plus the current state on the source of any blocks changed on the target after that divergence.</p>\n</li>\n<li class=\"step\">\n<p>Copy all other files, including new relation files, WAL segments, <code class=\"filename\">pg_xact</code>, and configuration files from the source cluster to the target cluster. Similarly to base backups, the contents of the directories <code class=\"filename\">pg_dynshmem/</code>, <code class=\"filename\">pg_notify/</code>, <code class=\"filename\">pg_replslot/</code>, <code class=\"filename\">pg_serial/</code>, <code class=\"filename\">pg_snapshots/</code>, <code class=\"filename\">pg_stat_tmp/</code>, and <code class=\"filename\">pg_subtrans/</code> are omitted from the data copied from the source cluster. The files <code class=\"filename\">backup_label</code>, <code class=\"filename\">tablespace_map</code>, <code class=\"filename\">pg_internal.init</code>, <code class=\"filename\">postmaster.opts</code>, <code class=\"filename\">postmaster.pid</code> and <code class=\"filename\">.DS_Store</code> as well as any file or directory beginning with <code class=\"filename\">pgsql_tmp</code>, are omitted.</p>\n</li>\n<li class=\"step\">\n<p>Create a <code class=\"filename\">backup_label</code> file to begin WAL replay at the checkpoint created at failover and configure the <code class=\"filename\">pg_control</code> file with a minimum consistency LSN defined as the result of <code class=\"literal\">pg_current_wal_insert_lsn()</code> when rewinding from a live source or the last checkpoint LSN when rewinding from a stopped source.</p>\n</li>\n<li class=\"step\">\n<p>When starting the target, <span class=\"productname\">PostgreSQL</span> replays all the required WAL, resulting in a data directory in a consistent state.</p>\n</li>\n</ol>\n</div>\n</div>\n</div>\n</div></div>", "manual_path": "app-pgrewind.html", "comparison_data": {"options": [{"names": ["-D directory", "--target-pgdata= directory"], "signature": "-D directory --target-pgdata= directory", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "signature": "--source-pgdata= directory", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "signature": "--source-server= connstr", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "signature": "-R --write-recovery-conf", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "signature": "-n --dry-run", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "signature": "-N --no-sync", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "signature": "-P --progress", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "signature": "-c --restore-target-wal", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--config-file= filename"], "signature": "--config-file= filename", "description": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery)."}, {"names": ["--debug"], "signature": "--debug", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "signature": "--no-ensure-shutdown", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["--sync-method= method"], "signature": "--sync-method= method", "description": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace. On Linux, syncfs may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See recovery_init_sync_method for information about the caveats to be aware of when using syncfs . This option has no effect when --no-sync is used."}, {"names": ["-V", "--version"], "signature": "-V --version", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "signature": "-? --help", "description": "Show help, then exit."}], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "environment": []}, "comparison_hash": "f13b20dd32c6f912b385ec025e611a3df4c9b924fe06a2effd6ad38d0f13f295"}, "18": {"facts": [{"label": "Documented executable", "value": "pg_rewind"}, {"label": "Executable version", "value": "18.6"}, {"label": "Reference inventory", "value": "Server applications"}, {"label": "Option definition groups", "value": "14"}], "tables": [{"key": "options", "rows": [{"summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-D directory --target-pgdata= directory"}}, {"summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "--source-pgdata= directory"}}, {"summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "--source-server= connstr"}}, {"summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-R --write-recovery-conf"}}, {"summary": "Do everything except actually modifying the target directory.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-n --dry-run"}}, {"summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-N --no-sync"}}, {"summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-P --progress"}}, {"summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-c --restore-target-wal"}}, {"summary": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery).", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "--config-file= filename"}}, {"summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "--debug"}}, {"summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "--no-ensure-shutdown"}}, {"summary": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "--sync-method= method"}}, {"summary": "Display version information, then exit.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-V --version"}}, {"summary": "Show help, then exit.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-? --help"}}], "title": "Documented options", "columns": [{"key": "signature", "label": "Option and arguments"}, {"key": "summary", "label": "Description"}]}], "options": [{"names": ["-D directory", "--target-pgdata= directory"], "summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": "-D directory --target-pgdata= directory", "source_url": "/docs/18/app-pgrewind.html", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": "--source-pgdata= directory", "source_url": "/docs/18/app-pgrewind.html", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": "--source-server= connstr", "source_url": "/docs/18/app-pgrewind.html", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option.", "signature": "-R --write-recovery-conf", "source_url": "/docs/18/app-pgrewind.html", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "summary": "Do everything except actually modifying the target directory.", "signature": "-n --dry-run", "source_url": "/docs/18/app-pgrewind.html", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": "-N --no-sync", "source_url": "/docs/18/app-pgrewind.html", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": "-P --progress", "source_url": "/docs/18/app-pgrewind.html", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": "-c --restore-target-wal", "source_url": "/docs/18/app-pgrewind.html", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--config-file= filename"], "summary": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery).", "signature": "--config-file= filename", "source_url": "/docs/18/app-pgrewind.html", "description": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery)."}, {"names": ["--debug"], "summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": "--debug", "source_url": "/docs/18/app-pgrewind.html", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": "--no-ensure-shutdown", "source_url": "/docs/18/app-pgrewind.html", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["--sync-method= method"], "summary": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.", "signature": "--sync-method= method", "source_url": "/docs/18/app-pgrewind.html", "description": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace. On Linux, syncfs may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See recovery_init_sync_method for information about the caveats to be aware of when using syncfs . This option has no effect when --no-sync is used."}, {"names": ["-V", "--version"], "summary": "Display version information, then exit.", "signature": "-V --version", "source_url": "/docs/18/app-pgrewind.html", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "summary": "Show help, then exit.", "signature": "-? --help", "source_url": "/docs/18/app-pgrewind.html", "description": "Show help, then exit."}], "related": [], "release": {"ref": "https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2", "label": "18.6", "major": "18", "channel": "stable", "manifest": {"pdf": {"A4": {"url": "/files/documentation/pdf/18/postgresql-18-A4.pdf", "bytes": 15865106, "pages": 3154, "sha256": "19512c405da53f9f7fcf0abba359223aa65f021be025bf3411381918f92e3190", "built_at": "2026-09-26"}, "US": {"url": "/files/documentation/pdf/18/postgresql-18-US.pdf", "bytes": 15748059, "pages": 3328, "sha256": "facbe6c229e598b872d3d98bef53308f46e06746006fa4590de9a7de9dd46319", "built_at": "2026-09-26"}}, "tree": "18", "index": "index.html", "major": "18", "pages": 1148, "release": "18.6", "source_url": "https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2", "svg_assets": 3, "source_mode": "en SGML built with pinned official archive", "source_sha256": "555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f"}, "revision": "ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8", "evidence_kind": "English manual and source declarations", "source_sha256": "555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f"}, "sources": [{"url": "/docs/18/app-pgrewind.html", "file": "app-pgrewind.html", "label": "18.6 English manual \u00b7 app-pgrewind.html", "anchor": "", "sha256": "2d8a13348838dfc44e3733f7e269f3fb35643d705686198ab09cc5bada9a1330"}, {"url": "/docs/18/reference-server.html", "file": "reference-server.html", "label": "Server applications inventory", "anchor": "", "sha256": "e9f8bbcc8e3641fadd1d20991e8aad6b532155b786ba5a1b7b364ec4f1a5bef3"}], "sections": [], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "signature": "pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }", "description": ["pg_rewind \u2014 synchronize a PostgreSQL data directory with another data directory that was forked from it"], "environment": [], "manual_html": "<div><div class=\"refentry\" id=\"APP-PGREWIND\">\n<div class=\"titlepage\"></div>\n<div class=\"refnamediv\">\n<h2><span class=\"refentrytitle\"><span class=\"application\">pg_rewind</span></span></h2>\n<p>pg_rewind \u2014 synchronize a <span class=\"productname\">PostgreSQL</span> data directory with another data directory that was forked from it</p>\n</div>\n<div class=\"refsynopsisdiv\">\n<h2>Synopsis</h2>\n<div class=\"cmdsynopsis\">\n<p id=\"id-1.9.5.10.4.1\"><code class=\"command\">pg_rewind</code> [<em class=\"replaceable\"><code>option</code></em>...] { <code class=\"option\">-D</code> | <code class=\"option\">--target-pgdata</code> } <em class=\"replaceable\"><code>directory</code></em> { <code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code> | <code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code> }</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.5\">\n<h2>Description</h2>\n<p><span class=\"application\">pg_rewind</span> is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old primary server back online after failover as a standby that follows the new primary.</p>\n<p>After a successful rewind, the state of the target data directory is analogous to a base backup of the source data directory. Unlike taking a new base backup or using a tool like <span class=\"application\">rsync</span>, <span class=\"application\">pg_rewind</span> does not require comparing or copying unchanged relation blocks in the cluster. Only changed blocks from existing relation files are copied; all other files, including new relation files, configuration files, and WAL segments, are copied in full. As such the rewind operation is significantly faster than other approaches when the database is large and only a small fraction of blocks differ between the clusters.</p>\n<p><span class=\"application\">pg_rewind</span> examines the timeline histories of the source and target clusters to determine the point where they diverged, and expects to find WAL in the target cluster's <code class=\"filename\">pg_wal</code> directory reaching all the way back to the point of divergence. The point of divergence can be found either on the target timeline, the source timeline, or their common ancestor. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem, but if the target cluster ran for a long time after the divergence, its old WAL files might no longer be present. In this case, you can manually copy them from the WAL archive to the <code class=\"filename\">pg_wal</code> directory, or run <span class=\"application\">pg_rewind</span> with the <code class=\"literal\">-c</code> option to automatically retrieve them from the WAL archive. The use of <span class=\"application\">pg_rewind</span> is not limited to failover, e.g., a standby server can be promoted, run some write transactions, and then rewound to become a standby again.</p>\n<p>After running <span class=\"application\">pg_rewind</span>, WAL replay needs to complete for the data directory to be in a consistent state. When the target server is started again it will enter archive recovery and replay all WAL generated in the source server from the last checkpoint before the point of divergence. If some of the WAL was no longer available in the source server when <span class=\"application\">pg_rewind</span> was run, and therefore could not be copied by the <span class=\"application\">pg_rewind</span> session, it must be made available when the target server is started. This can be done by creating a <code class=\"filename\">recovery.signal</code> file in the target data directory and by configuring a suitable <a class=\"xref\" href=\"/docs/18/runtime-config-wal.html#GUC-RESTORE-COMMAND\">restore_command</a> in <code class=\"filename\">postgresql.conf</code>.</p>\n<p><span class=\"application\">pg_rewind</span> requires that the target server either has the <a class=\"xref\" href=\"/docs/18/runtime-config-wal.html#GUC-WAL-LOG-HINTS\">wal_log_hints</a> option enabled in <code class=\"filename\">postgresql.conf</code> or data checksums enabled when the cluster was initialized with <span class=\"application\">initdb</span> (the default). <a class=\"xref\" href=\"/docs/18/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\">full_page_writes</a> must also be set to <code class=\"literal\">on</code>, but is enabled by default.</p>\n<div class=\"warning\">\n<h3 class=\"title\">Warning: Failures While Rewinding</h3>\n<p>If <span class=\"application\">pg_rewind</span> fails while processing, then the data folder of the target is likely not in a state that can be recovered. In such a case, taking a new fresh backup is recommended.</p>\n<p>As <span class=\"application\">pg_rewind</span> copies configuration files entirely from the source, it may be required to correct the configuration used for recovery before restarting the target server, especially if the target is reintroduced as a standby of the source. If you restart the server after the rewind operation has finished but without configuring recovery, the target may again diverge from the primary.</p>\n<p><span class=\"application\">pg_rewind</span> will fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates. If such files are present on the target server it is recommended to remove them before running <span class=\"application\">pg_rewind</span>. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind.</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.6\">\n<h2>Options</h2>\n<p><span class=\"application\">pg_rewind</span> accepts the following command-line arguments:</p>\n<div class=\"variablelist\">\n<dl class=\"variablelist\">\n<dt><span class=\"term\"><code class=\"option\">-D <em class=\"replaceable\"><code>directory</code></em></code><br></span><span class=\"term\"><code class=\"option\">--target-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running <span class=\"application\">pg_rewind</span></p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code></span></dt>\n<dd>\n<p>Specifies a libpq connection string to connect to the source <span class=\"productname\">PostgreSQL</span> server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-R</code><br></span><span class=\"term\"><code class=\"option\">--write-recovery-conf</code></span></dt>\n<dd>\n<p>Create <code class=\"filename\">standby.signal</code> and append connection settings to <code class=\"filename\">postgresql.auto.conf</code> in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or <a class=\"link\" href=\"/docs/18/libpq-envars.html\" title=\"32.15.\u00a0Environment Variables\">environment variable</a>. <code class=\"literal\">--source-server</code> is mandatory with this option.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-n</code><br></span><span class=\"term\"><code class=\"option\">--dry-run</code></span></dt>\n<dd>\n<p>Do everything except actually modifying the target directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-N</code><br></span><span class=\"term\"><code class=\"option\">--no-sync</code></span></dt>\n<dd>\n<p>By default, <code class=\"command\">pg_rewind</code> will wait for all files to be written safely to disk. This option causes <code class=\"command\">pg_rewind</code> to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-P</code><br></span><span class=\"term\"><code class=\"option\">--progress</code></span></dt>\n<dd>\n<p>Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-c</code><br></span><span class=\"term\"><code class=\"option\">--restore-target-wal</code></span></dt>\n<dd>\n<p>Use <code class=\"varname\">restore_command</code> defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the <code class=\"filename\">pg_wal</code> directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--config-file=<em class=\"replaceable\"><code>filename</code></em></code></span></dt>\n<dd>\n<p>Use the specified main server configuration file for the target cluster. This affects <span class=\"application\">pg_rewind</span> when it uses internally the <span class=\"application\">postgres</span> command for the rewind operation on this cluster (when retrieving <code class=\"varname\">restore_command</code> with the option <code class=\"option\">-c/--restore-target-wal</code> and when forcing a completion of crash recovery).</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--debug</code></span></dt>\n<dd>\n<p>Print verbose debugging output that is mostly useful for developers debugging <span class=\"application\">pg_rewind</span>.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--no-ensure-shutdown</code></span></dt>\n<dd>\n<p><span class=\"application\">pg_rewind</span> requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, <span class=\"application\">pg_rewind</span> starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, <span class=\"application\">pg_rewind</span> skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--sync-method=<em class=\"replaceable\"><code>method</code></em></code></span></dt>\n<dd>\n<p>When set to <code class=\"literal\">fsync</code>, which is the default, <code class=\"command\">pg_rewind</code> will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.</p>\n<p>On Linux, <code class=\"literal\">syncfs</code> may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See <a class=\"xref\" href=\"/docs/18/runtime-config-error-handling.html#GUC-RECOVERY-INIT-SYNC-METHOD\">recovery_init_sync_method</a> for information about the caveats to be aware of when using <code class=\"literal\">syncfs</code>.</p>\n<p>This option has no effect when <code class=\"option\">--no-sync</code> is used.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-V</code><br></span><span class=\"term\"><code class=\"option\">--version</code></span></dt>\n<dd>\n<p>Display version information, then exit.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-?</code><br></span><span class=\"term\"><code class=\"option\">--help</code></span></dt>\n<dd>\n<p>Show help, then exit.</p>\n</dd>\n</dl>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.7\">\n<h2>Environment</h2>\n<p>When <code class=\"option\">--source-server</code> option is used, <span class=\"application\">pg_rewind</span> also uses the environment variables supported by <span class=\"application\">libpq</span> (see <a class=\"xref\" href=\"/docs/18/libpq-envars.html\" title=\"32.15.\u00a0Environment Variables\">Section\u00a032.15</a>).</p>\n<p>The environment variable <code class=\"envar\">PG_COLOR</code> specifies whether to use color in diagnostic messages. Possible values are <code class=\"literal\">always</code>, <code class=\"literal\">auto</code> and <code class=\"literal\">never</code>.</p>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.8\">\n<h2>Notes</h2>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source, a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source cluster can be used instead of a superuser. Here is how to create such a role, named <code class=\"literal\">rewind_user</code> here:</p>\n<pre class=\"programlisting\">CREATE USER rewind_user LOGIN;\nGRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;\n</pre>\n<div class=\"refsect2\" id=\"id-1.9.5.10.8.3\">\n<h3>How It Works</h3>\n<p>The basic idea is to copy all file system-level changes from the source cluster to the target cluster:</p>\n<div class=\"procedure\">\n<ol class=\"procedure\">\n<li class=\"step\">\n<p>Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's timeline history forked off from the target cluster. For each WAL record, record each data block that was touched. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off. If some of the WAL files are no longer available, try re-running <span class=\"application\">pg_rewind</span> with the <code class=\"option\">-c</code> option to search for the missing files in the WAL archive.</p>\n</li>\n<li class=\"step\">\n<p>Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (<code class=\"option\">--source-pgdata</code>) or SQL (<code class=\"option\">--source-server</code>). Relation files are now in a state equivalent to the moment of the last completed checkpoint prior to the point at which the WAL timelines of the source and target diverged plus the current state on the source of any blocks changed on the target after that divergence.</p>\n</li>\n<li class=\"step\">\n<p>Copy all other files, including new relation files, WAL segments, <code class=\"filename\">pg_xact</code>, and configuration files from the source cluster to the target cluster. Similarly to base backups, the contents of the directories <code class=\"filename\">pg_dynshmem/</code>, <code class=\"filename\">pg_notify/</code>, <code class=\"filename\">pg_replslot/</code>, <code class=\"filename\">pg_serial/</code>, <code class=\"filename\">pg_snapshots/</code>, <code class=\"filename\">pg_stat_tmp/</code>, and <code class=\"filename\">pg_subtrans/</code> are omitted from the data copied from the source cluster. The files <code class=\"filename\">backup_label</code>, <code class=\"filename\">tablespace_map</code>, <code class=\"filename\">pg_internal.init</code>, <code class=\"filename\">postmaster.opts</code>, <code class=\"filename\">postmaster.pid</code> and <code class=\"filename\">.DS_Store</code> as well as any file or directory beginning with <code class=\"filename\">pgsql_tmp</code>, are omitted.</p>\n</li>\n<li class=\"step\">\n<p>Create a <code class=\"filename\">backup_label</code> file to begin WAL replay at the checkpoint created at failover and configure the <code class=\"filename\">pg_control</code> file with a minimum consistency LSN defined as the result of <code class=\"literal\">pg_current_wal_insert_lsn()</code> when rewinding from a live source or the last checkpoint LSN when rewinding from a stopped source.</p>\n</li>\n<li class=\"step\">\n<p>When starting the target, <span class=\"productname\">PostgreSQL</span> replays all the required WAL, resulting in a data directory in a consistent state.</p>\n</li>\n</ol>\n</div>\n</div>\n</div>\n</div></div>", "manual_path": "app-pgrewind.html", "comparison_data": {"options": [{"names": ["-D directory", "--target-pgdata= directory"], "signature": "-D directory --target-pgdata= directory", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "signature": "--source-pgdata= directory", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "signature": "--source-server= connstr", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "signature": "-R --write-recovery-conf", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "signature": "-n --dry-run", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "signature": "-N --no-sync", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "signature": "-P --progress", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "signature": "-c --restore-target-wal", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--config-file= filename"], "signature": "--config-file= filename", "description": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery)."}, {"names": ["--debug"], "signature": "--debug", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "signature": "--no-ensure-shutdown", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["--sync-method= method"], "signature": "--sync-method= method", "description": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace. On Linux, syncfs may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See recovery_init_sync_method for information about the caveats to be aware of when using syncfs . This option has no effect when --no-sync is used."}, {"names": ["-V", "--version"], "signature": "-V --version", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "signature": "-? --help", "description": "Show help, then exit."}], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "environment": []}, "comparison_hash": "65184284b9b782562c5b09cffbc1b933ebbf723a803dbe3a7320b5b328307823"}, "19": {"facts": [{"label": "Documented executable", "value": "pg_rewind"}, {"label": "Executable version", "value": "19beta4"}, {"label": "Reference inventory", "value": "Server applications"}, {"label": "Option definition groups", "value": "14"}], "tables": [{"key": "options", "rows": [{"summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": {"url": "/docs/19/app-pgrewind.html", "text": "-D datadir --target-pgdata= datadir"}}, {"summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": {"url": "/docs/19/app-pgrewind.html", "text": "--source-pgdata= datadir"}}, {"summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": {"url": "/docs/19/app-pgrewind.html", "text": "--source-server= connstr"}}, {"summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option.", "signature": {"url": "/docs/19/app-pgrewind.html", "text": "-R --write-recovery-conf"}}, {"summary": "Do everything except actually modifying the target directory.", "signature": {"url": "/docs/19/app-pgrewind.html", "text": "-n --dry-run"}}, {"summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": {"url": "/docs/19/app-pgrewind.html", "text": "-N --no-sync"}}, {"summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": {"url": "/docs/19/app-pgrewind.html", "text": "-P --progress"}}, {"summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": {"url": "/docs/19/app-pgrewind.html", "text": "-c --restore-target-wal"}}, {"summary": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery).", "signature": {"url": "/docs/19/app-pgrewind.html", "text": "--config-file= filename"}}, {"summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": {"url": "/docs/19/app-pgrewind.html", "text": "--debug"}}, {"summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": {"url": "/docs/19/app-pgrewind.html", "text": "--no-ensure-shutdown"}}, {"summary": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.", "signature": {"url": "/docs/19/app-pgrewind.html", "text": "--sync-method= method"}}, {"summary": "Display version information, then exit.", "signature": {"url": "/docs/19/app-pgrewind.html", "text": "-V --version"}}, {"summary": "Show help, then exit.", "signature": {"url": "/docs/19/app-pgrewind.html", "text": "-? --help"}}], "title": "Documented options", "columns": [{"key": "signature", "label": "Option and arguments"}, {"key": "summary", "label": "Description"}]}], "options": [{"names": ["-D datadir", "--target-pgdata= datadir"], "summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": "-D datadir --target-pgdata= datadir", "source_url": "/docs/19/app-pgrewind.html", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= datadir"], "summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": "--source-pgdata= datadir", "source_url": "/docs/19/app-pgrewind.html", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": "--source-server= connstr", "source_url": "/docs/19/app-pgrewind.html", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option.", "signature": "-R --write-recovery-conf", "source_url": "/docs/19/app-pgrewind.html", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "summary": "Do everything except actually modifying the target directory.", "signature": "-n --dry-run", "source_url": "/docs/19/app-pgrewind.html", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": "-N --no-sync", "source_url": "/docs/19/app-pgrewind.html", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": "-P --progress", "source_url": "/docs/19/app-pgrewind.html", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": "-c --restore-target-wal", "source_url": "/docs/19/app-pgrewind.html", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--config-file= filename"], "summary": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery).", "signature": "--config-file= filename", "source_url": "/docs/19/app-pgrewind.html", "description": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery)."}, {"names": ["--debug"], "summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": "--debug", "source_url": "/docs/19/app-pgrewind.html", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": "--no-ensure-shutdown", "source_url": "/docs/19/app-pgrewind.html", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["--sync-method= method"], "summary": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.", "signature": "--sync-method= method", "source_url": "/docs/19/app-pgrewind.html", "description": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace. On Linux, syncfs may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See recovery_init_sync_method for information about the caveats to be aware of when using syncfs . This option has no effect when --no-sync is used."}, {"names": ["-V", "--version"], "summary": "Display version information, then exit.", "signature": "-V --version", "source_url": "/docs/19/app-pgrewind.html", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "summary": "Show help, then exit.", "signature": "-? --help", "source_url": "/docs/19/app-pgrewind.html", "description": "Show help, then exit."}], "related": [], "release": {"ref": "https://ftp.postgresql.org/pub/source/v19beta4/postgresql-19beta4.tar.bz2", "label": "19beta4", "major": "19", "channel": "preview", "manifest": {"pdf": {"A4": {"url": "/files/documentation/pdf/19/postgresql-19-A4.pdf", "bytes": 16064841, "pages": 3052, "sha256": "4dd099e4125c591128fc5f3ebd02178dc24781f9e5ae629f96d67c4c8547427b", "built_at": "2026-09-26"}, "US": {"url": "/files/documentation/pdf/19/postgresql-19-US.pdf", "bytes": 15974616, "pages": 3225, "sha256": "61971fa857f0956d47341a0388fa6af9ae10acf691d4b2fc009007d384b0342b", "built_at": "2026-09-26"}}, "tree": "19", "index": "index.html", "major": "19", "pages": 1155, "release": "19beta4", "source_url": "https://ftp.postgresql.org/pub/source/v19beta4/postgresql-19beta4.tar.bz2", "svg_assets": 5, "source_mode": "en SGML built with pinned official archive", "source_sha256": "83157ee9c599d03b2f7a3d73ef3a56ec24e0e79cc2b3501a64d1364f56398c86"}, "revision": "1bbbbf4133d426f0e4304010688d2984c30fb67df0cc3a61b3e37eb3f6f37833", "evidence_kind": "English manual and source declarations", "source_sha256": "83157ee9c599d03b2f7a3d73ef3a56ec24e0e79cc2b3501a64d1364f56398c86"}, "sources": [{"url": "/docs/19/app-pgrewind.html", "file": "app-pgrewind.html", "label": "19beta4 English manual \u00b7 app-pgrewind.html", "anchor": "", "sha256": "ca761d5bf03354c4f9acbd76e533a6d7a64e1cca918720496321f6c8bbd99fdc"}, {"url": "/docs/19/reference-server.html", "file": "reference-server.html", "label": "Server applications inventory", "anchor": "", "sha256": "425b070323b448b5516d2cb4e9e4ac77efb5d19dd4e8005d0ae1c61db9ec78e6"}], "sections": [], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } datadir { --source-pgdata= datadir | --source-server= connstr }"], "signature": "pg_rewind [ option ...] { -D | --target-pgdata } datadir { --source-pgdata= datadir | --source-server= connstr }", "description": ["pg_rewind \u2014 synchronize a PostgreSQL data directory with another data directory that was forked from it"], "environment": [], "manual_html": "<div><div class=\"refentry\" id=\"APP-PGREWIND\">\n<div class=\"titlepage\"></div>\n<div class=\"refnamediv\">\n<h2><span class=\"refentrytitle\"><span class=\"application\">pg_rewind</span></span></h2>\n<p>pg_rewind \u2014 synchronize a <span class=\"productname\">PostgreSQL</span> data directory with another data directory that was forked from it</p>\n</div>\n<div class=\"refsynopsisdiv\">\n<h2>Synopsis</h2>\n<div class=\"cmdsynopsis\">\n<p id=\"id-1.9.5.10.4.1\"><code class=\"command\">pg_rewind</code> [<em class=\"replaceable\"><code>option</code></em>...] { <code class=\"option\">-D</code> | <code class=\"option\">--target-pgdata</code> } <em class=\"replaceable\"><code>datadir</code></em> { <code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>datadir</code></em></code> | <code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code> }</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.5\">\n<h2>Description</h2>\n<p><span class=\"application\">pg_rewind</span> is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old primary server back online after failover as a standby that follows the new primary.</p>\n<p>After a successful rewind, the state of the target data directory is analogous to a base backup of the source data directory. Unlike taking a new base backup or using a tool like <span class=\"application\">rsync</span>, <span class=\"application\">pg_rewind</span> does not require comparing or copying unchanged relation blocks in the cluster:</p>\n<div class=\"itemizedlist\">\n<ul class=\"itemizedlist\">\n<li class=\"listitem\">\n<p>Only changed blocks from existing relation files are copied.</p>\n</li>\n<li class=\"listitem\">\n<p>WAL segments prior to the point where the source and target servers have diverged are not copied. WAL segments generated after the source and target servers have diverged are copied in full.</p>\n</li>\n<li class=\"listitem\">\n<p>All other files, including new relation files and configuration files, are copied in full.</p>\n</li>\n</ul>\n</div>\n<p>As such, the rewind operation is significantly faster than other approaches when the database is large and only a small fraction of blocks differ between the clusters.</p>\n<p><span class=\"application\">pg_rewind</span> examines the timeline histories of the source and target clusters to determine the point where they diverged, and expects to find WAL in the target cluster's <code class=\"filename\">pg_wal</code> directory reaching all the way back to the point of divergence. The point of divergence can be found either on the target timeline, the source timeline, or their common ancestor. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem, but if the target cluster ran for a long time after the divergence, its old WAL files might no longer be present. In this case, you can manually copy them from the WAL archive to the <code class=\"filename\">pg_wal</code> directory, or run <span class=\"application\">pg_rewind</span> with the <code class=\"literal\">-c</code> option to automatically retrieve them from the WAL archive. The use of <span class=\"application\">pg_rewind</span> is not limited to failover, e.g., a standby server can be promoted, run some write transactions, and then rewound to become a standby again.</p>\n<p>After running <span class=\"application\">pg_rewind</span>, WAL replay needs to complete for the data directory to be in a consistent state. When the target server is started again it will enter archive recovery and replay all WAL generated in the source server from the last checkpoint before the point of divergence. If some of the WAL was no longer available in the source server when <span class=\"application\">pg_rewind</span> was run, and therefore could not be copied by the <span class=\"application\">pg_rewind</span> session, it must be made available when the target server is started. This can be done by creating a <code class=\"filename\">recovery.signal</code> file in the target data directory and by configuring a suitable <a class=\"xref\" href=\"/docs/19/runtime-config-wal.html#GUC-RESTORE-COMMAND\">restore_command</a> in <code class=\"filename\">postgresql.conf</code>.</p>\n<p><span class=\"application\">pg_rewind</span> requires that the target server either has the <a class=\"xref\" href=\"/docs/19/runtime-config-wal.html#GUC-WAL-LOG-HINTS\">wal_log_hints</a> option enabled in <code class=\"filename\">postgresql.conf</code> or data checksums enabled when the cluster was initialized with <span class=\"application\">initdb</span> (the default). <a class=\"xref\" href=\"/docs/19/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\">full_page_writes</a> must also be set to <code class=\"literal\">on</code>, but is enabled by default.</p>\n<div class=\"warning\">\n<h3 class=\"title\">Warning: Failures While Rewinding</h3>\n<p>If <span class=\"application\">pg_rewind</span> fails while processing, then the data folder of the target is likely not in a state that can be recovered. In such a case, taking a new fresh backup is recommended.</p>\n<p>As <span class=\"application\">pg_rewind</span> copies configuration files entirely from the source, it may be required to correct the configuration used for recovery before restarting the target server, especially if the target is reintroduced as a standby of the source. If you restart the server after the rewind operation has finished but without configuring recovery, the target may again diverge from the primary.</p>\n<p><span class=\"application\">pg_rewind</span> will fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates. If such files are present on the target server it is recommended to remove them before running <span class=\"application\">pg_rewind</span>. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind.</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.6\">\n<h2>Options</h2>\n<p><span class=\"application\">pg_rewind</span> accepts the following command-line arguments:</p>\n<div class=\"variablelist\">\n<dl class=\"variablelist\">\n<dt><span class=\"term\"><code class=\"option\">-D <em class=\"replaceable\"><code>datadir</code></em></code><br></span><span class=\"term\"><code class=\"option\">--target-pgdata=<em class=\"replaceable\"><code>datadir</code></em></code></span></dt>\n<dd>\n<p>This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running <span class=\"application\">pg_rewind</span></p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>datadir</code></em></code></span></dt>\n<dd>\n<p>Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code></span></dt>\n<dd>\n<p>Specifies a libpq connection string to connect to the source <span class=\"productname\">PostgreSQL</span> server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-R</code><br></span><span class=\"term\"><code class=\"option\">--write-recovery-conf</code></span></dt>\n<dd>\n<p>Create <code class=\"filename\">standby.signal</code> and append connection settings to <code class=\"filename\">postgresql.auto.conf</code> in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or <a class=\"link\" href=\"/docs/19/libpq-envars.html\" title=\"32.15.\u00a0Environment Variables\">environment variable</a>. <code class=\"literal\">--source-server</code> is mandatory with this option.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-n</code><br></span><span class=\"term\"><code class=\"option\">--dry-run</code></span></dt>\n<dd>\n<p>Do everything except actually modifying the target directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-N</code><br></span><span class=\"term\"><code class=\"option\">--no-sync</code></span></dt>\n<dd>\n<p>By default, <code class=\"command\">pg_rewind</code> will wait for all files to be written safely to disk. This option causes <code class=\"command\">pg_rewind</code> to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-P</code><br></span><span class=\"term\"><code class=\"option\">--progress</code></span></dt>\n<dd>\n<p>Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-c</code><br></span><span class=\"term\"><code class=\"option\">--restore-target-wal</code></span></dt>\n<dd>\n<p>Use <code class=\"varname\">restore_command</code> defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the <code class=\"filename\">pg_wal</code> directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--config-file=<em class=\"replaceable\"><code>filename</code></em></code></span></dt>\n<dd>\n<p>Use the specified main server configuration file for the target cluster. This affects <span class=\"application\">pg_rewind</span> when it uses internally the <span class=\"application\">postgres</span> command for the rewind operation on this cluster (when retrieving <code class=\"varname\">restore_command</code> with the option <code class=\"option\">-c/--restore-target-wal</code> and when forcing a completion of crash recovery).</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--debug</code></span></dt>\n<dd>\n<p>Print verbose debugging output that is mostly useful for developers debugging <span class=\"application\">pg_rewind</span>.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--no-ensure-shutdown</code></span></dt>\n<dd>\n<p><span class=\"application\">pg_rewind</span> requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, <span class=\"application\">pg_rewind</span> starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, <span class=\"application\">pg_rewind</span> skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--sync-method=<em class=\"replaceable\"><code>method</code></em></code></span></dt>\n<dd>\n<p>When set to <code class=\"literal\">fsync</code>, which is the default, <code class=\"command\">pg_rewind</code> will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.</p>\n<p>On Linux, <code class=\"literal\">syncfs</code> may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See <a class=\"xref\" href=\"/docs/19/runtime-config-error-handling.html#GUC-RECOVERY-INIT-SYNC-METHOD\">recovery_init_sync_method</a> for information about the caveats to be aware of when using <code class=\"literal\">syncfs</code>.</p>\n<p>This option has no effect when <code class=\"option\">--no-sync</code> is used.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-V</code><br></span><span class=\"term\"><code class=\"option\">--version</code></span></dt>\n<dd>\n<p>Display version information, then exit.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-?</code><br></span><span class=\"term\"><code class=\"option\">--help</code></span></dt>\n<dd>\n<p>Show help, then exit.</p>\n</dd>\n</dl>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.7\">\n<h2>Environment</h2>\n<p>When <code class=\"option\">--source-server</code> option is used, <span class=\"application\">pg_rewind</span> also uses the environment variables supported by <span class=\"application\">libpq</span> (see <a class=\"xref\" href=\"/docs/19/libpq-envars.html\" title=\"32.15.\u00a0Environment Variables\">Section\u00a032.15</a>).</p>\n<p>The environment variable <code class=\"envar\">PG_COLOR</code> specifies whether to use color in diagnostic messages. Possible values are <code class=\"literal\">always</code>, <code class=\"literal\">auto</code> and <code class=\"literal\">never</code>.</p>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.8\">\n<h2>Notes</h2>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source, a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source cluster can be used instead of a superuser. Here is how to create such a role, named <code class=\"literal\">rewind_user</code> here:</p>\n<pre class=\"programlisting\">CREATE USER rewind_user LOGIN;\nGRANT EXECUTE ON FUNCTION pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;\nGRANT EXECUTE ON FUNCTION pg_catalog.pg_stat_file(text, boolean) TO rewind_user;\nGRANT EXECUTE ON FUNCTION pg_catalog.pg_read_binary_file(text) TO rewind_user;\nGRANT EXECUTE ON FUNCTION pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;\n</pre>\n<div class=\"refsect2\" id=\"id-1.9.5.10.8.3\">\n<h3>How It Works</h3>\n<p>The basic idea is to copy all file system-level changes from the source cluster to the target cluster:</p>\n<div class=\"procedure\">\n<ol class=\"procedure\">\n<li class=\"step\">\n<p>Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's timeline history forked off from the target cluster. For each WAL record, record each data block that was touched. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off. If some of the WAL files are no longer available, try re-running <span class=\"application\">pg_rewind</span> with the <code class=\"option\">-c</code> option to search for the missing files in the WAL archive.</p>\n</li>\n<li class=\"step\">\n<p>Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (<code class=\"option\">--source-pgdata</code>) or SQL (<code class=\"option\">--source-server</code>). Relation files are now in a state equivalent to the moment of the last completed checkpoint prior to the point at which the WAL timelines of the source and target diverged plus the current state on the source of any blocks changed on the target after that divergence.</p>\n</li>\n<li class=\"step\">\n<p>Copy all other files, including new relation files, WAL segments, <code class=\"filename\">pg_xact</code>, and configuration files from the source cluster to the target cluster. Similarly to base backups, the contents of the directories <code class=\"filename\">pg_dynshmem/</code>, <code class=\"filename\">pg_notify/</code>, <code class=\"filename\">pg_replslot/</code>, <code class=\"filename\">pg_serial/</code>, <code class=\"filename\">pg_snapshots/</code>, <code class=\"filename\">pg_stat_tmp/</code>, and <code class=\"filename\">pg_subtrans/</code> are omitted from the data copied from the source cluster. The files <code class=\"filename\">backup_label</code>, <code class=\"filename\">tablespace_map</code>, <code class=\"filename\">pg_internal.init</code>, <code class=\"filename\">postmaster.opts</code>, <code class=\"filename\">postmaster.pid</code> and <code class=\"filename\">.DS_Store</code> as well as any file or directory beginning with <code class=\"filename\">pgsql_tmp</code>, are omitted.</p>\n</li>\n<li class=\"step\">\n<p>Create a <code class=\"filename\">backup_label</code> file to begin WAL replay at the checkpoint created at failover and configure the <code class=\"filename\">pg_control</code> file with a minimum consistency LSN defined as the result of <code class=\"literal\">pg_current_wal_insert_lsn()</code> when rewinding from a live source or the last checkpoint LSN when rewinding from a stopped source.</p>\n</li>\n<li class=\"step\">\n<p>When starting the target, <span class=\"productname\">PostgreSQL</span> replays all the required WAL, resulting in a data directory in a consistent state.</p>\n</li>\n</ol>\n</div>\n</div>\n</div>\n</div></div>", "manual_path": "app-pgrewind.html", "comparison_data": {"options": [{"names": ["-D datadir", "--target-pgdata= datadir"], "signature": "-D datadir --target-pgdata= datadir", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= datadir"], "signature": "--source-pgdata= datadir", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "signature": "--source-server= connstr", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "signature": "-R --write-recovery-conf", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "signature": "-n --dry-run", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "signature": "-N --no-sync", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "signature": "-P --progress", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "signature": "-c --restore-target-wal", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--config-file= filename"], "signature": "--config-file= filename", "description": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery)."}, {"names": ["--debug"], "signature": "--debug", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "signature": "--no-ensure-shutdown", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["--sync-method= method"], "signature": "--sync-method= method", "description": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace. On Linux, syncfs may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See recovery_init_sync_method for information about the caveats to be aware of when using syncfs . This option has no effect when --no-sync is used."}, {"names": ["-V", "--version"], "signature": "-V --version", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "signature": "-? --help", "description": "Show help, then exit."}], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } datadir { --source-pgdata= datadir | --source-server= connstr }"], "environment": []}, "comparison_hash": "d59f36cd646493e88683607ce3c98ba98a53b8f84a354a45ec215e8d5f78c8b7"}, "20": {"facts": [{"label": "Documented executable", "value": "pg_rewind"}, {"label": "Executable version", "value": "20devel"}, {"label": "Reference inventory", "value": "Server applications"}, {"label": "Option definition groups", "value": "14"}], "tables": [{"key": "options", "rows": [{"summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": {"url": "/docs/devel/app-pgrewind.html", "text": "-D datadir --target-pgdata= datadir"}}, {"summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": {"url": "/docs/devel/app-pgrewind.html", "text": "--source-pgdata= datadir"}}, {"summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": {"url": "/docs/devel/app-pgrewind.html", "text": "--source-server= connstr"}}, {"summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option.", "signature": {"url": "/docs/devel/app-pgrewind.html", "text": "-R --write-recovery-conf"}}, {"summary": "Do everything except actually modifying the target directory.", "signature": {"url": "/docs/devel/app-pgrewind.html", "text": "-n --dry-run"}}, {"summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": {"url": "/docs/devel/app-pgrewind.html", "text": "-N --no-sync"}}, {"summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": {"url": "/docs/devel/app-pgrewind.html", "text": "-P --progress"}}, {"summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": {"url": "/docs/devel/app-pgrewind.html", "text": "-c --restore-target-wal"}}, {"summary": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery).", "signature": {"url": "/docs/devel/app-pgrewind.html", "text": "--config-file= filename"}}, {"summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": {"url": "/docs/devel/app-pgrewind.html", "text": "--debug"}}, {"summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": {"url": "/docs/devel/app-pgrewind.html", "text": "--no-ensure-shutdown"}}, {"summary": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.", "signature": {"url": "/docs/devel/app-pgrewind.html", "text": "--sync-method= method"}}, {"summary": "Display version information, then exit.", "signature": {"url": "/docs/devel/app-pgrewind.html", "text": "-V --version"}}, {"summary": "Show help, then exit.", "signature": {"url": "/docs/devel/app-pgrewind.html", "text": "-? --help"}}], "title": "Documented options", "columns": [{"key": "signature", "label": "Option and arguments"}, {"key": "summary", "label": "Description"}]}], "options": [{"names": ["-D datadir", "--target-pgdata= datadir"], "summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": "-D datadir --target-pgdata= datadir", "source_url": "/docs/devel/app-pgrewind.html", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= datadir"], "summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": "--source-pgdata= datadir", "source_url": "/docs/devel/app-pgrewind.html", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": "--source-server= connstr", "source_url": "/docs/devel/app-pgrewind.html", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option.", "signature": "-R --write-recovery-conf", "source_url": "/docs/devel/app-pgrewind.html", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "summary": "Do everything except actually modifying the target directory.", "signature": "-n --dry-run", "source_url": "/docs/devel/app-pgrewind.html", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": "-N --no-sync", "source_url": "/docs/devel/app-pgrewind.html", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": "-P --progress", "source_url": "/docs/devel/app-pgrewind.html", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": "-c --restore-target-wal", "source_url": "/docs/devel/app-pgrewind.html", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--config-file= filename"], "summary": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery).", "signature": "--config-file= filename", "source_url": "/docs/devel/app-pgrewind.html", "description": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery)."}, {"names": ["--debug"], "summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": "--debug", "source_url": "/docs/devel/app-pgrewind.html", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": "--no-ensure-shutdown", "source_url": "/docs/devel/app-pgrewind.html", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["--sync-method= method"], "summary": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.", "signature": "--sync-method= method", "source_url": "/docs/devel/app-pgrewind.html", "description": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace. On Linux, syncfs may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See recovery_init_sync_method for information about the caveats to be aware of when using syncfs . This option has no effect when --no-sync is used."}, {"names": ["-V", "--version"], "summary": "Display version information, then exit.", "signature": "-V --version", "source_url": "/docs/devel/app-pgrewind.html", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "summary": "Show help, then exit.", "signature": "-? --help", "source_url": "/docs/devel/app-pgrewind.html", "description": "Show help, then exit."}], "related": [], "release": {"ref": "https://ftp.postgresql.org/pub/snapshot/dev/postgresql-snapshot.tar.bz2", "label": "20devel", "major": "20", "channel": "devel", "manifest": {"pdf": {"A4": {"url": "/files/documentation/pdf/20/postgresql-20-A4.pdf", "bytes": 16030631, "pages": 3052, "sha256": "bd5d82c0ce38fc18f92a0447818a91a193a261776bca1c37564bf9a683e177d0", "built_at": "2026-09-28"}, "US": {"url": "/files/documentation/pdf/20/postgresql-20-US.pdf", "bytes": 15936613, "pages": 3223, "sha256": "d97d9e0db479a02f4234b175f50fcad70c3661619afc8d6df9b9437882e3c299", "built_at": "2026-09-28"}}, "tree": "0", "index": "index.html", "major": "20", "pages": 1156, "release": "20devel", "source_url": "https://ftp.postgresql.org/pub/snapshot/dev/postgresql-snapshot.tar.bz2", "svg_assets": 6, "source_mode": "en SGML built with pinned official archive", "source_sha256": "4d3346909b201ac1648232cf290462a7070c119326f56196f1f0253ed80fae41", "source_snapshot_utc": "26-Sep-2026 20:22"}, "revision": "2eba5e0fd4c3bffb2803247b6cd537878e9d6ee5a6dfbe3c50ece8b421b80918", "evidence_kind": "English manual and source declarations", "source_sha256": "4d3346909b201ac1648232cf290462a7070c119326f56196f1f0253ed80fae41"}, "sources": [{"url": "/docs/devel/app-pgrewind.html", "file": "app-pgrewind.html", "label": "20devel English manual \u00b7 app-pgrewind.html", "anchor": "", "sha256": "8ed7c9616716f2d04d81b1459495c14ec047920831a073729fcdf1144cb6fdb9"}, {"url": "/docs/devel/reference-server.html", "file": "reference-server.html", "label": "Server applications inventory", "anchor": "", "sha256": "5532c1ebfb8c6f310301be63a28daf0ae212e2a78025bea61a1bdca99e02c43d"}], "sections": [], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } datadir { --source-pgdata= datadir | --source-server= connstr }"], "signature": "pg_rewind [ option ...] { -D | --target-pgdata } datadir { --source-pgdata= datadir | --source-server= connstr }", "description": ["pg_rewind \u2014 synchronize a PostgreSQL data directory with another data directory that was forked from it"], "environment": [], "manual_html": "<div><div class=\"refentry\" id=\"APP-PGREWIND\">\n<div class=\"titlepage\"></div>\n<div class=\"refnamediv\">\n<h2><span class=\"refentrytitle\"><span class=\"application\">pg_rewind</span></span></h2>\n<p>pg_rewind \u2014 synchronize a <span class=\"productname\">PostgreSQL</span> data directory with another data directory that was forked from it</p>\n</div>\n<div class=\"refsynopsisdiv\">\n<h2>Synopsis</h2>\n<div class=\"cmdsynopsis\">\n<p id=\"id-1.9.5.10.4.1\"><code class=\"command\">pg_rewind</code> [<em class=\"replaceable\"><code>option</code></em>...] { <code class=\"option\">-D</code> | <code class=\"option\">--target-pgdata</code> } <em class=\"replaceable\"><code>datadir</code></em> { <code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>datadir</code></em></code> | <code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code> }</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.5\">\n<h2>Description</h2>\n<p><span class=\"application\">pg_rewind</span> is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old primary server back online after failover as a standby that follows the new primary.</p>\n<p>After a successful rewind, the state of the target data directory is analogous to a base backup of the source data directory. Unlike taking a new base backup or using a tool like <span class=\"application\">rsync</span>, <span class=\"application\">pg_rewind</span> does not require comparing or copying unchanged relation blocks in the cluster:</p>\n<div class=\"itemizedlist\">\n<ul class=\"itemizedlist\">\n<li class=\"listitem\">\n<p>Only changed blocks from existing relation files are copied.</p>\n</li>\n<li class=\"listitem\">\n<p>WAL segments prior to the point where the source and target servers have diverged are not copied. WAL segments generated after the source and target servers have diverged are copied in full.</p>\n</li>\n<li class=\"listitem\">\n<p>All other files, including new relation files and configuration files, are copied in full.</p>\n</li>\n</ul>\n</div>\n<p>As such, the rewind operation is significantly faster than other approaches when the database is large and only a small fraction of blocks differ between the clusters.</p>\n<p><span class=\"application\">pg_rewind</span> examines the timeline histories of the source and target clusters to determine the point where they diverged, and expects to find WAL in the target cluster's <code class=\"filename\">pg_wal</code> directory reaching all the way back to the point of divergence. The point of divergence can be found either on the target timeline, the source timeline, or their common ancestor. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem, but if the target cluster ran for a long time after the divergence, its old WAL files might no longer be present. In this case, you can manually copy them from the WAL archive to the <code class=\"filename\">pg_wal</code> directory, or run <span class=\"application\">pg_rewind</span> with the <code class=\"literal\">-c</code> option to automatically retrieve them from the WAL archive. The use of <span class=\"application\">pg_rewind</span> is not limited to failover, e.g., a standby server can be promoted, run some write transactions, and then rewound to become a standby again.</p>\n<p>After running <span class=\"application\">pg_rewind</span>, WAL replay needs to complete for the data directory to be in a consistent state. When the target server is started again it will enter archive recovery and replay all WAL generated in the source server from the last checkpoint before the point of divergence. If some of the WAL was no longer available in the source server when <span class=\"application\">pg_rewind</span> was run, and therefore could not be copied by the <span class=\"application\">pg_rewind</span> session, it must be made available when the target server is started. This can be done by creating a <code class=\"filename\">recovery.signal</code> file in the target data directory and by configuring a suitable <a class=\"xref\" href=\"/docs/devel/runtime-config-wal.html#GUC-RESTORE-COMMAND\">restore_command</a> in <code class=\"filename\">postgresql.conf</code>.</p>\n<p><span class=\"application\">pg_rewind</span> requires that the target server either has the <a class=\"xref\" href=\"/docs/devel/runtime-config-wal.html#GUC-WAL-LOG-HINTS\">wal_log_hints</a> option enabled in <code class=\"filename\">postgresql.conf</code> or data checksums enabled when the cluster was initialized with <span class=\"application\">initdb</span> (the default). <a class=\"xref\" href=\"/docs/devel/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\">full_page_writes</a> must also be set to <code class=\"literal\">on</code>, but is enabled by default.</p>\n<p>The data checksum states of the source and target server must be compatible. <span class=\"application\">pg_rewind</span> refuses to run if an online checksum state transition was interrupted on either server, or if data checksums are enabled on the target server, or were enabled at the point of divergence, while the source server runs without them: the rewound server would fail checksum verification on the blocks copied from the source. The opposite combination is allowed with a warning; the rewound server keeps data checksums disabled unless the source server enabled them online after the point of divergence. See <a class=\"xref\" href=\"/docs/devel/app-pgchecksums.html\" title=\"pg_checksums\"><span class=\"refentrytitle\"><span class=\"application\">pg_checksums</span></span></a> for how to change the state consistently across a replication setup.</p>\n<div class=\"warning\">\n<h3 class=\"title\">Warning: Failures While Rewinding</h3>\n<p>If <span class=\"application\">pg_rewind</span> fails while processing, then the data folder of the target is likely not in a state that can be recovered. In such a case, taking a new fresh backup is recommended.</p>\n<p>As <span class=\"application\">pg_rewind</span> copies configuration files entirely from the source, it may be required to correct the configuration used for recovery before restarting the target server, especially if the target is reintroduced as a standby of the source. If you restart the server after the rewind operation has finished but without configuring recovery, the target may again diverge from the primary.</p>\n<p><span class=\"application\">pg_rewind</span> will fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates. If such files are present on the target server it is recommended to remove them before running <span class=\"application\">pg_rewind</span>. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind.</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.6\">\n<h2>Options</h2>\n<p><span class=\"application\">pg_rewind</span> accepts the following command-line arguments:</p>\n<div class=\"variablelist\">\n<dl class=\"variablelist\">\n<dt><span class=\"term\"><code class=\"option\">-D <em class=\"replaceable\"><code>datadir</code></em></code><br></span><span class=\"term\"><code class=\"option\">--target-pgdata=<em class=\"replaceable\"><code>datadir</code></em></code></span></dt>\n<dd>\n<p>This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running <span class=\"application\">pg_rewind</span></p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>datadir</code></em></code></span></dt>\n<dd>\n<p>Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code></span></dt>\n<dd>\n<p>Specifies a libpq connection string to connect to the source <span class=\"productname\">PostgreSQL</span> server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-R</code><br></span><span class=\"term\"><code class=\"option\">--write-recovery-conf</code></span></dt>\n<dd>\n<p>Create <code class=\"filename\">standby.signal</code> and append connection settings to <code class=\"filename\">postgresql.auto.conf</code> in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or <a class=\"link\" href=\"/docs/devel/libpq-envars.html\" title=\"32.14.\u00a0Environment Variables\">environment variable</a>. <code class=\"literal\">--source-server</code> is mandatory with this option.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-n</code><br></span><span class=\"term\"><code class=\"option\">--dry-run</code></span></dt>\n<dd>\n<p>Do everything except actually modifying the target directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-N</code><br></span><span class=\"term\"><code class=\"option\">--no-sync</code></span></dt>\n<dd>\n<p>By default, <code class=\"command\">pg_rewind</code> will wait for all files to be written safely to disk. This option causes <code class=\"command\">pg_rewind</code> to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-P</code><br></span><span class=\"term\"><code class=\"option\">--progress</code></span></dt>\n<dd>\n<p>Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-c</code><br></span><span class=\"term\"><code class=\"option\">--restore-target-wal</code></span></dt>\n<dd>\n<p>Use <code class=\"varname\">restore_command</code> defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the <code class=\"filename\">pg_wal</code> directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--config-file=<em class=\"replaceable\"><code>filename</code></em></code></span></dt>\n<dd>\n<p>Use the specified main server configuration file for the target cluster. This affects <span class=\"application\">pg_rewind</span> when it uses internally the <span class=\"application\">postgres</span> command for the rewind operation on this cluster (when retrieving <code class=\"varname\">restore_command</code> with the option <code class=\"option\">-c/--restore-target-wal</code> and when forcing a completion of crash recovery).</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--debug</code></span></dt>\n<dd>\n<p>Print verbose debugging output that is mostly useful for developers debugging <span class=\"application\">pg_rewind</span>.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--no-ensure-shutdown</code></span></dt>\n<dd>\n<p><span class=\"application\">pg_rewind</span> requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, <span class=\"application\">pg_rewind</span> starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, <span class=\"application\">pg_rewind</span> skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--sync-method=<em class=\"replaceable\"><code>method</code></em></code></span></dt>\n<dd>\n<p>When set to <code class=\"literal\">fsync</code>, which is the default, <code class=\"command\">pg_rewind</code> will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.</p>\n<p>On Linux, <code class=\"literal\">syncfs</code> may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See <a class=\"xref\" href=\"/docs/devel/runtime-config-error-handling.html#GUC-RECOVERY-INIT-SYNC-METHOD\">recovery_init_sync_method</a> for information about the caveats to be aware of when using <code class=\"literal\">syncfs</code>.</p>\n<p>This option has no effect when <code class=\"option\">--no-sync</code> is used.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-V</code><br></span><span class=\"term\"><code class=\"option\">--version</code></span></dt>\n<dd>\n<p>Display version information, then exit.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-?</code><br></span><span class=\"term\"><code class=\"option\">--help</code></span></dt>\n<dd>\n<p>Show help, then exit.</p>\n</dd>\n</dl>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.7\">\n<h2>Environment</h2>\n<p>When <code class=\"option\">--source-server</code> option is used, <span class=\"application\">pg_rewind</span> also uses the environment variables supported by <span class=\"application\">libpq</span> (see <a class=\"xref\" href=\"/docs/devel/libpq-envars.html\" title=\"32.14.\u00a0Environment Variables\">Section\u00a032.14</a>).</p>\n<p>The environment variable <code class=\"envar\">PG_COLOR</code> specifies whether to use color in diagnostic messages. Possible values are <code class=\"literal\">always</code>, <code class=\"literal\">auto</code> and <code class=\"literal\">never</code>.</p>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.8\">\n<h2>Notes</h2>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source, a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source cluster can be used instead of a superuser. Here is how to create such a role, named <code class=\"literal\">rewind_user</code> here:</p>\n<pre class=\"programlisting\">CREATE USER rewind_user LOGIN;\nGRANT EXECUTE ON FUNCTION pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;\nGRANT EXECUTE ON FUNCTION pg_catalog.pg_stat_file(text, boolean) TO rewind_user;\nGRANT EXECUTE ON FUNCTION pg_catalog.pg_read_binary_file(text) TO rewind_user;\nGRANT EXECUTE ON FUNCTION pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;\n</pre>\n<div class=\"refsect2\" id=\"id-1.9.5.10.8.3\">\n<h3>How It Works</h3>\n<p>The basic idea is to copy all file system-level changes from the source cluster to the target cluster:</p>\n<div class=\"procedure\">\n<ol class=\"procedure\">\n<li class=\"step\">\n<p>Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's timeline history forked off from the target cluster. For each WAL record, record each data block that was touched. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off. If some of the WAL files are no longer available, try re-running <span class=\"application\">pg_rewind</span> with the <code class=\"option\">-c</code> option to search for the missing files in the WAL archive.</p>\n</li>\n<li class=\"step\">\n<p>Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (<code class=\"option\">--source-pgdata</code>) or SQL (<code class=\"option\">--source-server</code>). Relation files are now in a state equivalent to the moment of the last completed checkpoint prior to the point at which the WAL timelines of the source and target diverged plus the current state on the source of any blocks changed on the target after that divergence.</p>\n</li>\n<li class=\"step\">\n<p>Copy all other files, including new relation files, WAL segments, <code class=\"filename\">pg_xact</code>, and configuration files from the source cluster to the target cluster. Similarly to base backups, the contents of the directories <code class=\"filename\">pg_dynshmem/</code>, <code class=\"filename\">pg_notify/</code>, <code class=\"filename\">pg_replslot/</code>, <code class=\"filename\">pg_serial/</code>, <code class=\"filename\">pg_snapshots/</code>, <code class=\"filename\">pg_stat_tmp/</code>, and <code class=\"filename\">pg_subtrans/</code> are omitted from the data copied from the source cluster. The files <code class=\"filename\">backup_label</code>, <code class=\"filename\">tablespace_map</code>, <code class=\"filename\">pg_internal.init</code>, <code class=\"filename\">postmaster.opts</code>, <code class=\"filename\">postmaster.pid</code> and <code class=\"filename\">.DS_Store</code> as well as any file or directory beginning with <code class=\"filename\">pgsql_tmp</code>, are omitted.</p>\n</li>\n<li class=\"step\">\n<p>Create a <code class=\"filename\">backup_label</code> file to begin WAL replay at the checkpoint created at failover and configure the <code class=\"filename\">pg_control</code> file with a minimum consistency LSN defined as the result of <code class=\"literal\">pg_current_wal_insert_lsn()</code> when rewinding from a live source or the last checkpoint LSN when rewinding from a stopped source.</p>\n</li>\n<li class=\"step\">\n<p>When starting the target, <span class=\"productname\">PostgreSQL</span> replays all the required WAL, resulting in a data directory in a consistent state.</p>\n</li>\n</ol>\n</div>\n</div>\n</div>\n</div></div>", "manual_path": "app-pgrewind.html", "comparison_data": {"options": [{"names": ["-D datadir", "--target-pgdata= datadir"], "signature": "-D datadir --target-pgdata= datadir", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= datadir"], "signature": "--source-pgdata= datadir", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "signature": "--source-server= connstr", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "signature": "-R --write-recovery-conf", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "signature": "-n --dry-run", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "signature": "-N --no-sync", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "signature": "-P --progress", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "signature": "-c --restore-target-wal", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--config-file= filename"], "signature": "--config-file= filename", "description": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery)."}, {"names": ["--debug"], "signature": "--debug", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "signature": "--no-ensure-shutdown", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["--sync-method= method"], "signature": "--sync-method= method", "description": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace. On Linux, syncfs may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See recovery_init_sync_method for information about the caveats to be aware of when using syncfs . This option has no effect when --no-sync is used."}, {"names": ["-V", "--version"], "signature": "-V --version", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "signature": "-? --help", "description": "Show help, then exit."}], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } datadir { --source-pgdata= datadir | --source-server= connstr }"], "environment": []}, "comparison_hash": "d59f36cd646493e88683607ce3c98ba98a53b8f84a354a45ec215e8d5f78c8b7"}}}, "snapshot": {"facts": [{"label": "Documented executable", "value": "pg_rewind"}, {"label": "Executable version", "value": "18.6"}, {"label": "Reference inventory", "value": "Server applications"}, {"label": "Option definition groups", "value": "14"}], "tables": [{"key": "options", "rows": [{"summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-D directory --target-pgdata= directory"}}, {"summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "--source-pgdata= directory"}}, {"summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "--source-server= connstr"}}, {"summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-R --write-recovery-conf"}}, {"summary": "Do everything except actually modifying the target directory.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-n --dry-run"}}, {"summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-N --no-sync"}}, {"summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-P --progress"}}, {"summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-c --restore-target-wal"}}, {"summary": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery).", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "--config-file= filename"}}, {"summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "--debug"}}, {"summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "--no-ensure-shutdown"}}, {"summary": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "--sync-method= method"}}, {"summary": "Display version information, then exit.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-V --version"}}, {"summary": "Show help, then exit.", "signature": {"url": "/docs/18/app-pgrewind.html", "text": "-? --help"}}], "title": "Documented options", "columns": [{"key": "signature", "label": "Option and arguments"}, {"key": "summary", "label": "Description"}]}], "options": [{"names": ["-D directory", "--target-pgdata= directory"], "summary": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind", "signature": "-D directory --target-pgdata= directory", "source_url": "/docs/18/app-pgrewind.html", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "summary": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.", "signature": "--source-pgdata= directory", "source_url": "/docs/18/app-pgrewind.html", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "summary": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.", "signature": "--source-server= connstr", "source_url": "/docs/18/app-pgrewind.html", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "summary": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option.", "signature": "-R --write-recovery-conf", "source_url": "/docs/18/app-pgrewind.html", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "summary": "Do everything except actually modifying the target directory.", "signature": "-n --dry-run", "source_url": "/docs/18/app-pgrewind.html", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "summary": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.", "signature": "-N --no-sync", "source_url": "/docs/18/app-pgrewind.html", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "summary": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.", "signature": "-P --progress", "source_url": "/docs/18/app-pgrewind.html", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "summary": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.", "signature": "-c --restore-target-wal", "source_url": "/docs/18/app-pgrewind.html", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--config-file= filename"], "summary": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery).", "signature": "--config-file= filename", "source_url": "/docs/18/app-pgrewind.html", "description": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery)."}, {"names": ["--debug"], "summary": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind .", "signature": "--debug", "source_url": "/docs/18/app-pgrewind.html", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "summary": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.", "signature": "--no-ensure-shutdown", "source_url": "/docs/18/app-pgrewind.html", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["--sync-method= method"], "summary": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.", "signature": "--sync-method= method", "source_url": "/docs/18/app-pgrewind.html", "description": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace. On Linux, syncfs may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See recovery_init_sync_method for information about the caveats to be aware of when using syncfs . This option has no effect when --no-sync is used."}, {"names": ["-V", "--version"], "summary": "Display version information, then exit.", "signature": "-V --version", "source_url": "/docs/18/app-pgrewind.html", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "summary": "Show help, then exit.", "signature": "-? --help", "source_url": "/docs/18/app-pgrewind.html", "description": "Show help, then exit."}], "related": [], "release": {"ref": "https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2", "label": "18.6", "major": "18", "channel": "stable", "manifest": {"pdf": {"A4": {"url": "/files/documentation/pdf/18/postgresql-18-A4.pdf", "bytes": 15865106, "pages": 3154, "sha256": "19512c405da53f9f7fcf0abba359223aa65f021be025bf3411381918f92e3190", "built_at": "2026-09-26"}, "US": {"url": "/files/documentation/pdf/18/postgresql-18-US.pdf", "bytes": 15748059, "pages": 3328, "sha256": "facbe6c229e598b872d3d98bef53308f46e06746006fa4590de9a7de9dd46319", "built_at": "2026-09-26"}}, "tree": "18", "index": "index.html", "major": "18", "pages": 1148, "release": "18.6", "source_url": "https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2", "svg_assets": 3, "source_mode": "en SGML built with pinned official archive", "source_sha256": "555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f"}, "revision": "ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8", "evidence_kind": "English manual and source declarations", "source_sha256": "555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f"}, "sources": [{"url": "/docs/18/app-pgrewind.html", "file": "app-pgrewind.html", "label": "18.6 English manual \u00b7 app-pgrewind.html", "anchor": "", "sha256": "2d8a13348838dfc44e3733f7e269f3fb35643d705686198ab09cc5bada9a1330"}, {"url": "/docs/18/reference-server.html", "file": "reference-server.html", "label": "Server applications inventory", "anchor": "", "sha256": "e9f8bbcc8e3641fadd1d20991e8aad6b532155b786ba5a1b7b364ec4f1a5bef3"}], "sections": [], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "signature": "pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }", "description": ["pg_rewind \u2014 synchronize a PostgreSQL data directory with another data directory that was forked from it"], "environment": [], "manual_html": "<div><div class=\"refentry\" id=\"APP-PGREWIND\">\n<div class=\"titlepage\"></div>\n<div class=\"refnamediv\">\n<h2><span class=\"refentrytitle\"><span class=\"application\">pg_rewind</span></span></h2>\n<p>pg_rewind \u2014 synchronize a <span class=\"productname\">PostgreSQL</span> data directory with another data directory that was forked from it</p>\n</div>\n<div class=\"refsynopsisdiv\">\n<h2>Synopsis</h2>\n<div class=\"cmdsynopsis\">\n<p id=\"id-1.9.5.10.4.1\"><code class=\"command\">pg_rewind</code> [<em class=\"replaceable\"><code>option</code></em>...] { <code class=\"option\">-D</code> | <code class=\"option\">--target-pgdata</code> } <em class=\"replaceable\"><code>directory</code></em> { <code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code> | <code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code> }</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.5\">\n<h2>Description</h2>\n<p><span class=\"application\">pg_rewind</span> is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old primary server back online after failover as a standby that follows the new primary.</p>\n<p>After a successful rewind, the state of the target data directory is analogous to a base backup of the source data directory. Unlike taking a new base backup or using a tool like <span class=\"application\">rsync</span>, <span class=\"application\">pg_rewind</span> does not require comparing or copying unchanged relation blocks in the cluster. Only changed blocks from existing relation files are copied; all other files, including new relation files, configuration files, and WAL segments, are copied in full. As such the rewind operation is significantly faster than other approaches when the database is large and only a small fraction of blocks differ between the clusters.</p>\n<p><span class=\"application\">pg_rewind</span> examines the timeline histories of the source and target clusters to determine the point where they diverged, and expects to find WAL in the target cluster's <code class=\"filename\">pg_wal</code> directory reaching all the way back to the point of divergence. The point of divergence can be found either on the target timeline, the source timeline, or their common ancestor. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem, but if the target cluster ran for a long time after the divergence, its old WAL files might no longer be present. In this case, you can manually copy them from the WAL archive to the <code class=\"filename\">pg_wal</code> directory, or run <span class=\"application\">pg_rewind</span> with the <code class=\"literal\">-c</code> option to automatically retrieve them from the WAL archive. The use of <span class=\"application\">pg_rewind</span> is not limited to failover, e.g., a standby server can be promoted, run some write transactions, and then rewound to become a standby again.</p>\n<p>After running <span class=\"application\">pg_rewind</span>, WAL replay needs to complete for the data directory to be in a consistent state. When the target server is started again it will enter archive recovery and replay all WAL generated in the source server from the last checkpoint before the point of divergence. If some of the WAL was no longer available in the source server when <span class=\"application\">pg_rewind</span> was run, and therefore could not be copied by the <span class=\"application\">pg_rewind</span> session, it must be made available when the target server is started. This can be done by creating a <code class=\"filename\">recovery.signal</code> file in the target data directory and by configuring a suitable <a class=\"xref\" href=\"/docs/18/runtime-config-wal.html#GUC-RESTORE-COMMAND\">restore_command</a> in <code class=\"filename\">postgresql.conf</code>.</p>\n<p><span class=\"application\">pg_rewind</span> requires that the target server either has the <a class=\"xref\" href=\"/docs/18/runtime-config-wal.html#GUC-WAL-LOG-HINTS\">wal_log_hints</a> option enabled in <code class=\"filename\">postgresql.conf</code> or data checksums enabled when the cluster was initialized with <span class=\"application\">initdb</span> (the default). <a class=\"xref\" href=\"/docs/18/runtime-config-wal.html#GUC-FULL-PAGE-WRITES\">full_page_writes</a> must also be set to <code class=\"literal\">on</code>, but is enabled by default.</p>\n<div class=\"warning\">\n<h3 class=\"title\">Warning: Failures While Rewinding</h3>\n<p>If <span class=\"application\">pg_rewind</span> fails while processing, then the data folder of the target is likely not in a state that can be recovered. In such a case, taking a new fresh backup is recommended.</p>\n<p>As <span class=\"application\">pg_rewind</span> copies configuration files entirely from the source, it may be required to correct the configuration used for recovery before restarting the target server, especially if the target is reintroduced as a standby of the source. If you restart the server after the rewind operation has finished but without configuring recovery, the target may again diverge from the primary.</p>\n<p><span class=\"application\">pg_rewind</span> will fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates. If such files are present on the target server it is recommended to remove them before running <span class=\"application\">pg_rewind</span>. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind.</p>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.6\">\n<h2>Options</h2>\n<p><span class=\"application\">pg_rewind</span> accepts the following command-line arguments:</p>\n<div class=\"variablelist\">\n<dl class=\"variablelist\">\n<dt><span class=\"term\"><code class=\"option\">-D <em class=\"replaceable\"><code>directory</code></em></code><br></span><span class=\"term\"><code class=\"option\">--target-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running <span class=\"application\">pg_rewind</span></p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-pgdata=<em class=\"replaceable\"><code>directory</code></em></code></span></dt>\n<dd>\n<p>Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--source-server=<em class=\"replaceable\"><code>connstr</code></em></code></span></dt>\n<dd>\n<p>Specifies a libpq connection string to connect to the source <span class=\"productname\">PostgreSQL</span> server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-R</code><br></span><span class=\"term\"><code class=\"option\">--write-recovery-conf</code></span></dt>\n<dd>\n<p>Create <code class=\"filename\">standby.signal</code> and append connection settings to <code class=\"filename\">postgresql.auto.conf</code> in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or <a class=\"link\" href=\"/docs/18/libpq-envars.html\" title=\"32.15.\u00a0Environment Variables\">environment variable</a>. <code class=\"literal\">--source-server</code> is mandatory with this option.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-n</code><br></span><span class=\"term\"><code class=\"option\">--dry-run</code></span></dt>\n<dd>\n<p>Do everything except actually modifying the target directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-N</code><br></span><span class=\"term\"><code class=\"option\">--no-sync</code></span></dt>\n<dd>\n<p>By default, <code class=\"command\">pg_rewind</code> will wait for all files to be written safely to disk. This option causes <code class=\"command\">pg_rewind</code> to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-P</code><br></span><span class=\"term\"><code class=\"option\">--progress</code></span></dt>\n<dd>\n<p>Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-c</code><br></span><span class=\"term\"><code class=\"option\">--restore-target-wal</code></span></dt>\n<dd>\n<p>Use <code class=\"varname\">restore_command</code> defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the <code class=\"filename\">pg_wal</code> directory.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--config-file=<em class=\"replaceable\"><code>filename</code></em></code></span></dt>\n<dd>\n<p>Use the specified main server configuration file for the target cluster. This affects <span class=\"application\">pg_rewind</span> when it uses internally the <span class=\"application\">postgres</span> command for the rewind operation on this cluster (when retrieving <code class=\"varname\">restore_command</code> with the option <code class=\"option\">-c/--restore-target-wal</code> and when forcing a completion of crash recovery).</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--debug</code></span></dt>\n<dd>\n<p>Print verbose debugging output that is mostly useful for developers debugging <span class=\"application\">pg_rewind</span>.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--no-ensure-shutdown</code></span></dt>\n<dd>\n<p><span class=\"application\">pg_rewind</span> requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, <span class=\"application\">pg_rewind</span> starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, <span class=\"application\">pg_rewind</span> skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">--sync-method=<em class=\"replaceable\"><code>method</code></em></code></span></dt>\n<dd>\n<p>When set to <code class=\"literal\">fsync</code>, which is the default, <code class=\"command\">pg_rewind</code> will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.</p>\n<p>On Linux, <code class=\"literal\">syncfs</code> may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See <a class=\"xref\" href=\"/docs/18/runtime-config-error-handling.html#GUC-RECOVERY-INIT-SYNC-METHOD\">recovery_init_sync_method</a> for information about the caveats to be aware of when using <code class=\"literal\">syncfs</code>.</p>\n<p>This option has no effect when <code class=\"option\">--no-sync</code> is used.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-V</code><br></span><span class=\"term\"><code class=\"option\">--version</code></span></dt>\n<dd>\n<p>Display version information, then exit.</p>\n</dd>\n<dt><span class=\"term\"><code class=\"option\">-?</code><br></span><span class=\"term\"><code class=\"option\">--help</code></span></dt>\n<dd>\n<p>Show help, then exit.</p>\n</dd>\n</dl>\n</div>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.7\">\n<h2>Environment</h2>\n<p>When <code class=\"option\">--source-server</code> option is used, <span class=\"application\">pg_rewind</span> also uses the environment variables supported by <span class=\"application\">libpq</span> (see <a class=\"xref\" href=\"/docs/18/libpq-envars.html\" title=\"32.15.\u00a0Environment Variables\">Section\u00a032.15</a>).</p>\n<p>The environment variable <code class=\"envar\">PG_COLOR</code> specifies whether to use color in diagnostic messages. Possible values are <code class=\"literal\">always</code>, <code class=\"literal\">auto</code> and <code class=\"literal\">never</code>.</p>\n</div>\n<div class=\"refsect1\" id=\"id-1.9.5.10.8\">\n<h2>Notes</h2>\n<p>When executing <span class=\"application\">pg_rewind</span> using an online cluster as source, a role having sufficient permissions to execute the functions used by <span class=\"application\">pg_rewind</span> on the source cluster can be used instead of a superuser. Here is how to create such a role, named <code class=\"literal\">rewind_user</code> here:</p>\n<pre class=\"programlisting\">CREATE USER rewind_user LOGIN;\nGRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;\nGRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;\n</pre>\n<div class=\"refsect2\" id=\"id-1.9.5.10.8.3\">\n<h3>How It Works</h3>\n<p>The basic idea is to copy all file system-level changes from the source cluster to the target cluster:</p>\n<div class=\"procedure\">\n<ol class=\"procedure\">\n<li class=\"step\">\n<p>Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's timeline history forked off from the target cluster. For each WAL record, record each data block that was touched. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off. If some of the WAL files are no longer available, try re-running <span class=\"application\">pg_rewind</span> with the <code class=\"option\">-c</code> option to search for the missing files in the WAL archive.</p>\n</li>\n<li class=\"step\">\n<p>Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (<code class=\"option\">--source-pgdata</code>) or SQL (<code class=\"option\">--source-server</code>). Relation files are now in a state equivalent to the moment of the last completed checkpoint prior to the point at which the WAL timelines of the source and target diverged plus the current state on the source of any blocks changed on the target after that divergence.</p>\n</li>\n<li class=\"step\">\n<p>Copy all other files, including new relation files, WAL segments, <code class=\"filename\">pg_xact</code>, and configuration files from the source cluster to the target cluster. Similarly to base backups, the contents of the directories <code class=\"filename\">pg_dynshmem/</code>, <code class=\"filename\">pg_notify/</code>, <code class=\"filename\">pg_replslot/</code>, <code class=\"filename\">pg_serial/</code>, <code class=\"filename\">pg_snapshots/</code>, <code class=\"filename\">pg_stat_tmp/</code>, and <code class=\"filename\">pg_subtrans/</code> are omitted from the data copied from the source cluster. The files <code class=\"filename\">backup_label</code>, <code class=\"filename\">tablespace_map</code>, <code class=\"filename\">pg_internal.init</code>, <code class=\"filename\">postmaster.opts</code>, <code class=\"filename\">postmaster.pid</code> and <code class=\"filename\">.DS_Store</code> as well as any file or directory beginning with <code class=\"filename\">pgsql_tmp</code>, are omitted.</p>\n</li>\n<li class=\"step\">\n<p>Create a <code class=\"filename\">backup_label</code> file to begin WAL replay at the checkpoint created at failover and configure the <code class=\"filename\">pg_control</code> file with a minimum consistency LSN defined as the result of <code class=\"literal\">pg_current_wal_insert_lsn()</code> when rewinding from a live source or the last checkpoint LSN when rewinding from a stopped source.</p>\n</li>\n<li class=\"step\">\n<p>When starting the target, <span class=\"productname\">PostgreSQL</span> replays all the required WAL, resulting in a data directory in a consistent state.</p>\n</li>\n</ol>\n</div>\n</div>\n</div>\n</div></div>", "manual_path": "app-pgrewind.html", "comparison_data": {"options": [{"names": ["-D directory", "--target-pgdata= directory"], "signature": "-D directory --target-pgdata= directory", "description": "This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running pg_rewind"}, {"names": ["--source-pgdata= directory"], "signature": "--source-pgdata= directory", "description": "Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down."}, {"names": ["--source-server= connstr"], "signature": "--source-server= connstr", "description": "Specifies a libpq connection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections."}, {"names": ["-R", "--write-recovery-conf"], "signature": "-R --write-recovery-conf", "description": "Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. The dbname will be recorded only if the dbname was specified explicitly in the connection string or environment variable . --source-server is mandatory with this option."}, {"names": ["-n", "--dry-run"], "signature": "-n --dry-run", "description": "Do everything except actually modifying the target directory."}, {"names": ["-N", "--no-sync"], "signature": "-N --no-sync", "description": "By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation."}, {"names": ["-P", "--progress"], "signature": "-P --progress", "description": "Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster."}, {"names": ["-c", "--restore-target-wal"], "signature": "-c --restore-target-wal", "description": "Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory."}, {"names": ["--config-file= filename"], "signature": "--config-file= filename", "description": "Use the specified main server configuration file for the target cluster. This affects pg_rewind when it uses internally the postgres command for the rewind operation on this cluster (when retrieving restore_command with the option -c/--restore-target-wal and when forcing a completion of crash recovery)."}, {"names": ["--debug"], "signature": "--debug", "description": "Print verbose debugging output that is mostly useful for developers debugging pg_rewind ."}, {"names": ["--no-ensure-shutdown"], "signature": "--no-ensure-shutdown", "description": "pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case."}, {"names": ["--sync-method= method"], "signature": "--sync-method= method", "description": "When set to fsync , which is the default, pg_rewind will recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace. On Linux, syncfs may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See recovery_init_sync_method for information about the caveats to be aware of when using syncfs . This option has no effect when --no-sync is used."}, {"names": ["-V", "--version"], "signature": "-V --version", "description": "Display version information, then exit."}, {"names": ["-?", "--help"], "signature": "-? --help", "description": "Show help, then exit."}], "synopsis": ["pg_rewind [ option ...] { -D | --target-pgdata } directory { --source-pgdata= directory | --source-server= connstr }"], "environment": []}, "comparison_hash": "65184284b9b782562c5b09cffbc1b933ebbf723a803dbe3a7320b5b328307823"}, "comparison": {"left": "12", "right": "13", "status": "changed", "diff": "--- PostgreSQL 12\n+++ PostgreSQL 13\n@@ -24,6 +24,14 @@\n       \"signature\": \"--source-server= connstr\"\n     },\n     {\n+      \"description\": \"Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option.\",\n+      \"names\": [\n+        \"-R\",\n+        \"--write-recovery-conf\"\n+      ],\n+      \"signature\": \"-R --write-recovery-conf\"\n+    },\n+    {\n       \"description\": \"Do everything except actually modifying the target directory.\",\n       \"names\": [\n         \"-n\",\n@@ -32,7 +40,7 @@\n       \"signature\": \"-n --dry-run\"\n     },\n     {\n-      \"description\": \"By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used when creating a production installation.\",\n+      \"description\": \"By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.\",\n       \"names\": [\n         \"-N\",\n         \"--no-sync\"\n@@ -48,11 +56,26 @@\n       \"signature\": \"-P --progress\"\n     },\n     {\n+      \"description\": \"Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.\",\n+      \"names\": [\n+        \"-c\",\n+        \"--restore-target-wal\"\n+      ],\n+      \"signature\": \"-c --restore-target-wal\"\n+    },\n+    {\n       \"description\": \"Print verbose debugging output that is mostly useful for developers debugging pg_rewind .\",\n       \"names\": [\n         \"--debug\"\n       ],\n       \"signature\": \"--debug\"\n+    },\n+    {\n+      \"description\": \"pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case.\",\n+      \"names\": [\n+        \"--no-ensure-shutdown\"\n+      ],\n+      \"signature\": \"--no-ensure-shutdown\"\n     },\n     {\n       \"description\": \"Display version information, then exit.\","}}