<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >pg_basebackup</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 9.1.15 Documentation" HREF="index.html"><LINK REL="UP" TITLE="PostgreSQL Client Applications" HREF="reference-client.html"><LINK REL="PREVIOUS" TITLE="ecpg" HREF="app-ecpg.html"><LINK REL="NEXT" TITLE="pg_config" HREF="app-pgconfig.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2015-02-07T09:47:53"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" ><A HREF="index.html" >PostgreSQL 9.1.15 Documentation</A ></TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A TITLE="ecpg" HREF="app-ecpg.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="reference-client.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="20%" ALIGN="right" VALIGN="top" ><A TITLE="pg_config" HREF="app-pgconfig.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="APP-PGBASEBACKUP" ></A >pg_basebackup</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN78453" ></A ><H2 >Name</H2 >pg_basebackup -- take a base backup of a <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > cluster</DIV ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN78459" ></A ><H2 >Synopsis</H2 ><P ><TT CLASS="COMMAND" >pg_basebackup</TT > [<TT CLASS="REPLACEABLE" ><I >option</I ></TT >...]</P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN78464" ></A ><H2 > Description </H2 ><P > <SPAN CLASS="APPLICATION" >pg_basebackup</SPAN > is used to take base backups of a running <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > database cluster. These are taken without affecting other clients to the database, and can be used both for point-in-time recovery (see <A HREF="continuous-archiving.html" >Section 24.3</A >) and as the starting point for a log shipping or streaming replication standby servers (see <A HREF="warm-standby.html" >Section 25.2</A >). </P ><P > <SPAN CLASS="APPLICATION" >pg_basebackup</SPAN > makes a binary copy of the database cluster files, while making sure the system is put in and out of backup mode automatically. Backups are always taken of the entire database cluster; it is not possible to back up individual databases or database objects. For individual database backups, a tool such as <A HREF="app-pgdump.html" >pg_dump</A > must be used. </P ><P > The backup is made over a regular <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > connection, and uses the replication protocol. The connection must be made with a user having <TT CLASS="LITERAL" >REPLICATION</TT > permissions (see <A HREF="role-attributes.html" >Section 20.2</A >), and the user must be granted explicit permissions in <TT CLASS="FILENAME" >pg_hba.conf</TT >. The server must also be configured with <A HREF="runtime-config-replication.html#GUC-MAX-WAL-SENDERS" >max_wal_senders</A > set high enough to leave at least one session available for the backup. </P ><P > There can be multiple <TT CLASS="COMMAND" >pg_basebackup</TT >s running at the same time, but it is better from a performance point of view to take only one backup, and copy the result. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN78482" ></A ><H2 >Options</H2 ><P > The following command-line options control the location and format of the output. <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="OPTION" >-D <TT CLASS="REPLACEABLE" ><I >directory</I ></TT ></TT ><BR><TT CLASS="OPTION" >--pgdata=<TT CLASS="REPLACEABLE" ><I >directory</I ></TT ></TT ></DT ><DD ><P > Directory to write the output to. </P ><P > When the backup is in tar mode, and the directory is specified as <TT CLASS="LITERAL" >-</TT > (dash), the tar file will be written to <TT CLASS="LITERAL" >stdout</TT >. </P ><P > This parameter is required. </P ></DD ><DT ><TT CLASS="OPTION" >-F <TT CLASS="REPLACEABLE" ><I >format</I ></TT ></TT ><BR><TT CLASS="OPTION" >--format=<TT CLASS="REPLACEABLE" ><I >format</I ></TT ></TT ></DT ><DD ><P > Selects the format for the output. <TT CLASS="REPLACEABLE" ><I >format</I ></TT > can be one of the following: <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="LITERAL" >p</TT ><BR><TT CLASS="LITERAL" >plain</TT ></DT ><DD ><P > Write the output as plain files, with the same layout as the current data directory and tablespaces. When the cluster has no additional tablespaces, the whole database will be placed in the target directory. If the cluster contains additional tablespaces, the main data directory will be placed in the target directory, but all other tablespaces will be placed in the same absolute path as they have on the server. </P ><P > This is the default format. </P ></DD ><DT ><TT CLASS="LITERAL" >t</TT ><BR><TT CLASS="LITERAL" >tar</TT ></DT ><DD ><P > Write the output as tar files in the target directory. The main data directory will be written to a file named <TT CLASS="FILENAME" >base.tar</TT >, and all other tablespaces will be named after the tablespace OID. </P ><P > If the value <TT CLASS="LITERAL" >-</TT > (dash) is specified as target directory, the tar contents will be written to standard output, suitable for piping to for example <SPAN CLASS="PRODUCTNAME" >gzip</SPAN >. This is only possible if the cluster has no additional tablespaces. </P ></DD ></DL ></DIV ><P></P ></DD ><DT ><TT CLASS="OPTION" >-x</TT ><BR><TT CLASS="OPTION" >--xlog</TT ></DT ><DD ><P > Includes the required transaction log files (WAL files) in the backup. This will include all transaction logs generated during the backup. If this option is specified, it is possible to start a postmaster directly in the extracted directory without the need to consult the log archive, thus making this a completely standalone backup. </P ><DIV CLASS="NOTE" ><BLOCKQUOTE CLASS="NOTE" ><P ><B >Note: </B > The transaction log files are collected at the end of the backup. Therefore, it is necessary for the <A HREF="runtime-config-replication.html#GUC-WAL-KEEP-SEGMENTS" >wal_keep_segments</A > parameter to be set high enough that the log is not removed before the end of the backup. If the log has been rotated when it's time to transfer it, the backup will fail and be unusable. </P ></BLOCKQUOTE ></DIV ></DD ><DT ><TT CLASS="OPTION" >-z</TT ><BR><TT CLASS="OPTION" >--gzip</TT ></DT ><DD ><P > Enables gzip compression of tar file output, with the default compression level. Compression is only available when using the tar format. </P ></DD ><DT ><TT CLASS="OPTION" >-Z <TT CLASS="REPLACEABLE" ><I >level</I ></TT ></TT ><BR><TT CLASS="OPTION" >--compress=<TT CLASS="REPLACEABLE" ><I >level</I ></TT ></TT ></DT ><DD ><P > Enables gzip compression of tar file output, and specifies the compression level (1 through 9, 9 being best compression). Compression is only available when using the tar format. </P ></DD ></DL ></DIV ><P> </P ><P > The following command-line options control the generation of the backup and the running of the program. <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="OPTION" >-c <TT CLASS="REPLACEABLE" ><I >fast|spread</I ></TT ></TT ><BR><TT CLASS="OPTION" >--checkpoint=<TT CLASS="REPLACEABLE" ><I >fast|spread</I ></TT ></TT ></DT ><DD ><P > Sets checkpoint mode to fast or spread (default). </P ></DD ><DT ><TT CLASS="OPTION" >-l <TT CLASS="REPLACEABLE" ><I >label</I ></TT ></TT ><BR><TT CLASS="OPTION" >--label=<TT CLASS="REPLACEABLE" ><I >label</I ></TT ></TT ></DT ><DD ><P > Sets the label for the backup. If none is specified, a default value of <TT CLASS="LITERAL" >pg_basebackup base backup</TT > will be used. </P ></DD ><DT ><TT CLASS="OPTION" >-P</TT ><BR><TT CLASS="OPTION" >--progress</TT ></DT ><DD ><P > Enables progress reporting. Turning this on will deliver an approximate progress report during the backup. Since the database may change during the backup, this is only an approximation and may not end at exactly <TT CLASS="LITERAL" >100%</TT >. In particular, when WAL log is included in the backup, the total amount of data cannot be estimated in advance, and in this case the estimated target size will increase once it passes the total estimate without WAL. </P ><P > When this is enabled, the backup will start by enumerating the size of the entire database, and then go back and send the actual contents. This may make the backup take slightly longer, and in particular it will take longer before the first data is sent. </P ></DD ><DT ><TT CLASS="OPTION" >-v</TT ><BR><TT CLASS="OPTION" >--verbose</TT ></DT ><DD ><P > Enables verbose mode. Will output some extra steps during startup and shutdown, as well as show the exact file name that is currently being processed if progress reporting is also enabled. </P ></DD ></DL ></DIV ><P> </P ><P > The following command-line options control the database connection parameters. <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="OPTION" >-h <TT CLASS="REPLACEABLE" ><I >host</I ></TT ></TT ><BR><TT CLASS="OPTION" >--host=<TT CLASS="REPLACEABLE" ><I >host</I ></TT ></TT ></DT ><DD ><P > Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket. The default is taken from the <TT CLASS="ENVAR" >PGHOST</TT > environment variable, if set, else a Unix domain socket connection is attempted. </P ></DD ><DT ><TT CLASS="OPTION" >-p <TT CLASS="REPLACEABLE" ><I >port</I ></TT ></TT ><BR><TT CLASS="OPTION" >--port=<TT CLASS="REPLACEABLE" ><I >port</I ></TT ></TT ></DT ><DD ><P > Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. Defaults to the <TT CLASS="ENVAR" >PGPORT</TT > environment variable, if set, or a compiled-in default. </P ></DD ><DT ><TT CLASS="OPTION" >-U <TT CLASS="REPLACEABLE" ><I >username</I ></TT ></TT ><BR><TT CLASS="OPTION" >--username=<TT CLASS="REPLACEABLE" ><I >username</I ></TT ></TT ></DT ><DD ><P > User name to connect as. </P ></DD ><DT ><TT CLASS="OPTION" >-w</TT ><BR><TT CLASS="OPTION" >--no-password</TT ></DT ><DD ><P > Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a <TT CLASS="FILENAME" >.pgpass</TT > file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password. </P ></DD ><DT ><TT CLASS="OPTION" >-W</TT ><BR><TT CLASS="OPTION" >--password</TT ></DT ><DD ><P > Force <SPAN CLASS="APPLICATION" >pg_basebackup</SPAN > to prompt for a password before connecting to a database. </P ><P > This option is never essential, since <SPAN CLASS="APPLICATION" >pg_basebackup</SPAN > will automatically prompt for a password if the server demands password authentication. However, <SPAN CLASS="APPLICATION" >pg_basebackup</SPAN > will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing <TT CLASS="OPTION" >-W</TT > to avoid the extra connection attempt. </P ></DD ></DL ></DIV ><P> </P ><P > Other, less commonly used, parameters are also available: <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="OPTION" >-V</TT ><BR><TT CLASS="OPTION" >--version</TT ></DT ><DD ><P > Print the <SPAN CLASS="APPLICATION" >pg_basebackup</SPAN > version and exit. </P ></DD ><DT ><TT CLASS="OPTION" >-?</TT ><BR><TT CLASS="OPTION" >--help</TT ></DT ><DD ><P > Show help about <SPAN CLASS="APPLICATION" >pg_basebackup</SPAN > command line arguments, and exit. </P ></DD ></DL ></DIV ><P> </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN78661" ></A ><H2 >Environment</H2 ><P > This utility, like most other <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > utilities, uses the environment variables supported by <SPAN CLASS="APPLICATION" >libpq</SPAN > (see <A HREF="libpq-envars.html" >Section 31.13</A >). </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN78667" ></A ><H2 >Notes</H2 ><P > The backup will include all files in the data directory and tablespaces, including the configuration files and any additional files placed in the directory by third parties. Only regular files and directories are allowed in the data directory, no symbolic links or special device files. </P ><P > The way <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > manages tablespaces, the path for all additional tablespaces must be identical whenever a backup is restored. The main data directory, however, is relocatable to any location. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN78672" ></A ><H2 >Examples</H2 ><P > To create a base backup of the server at <TT CLASS="LITERAL" >mydbserver</TT > and store it in the local directory <TT CLASS="FILENAME" >/usr/local/pgsql/data</TT >: </P><PRE CLASS="SCREEN" ><SAMP CLASS="PROMPT" >$</SAMP > <KBD CLASS="USERINPUT" >pg_basebackup -h mydbserver -D /usr/local/pgsql/data</KBD ></PRE ><P> </P ><P > To create a backup of the local server with one compressed tar file for each tablespace, and store it in the directory <TT CLASS="FILENAME" >backup</TT >, showing a progress report while running: </P><PRE CLASS="SCREEN" ><SAMP CLASS="PROMPT" >$</SAMP > <KBD CLASS="USERINPUT" >pg_basebackup -D backup -Ft -z -P</KBD ></PRE ><P> </P ><P > To create a backup of a single-tablespace local database and compress this with <SPAN CLASS="PRODUCTNAME" >bzip2</SPAN >: </P><PRE CLASS="SCREEN" ><SAMP CLASS="PROMPT" >$</SAMP > <KBD CLASS="USERINPUT" >pg_basebackup -D - -Ft | bzip2 > backup.tar.bz2</KBD ></PRE ><P> (This command will fail if there are multiple tablespaces in the database.) </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN78690" ></A ><H2 >See Also</H2 ><A HREF="app-pgdump.html" >pg_dump</A ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="app-ecpg.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="app-pgconfig.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><SPAN CLASS="APPLICATION" >ecpg</SPAN ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="reference-client.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >pg_config</TD ></TR ></TABLE ></DIV ></BODY ></HTML >