Backups en Postgre

download Backups en Postgre

of 29

Transcript of Backups en Postgre

  • 8/6/2019 Backups en Postgre

    1/29

    Howto Backup PostgreSQL Databases Server

    With pg_dump command

    by LinuxTitli on March 1, 2006 21 comments

    Generally, I like to work with MySQL but some time my work force me to work with PostgreSQLdatabase server.

    Recently I had received a request to backup PostgreSQL databases as one of our client want to

    format and reinstall RHEL server.

    PostgreSQL is a one of the robust, open source database server. Like MySQL database server, itprovides utilities for creating a backup.

    Step # 1: Login as a pgsql user

    Type the following command:$ su - pgsql

    Get list of database(s) to backup:$ psql -l

    Step # 2: Make a backup using pg_dump

    Backup database using pg_dump command. pg_dump is a utility for backing up a PostgreSQLdatabase. It dumps only one database at a time. General syntax:pg_dump databasename > outputfile

    Task: dump a payroll database

    Type the following command$ pg_dump payroll > payroll.dump.out

    To restore a payroll database:$ psql -d payroll -f payroll.dump.out

    OR

    $ createdb payroll$ psql payroll

  • 8/6/2019 Backups en Postgre

    2/29

    However, i realli e you need to compress database:

    $ pg_dump payroll | gzip -c > payroll.dump.out.gz

    To restore database use t e following command:

    $ gunzip payroll.dump.out.gz$ psql -d payroll -f payroll.dump.out

    Here is a shell script for same task:

    #!/bin/bashDIR=/backup/psql[ ! $DIR] && mkdir -p $DIR || :LIST=$(psql -l | awk'{ print $1}' | grep -vE '^-|^List|^Name|template[0|1]' )for d in$LISTdopg_dump $d | gzip -c > $DIR/$d.out.gz

    done

    Another option is use to pg_dumpall command. As a name suggestit dumps (backs up) eachdatabase, and preserves cluster-wide data such as users and groups. You can use it as follows:

    $ pg_dumpall > all.dbs.out

    OR

    $ pg_dumpall | gzip -c > all.dbs.out.gz

    To restore backup use the following command:

    $ psql -f all.dbs.out postgresReferences:

  • 8/6/2019 Backups en Postgre

    3/29

    Forma correcta de sacar un respaldo (backup)

    en PostgreSQL

    Publicado porRolando en Bases De Datos el 07 9th, 2008 | 62 Comentarios

    Esta base de datos es una de las mejores, y a mi en particular me gusta mas queMySQL, pero sigue siendo un dolor de cabeza a la hora de hacer respaldos.

    A continuacin les enseare como hacer respaldos de una manera rpida y sencilla. Con este tipo de

    respaldo no vamos a perder SP, Vistas, tablas, ni nada de data etc.

    Vamos hacer de cuenta que la base de datos que queremos hacer respaldo se llama prueba. Yo voy a

    utilizar el usuario de postgres para trabajar con esto. Pero podran usar cualquier otro usuario. Yopara pasarme al usuario postgres ejecuto el siguiente comando como root:

    www:/etc# su postgrespostgres@www:/etc$

    Ya siendo usuario postgres no tengo ningn tipo de restriccin y puedo ejecutar cualquiera de loscomandos mencionados a continuacin.

    Con el siguiente comando sacaremos respaldo del esquema (schema) de la base de datos:

    pg_dump -sv prueba -O > /backup/prueba.schema.sql

    Con el siguiente comando sacaremos el repaldo de la DATA de la base de datos:

    pg_dump -Fc -f /backup/prueba.data.dump -a disable-triggers prueba

    Estamos usando las opciones -Fc que significa la F de formato y la c de custom, estamos utilizandoel formato custom, para as al momento de restaurarla base de datos en otro server o en otra base dedatos se nos har mas fcil porque vamos a utilizar el comando pg_restore.

    Les adjunto un poco mas de informacin en ingles con respecto al dump de postgreSQL en formatocustom.

    c Output a custom archive suitable forinputinto pg_restore. This is themost flexible formatin thatit allows reordering ofloading data as wellas object definitions. This formatis also compressed by default.

    La opcin -flo uso para especificar el archivo.

    La opcin -a especificar que quiero solo exportarla data.

  • 8/6/2019 Backups en Postgre

    4/29

    La opcin disable-triggers la uso para que no de problemas a la hora de hacerla restauracin.

    Estos comandos nos crearan dos archivos, uno con el esquema (schema) y otro con la data de la basede datos.

    Para restaurarla informacin haremos lo siguiente.

    Vamos escribir el comando psql y luego enter.

    Welcome to psql 8.1.11, the PostgreSQLinteractive terminal.

    Type: \copyright for distribution terms

    \h for help with SQL commands

    \? for help with psql commands

    \g or terminate with semicolon to execute query

    \q to quit

    postgres=#

    Ahora ya estando dentro de postgres, voy a crearla base de datos con el siguiente comando:

    CREATE DATABASE prueba ENCODING UTF8;

    Antes de crearla base de datos verifiquen que tipo de ENCODIN usa la base de datos de dondesacaron el respaldo ya que si crean la base de datos con diferente encoding alimportarla data elrespaldo NO va a funcionar, para saber el encoding de la base de datos lo hacen de la siguientemanera:

    psql -l

    Y les va a mostrarlo siguiente:

    sh-3.1$ psql -lList of databases

    Name | Owner | Encoding++data1 | data1 | SQL_ASCIIdata2 | data2 | SQL_ASCII

    postgres | postgres | SQL_ASCIItemplate0 | postgres | SQL_ASCIItemplate1 | postgres | SQL_ASCII

    El ENCODIN de todas las bases de datos en este ejemplo es SQL_ASCII.

    luego nos salimos de la consola de postgres con el siguiente comando:

    \q y presionamos enter.

    Ya estando de vuela en la consola de sistema, ejecutaremos el siguiente comando:

  • 8/6/2019 Backups en Postgre

    5/29

    psql prueba < prueba.schema.sql

    Con este comando importaremos el schema en la nueva base de datos.

    Ahora con el siguiente comando importaremos toda la data:

    pg_restore -a -v -e -Fc -O disable-triggers -d prueba prueba.data.dump

    Con este comando insertaremos toda la data en la nueva base de datos.

    Recomendaciones:

    En este ejemplo sacamos el respaldo con el usuario postgres, recomiendo sacar el respaldo e importarla base de datos con el usuario propietario de la base de datos. Ya que siinsertamos la base de datosen otro server cone l usuario postres, al momento de poner a funcionar el respaldo van a haber

    problemas con los permisos del usuario, ya que el usuario de la base de datos en el anterior server esjuan, el usuario en el respaldo va a ser postgres.

    Felicitaciones, ha echo un respaldo y ha restaurado una base de datos de postgreSQL, sin perder nadade informacin me imagino que hay muchas maneras de realizar esto, pero compart con ustedes lamanera como yo hago los respaldos, cualquier comentario o sugerencia es bienvenida.

  • 8/6/2019 Backups en Postgre

    6/29

    Chapter 23. Backup and Restore

    http://www.postgresql.org/docs/8.1/static/backup.html

    http://www.postgresql.org/docs/8.3/static/backup.html

    Table of Contents23.1. SQL Dump

    23.1.1. Restoring the dump23.1.2. Using pg_dumpall23.1.3. Handling large databases

    23.2. File system level backup23.3. On-line backup and point-in-time recovery (PITR)

    23.3.1. Setting up WAL archiving23.3.2. Making a Base Backup

    23.3.3. Recovering with an On-line Backup23.3.4. Timelines23.3.5. Caveats

    23.4. Migration Between Releases

    As with everything that contains valuable data, PostgreSQL databases should be backed up regularly.While the procedure is essentially simple, itis importantto have a basic understanding oftheunderlying techniques and assumptions.

    There are three fundamentally different approaches to backing up PostgreSQL data:

    y SQL dumpy File system level backupy On-line backup

    Each has its own strengths and weaknesses.

    23.1. SQL Dump

    The idea behind the SQL-dump method is to generate a text file with SQL commands that, when fedbackto the server, will recreate the database in the same state as it was atthe time ofthe dump.PostgreSQL provides the utility programpg_dump forthis purpose. The basic usage ofthiscommand is:

    pg_dump dbname > outfile

    As you see, pg_dump writes its results to the standard output. We will see below how this can beuseful.

    pg_dump is a regularPostgreSQL client application (albeit a particularly clever one). This meansthat you can do this backup procedure from any remote hostthat has access to the database. Butrememberthat pg_dump does not operate with special permissions. In particular, it must have read

  • 8/6/2019 Backups en Postgre

    7/29

    access to alltables that you wantto back up, so in practice you almost always have to run it as adatabase superuser.

    To specify which database server pg_dump should contact, use the command line options -h host

    and -p port. The default hostis the local host or whatever yourPGHOST environment variablespecifies. Similarly, the default portis indicated by the PGPORT environment variable or, failing that,

    by the compiled-in default. (Conveniently, the server will normally have the same compiled-indefault.)

    As any otherPostgreSQL client application, pg_dump will by default connect with the database user

    name thatis equalto the current operating system user name. To override this, either specify the -Uoption or setthe environment variable PGUSER. Rememberthat pg_dump connections are subjecttothe normal client authentication mechanisms (which are described inChapter 20).

    Dumps created by pg_dump are internally consistent, thatis, updates to the database while pg_dumpis running will not be in the dump. pg_dump does not block other operations on the database while itis working. (Exceptions are those operations that need to operate with an exclusive lock, such asVACUUM FULL .)

    Important: When your database schema relies on OIDs (forinstance as foreign keys) you must

    instruct pg_dump to dump the OIDs as well. To do this, use the -o command line option.

    23.1.1. Restoring the dump

    The text files created by pg_dump are intended to be read in by the psql program. The generalcommand form to restore a dump is

    psql dbname < infile

    where infileis what you used as outfile forthe pg_dump command. The database dbname willnot be created by this command, you must create it yourself from template0 before executing psql(e.g., with createdb -T template0 dbname). psql supports options similarto pg_dump forcontrolling the database serverlocation and the user name. Seepsql's reference page for moreinformation.

    Not only mustthe target database already exist before starting to run the restore, but so must alltheusers who own objects in the dumped database or were granted permissions on the objects. Ifthey donot, then the restore will failto recreate the objects with the original ownership and/or permissions.(Sometimes this is what you want, but usually itis not.)

    Once restored, itis wise to run ANALYZE on each database so the optimizer has useful statistics. An

    easy way to do this is to run vacuumdb -a -zto VACUUM ANALYZE all databases; this is equivalenttorunning VACUUM ANALYZE manually.

    The ability of pg_dump and psqlto write to or read from pipes makes it possible to dump a databasedirectly from one serverto another; for example:

    pg_dump -h host1dbname | psql -h host2dbname

  • 8/6/2019 Backups en Postgre

    8/29

    Important: The dumps produced by pg_dump are relative to template0. This means that anylanguages, procedures, etc. added to template1 will also be dumped by pg_dump. As a result, when

    restoring, if you are using a customized template1, you must create the empty database from

    template0, as in the example above.

    For advice on how to load large amounts of data into PostgreSQL efficiently, referto Section 13.4.

    23.1.2. Using pg_dumpall

    The above mechanism is cumbersome and inappropriate when backing up an entire database cluster.Forthis reason thepg_dumpall program is provided. pg_dumpall backs up each database in a givencluster, and also preserves cluster-wide data such as users and groups. The basic usage ofthiscommand is:

    pg_dumpall > outfile

    The resulting dump can be restored with psql:

    psql -f infile postgres

    (Actually, you can specify any existing database name to start from, butif you are reloading in an

    empty clusterthen postgres should generally be used.) Itis always necessary to have databasesuperuser access when restoring a pg_dumpall dump, as thatis required to restore the user and groupinformation.

    23.1.3. Handling large databases

    Since PostgreSQL allows tables largerthan the maximum file size on your system, it can beproblematic to dump such a table to a file, since the resulting file willlikely be largerthan themaximum size allowed by your system. Since pg_dump can write to the standard output, you can justuse standard Unix tools to work around this possible problem.

    Use compressed dumps. You can use your favorite compression program, for example gzip.

    pg_dump dbname | gzip > filename.gz

    Reload with

    createdb dbnamegunzip -c filename.gz | psql dbname

    or

    cat filename.gz | gunzip | psql dbname

    Use split. The split command allows you to splitthe outputinto pieces that are acceptable in sizeto the underlying file system. For example, to make chunks of 1 megabyte:

    pg_dump dbname | split -b 1m - filename

  • 8/6/2019 Backups en Postgre

    9/29

    Reload with

    createdb dbnamecat filename* | psql dbname

    Use the custom dump format. IfPostgreSQL was built on a system with the zlib compression

    library installed, the custom dump format will compress data as it writes itto the output file. Thiswill produce dump file sizes similarto using gzip, butit has the added advantage thattables can berestored selectively. The following command dumps a database using the custom dump format:

    pg_dump -Fc dbname > filename

    A custom-format dump is not a script for psql, butinstead must be restored with pg_restore. See thepg_dump andpg_restore reference pages for details.

  • 8/6/2019 Backups en Postgre

    10/29

    pg_dump

    Name

    pg_dump -- extract aP

    ostgreSQL database into a script file or other archive file

    Synopsis

    pg_dump [connection-option...] [option...] [dbname]

    Description

    pg_dump is a utility for backing up a PostgreSQL database. It makes consistent backups even ifthedatabase is being used concurrently. pg_dump does not block other users accessing the database(readers or writers).

    Dumps can be outputin script or archive file formats. Script dumps are plain-text files containing theSQL commands required to reconstructthe database to the state it was in atthe time it was saved. Torestore from such a script, feed ittopsql. Script files can be used to reconstructthe database even onother machines and other architectures; with some modifications, even on other SQL database

    products.

    The alternative archive file formats must be used withpg_restoreto rebuild the database. They allowpg_restore to be selective about whatis restored, or even to reorderthe items priorto being restored.The archive file formats are designed to be portable across architectures.

    When used with one ofthe archive file formats and combined with pg_restore, pg_dump provides a

    flexible archival and transfer mechanism. pg_dump can be used to backup an entire database, thenpg_restore can be used to examine the archive and/or select which parts ofthe database are to be

    restored. The most flexible output file formatis the "custom" format (-Fc). It allows for selectionand reordering of all archived items, and is compressed by default.

    While running pg_dump, one should examine the output for any warnings (printed on standarderror), especially in light ofthe limitations listed below.

    Options

    The following command-line options controlthe content and format ofthe output.

    dbname

    Specifies the name ofthe database to be dumped. Ifthis is not specified, the environment

    variable PGDATABASEis used. Ifthatis not set, the user name specified forthe connection isused.

    -a--data-only

  • 8/6/2019 Backups en Postgre

    11/29

  • 8/6/2019 Backups en Postgre

    12/29

    pplain

    Output a plain-text SQL script file (the default).

    ccustom

    Output a custom-format archive suitable forinputinto pg_restore. Together with thedirectory output format, this is the most flexible output formatin thatit allows manualselection and reordering of archived items during restore. This formatis also compressed bydefault.

    ddirectory

    Output a directory-format archive suitable forinputinto pg_restore. This will create a

    directory with one file for each table and blob being dumped, plus a so-called Table ofContents file describing the dumped objects in a machine-readable formatthat pg_restore can

    read. A directory format archive can be manipulated with standard Unix tools; for example,files in an uncompressed archive can be compressed with the gzip tool. This formatis

    compressed by default.

    ttar

    Output a tar-format archive suitable forinputinto pg_restore. The tar-formatis compatiblewith the directory-format; extracting a tar-format archive produces a valid directory-formatarchive. However, the tar-format does not support compression and has a limit of 8 B on thesize ofindividualtables. Also, the relative order oftable data items cannot be changed duringrestore.

    -i--ignore-version

    A deprecated option thatis now ignored.

    -n schema--schema=schema

    Dump only schemas matching schema; this selects both the schema itself, and allitscontained objects. When this option is not specified, all non-system schemas in the target

    database will be dumped. Multiple schemas can be selected by writing multiple -n switches.

    Also, the schema parameteris interpreted as a pattern according to the same rules used bypsql's \d commands (seePatterns), so multiple schemas can also be selected by writingwildcard characters in the pattern. When using wildcards, be carefulto quote the pattern ifneeded to preventthe shell from expanding the wildcards; see Examples.

    Note: When -nis specified, pg_dump makes no attemptto dump any other databaseobjects thatthe selected schema(s) might depend upon. Therefore, there is noguarantee thatthe results of a specific-schema dump can be successfully restored bythemselves into a clean database.

  • 8/6/2019 Backups en Postgre

    13/29

    Note:Non-schema objects such as blobs are not dumped when -nis specified. You

    can add blobs backto the dump with the --blobs switch.

    -N schema--exclude-schema=schema

    Do not dump any schemas matching the schema pattern. The pattern is interpreted accordingto the same rules as for-n. -N can be given more than once to exclude schemas matching anyof several patterns.

    When both -n and -N are given, the behavioris to dump justthe schemas that match atleastone -n switch but no -N switches. If-N appears without-n, then schemas matching -N areexcluded from whatis otherwise a normal dump.

    -o--oids

    Dump objectidentifiers (OIDs) as part ofthe data for every table. Use this option if your

    application references the OID columns in some way (e.g., in a foreign key constraint).Otherwise, this option should not be used.

    -O--no-owner

    Do not output commands to set ownership of objects to match the original database. By

    default, pg_dump issues ALTER OWNER orSET SESSION AUTHORIZATION statements to setownership of created database objects. These statements will fail when the scriptis run unlessitis started by a superuser (orthe same userthat owns all ofthe objects in the script). Tomake a scriptthat can be restored by any user, but will give that user ownership of alltheobjects, specify -O.

    This option is only meaningful forthe plain-text format. Forthe archive formats, you can

    specify the option when you callpg_restore.

    -R--no-reconnect

    This option is obsolete but still accepted for backwards compatibility.

    -s--schema-only

    Dump only the object definitions (schema), not data.

    -S username--superuser=username

    Specify the superuser user name to use when disabling triggers. This is only relevantif--

    disable-triggers is used. (Usually, it's betterto leave this out, and instead starttheresulting script as superuser.)

  • 8/6/2019 Backups en Postgre

    14/29

    -t table--table=table

    Dump only tables (or views or sequences or foreign tables) matchingtable. Multiple tables

    can be selected by writing multiple -t switches. Also, the table parameteris interpreted as apattern according to the same rules used by psql's \d commands (seePatterns), so multiple

    tables can also be selected by writing wildcard characters in the pattern. When usingwildcards, be carefulto quote the pattern if needed to preventthe shell from expanding thewildcards; see Examples.

    The -n and -N switches have no effect when -tis used, because tables selected by -t will bedumped regardless ofthose switches, and non-table objects will not be dumped.

    Note: When -tis specified, pg_dump makes no attemptto dump any other databaseobjects thatthe selected table(s) might depend upon. Therefore, there is no guaranteethatthe results of a specific-table dump can be successfully restored by themselvesinto a clean database.

    Note: The behavior ofthe -t switch is not entirely upward compatible with pre-8.2PostgreSQL versions. Formerly, writing -t tab would dump alltables named tab,

    but now it just dumps whichever one is visible in your default search path. To getthe

    old behavior you can write -t '*.tab'. Also, you must write something like -tsch.tabto select a table in a particular schema, ratherthan the old locution of-nsch -t tab.

    -T table--exclude-table=table

    Do not dump any tables matching the table pattern. The pattern is interpreted according to

    the same rules as for-t. -T can be given more than once to exclude tables matching any of

    several patterns.

    When both -t and -T are given, the behavioris to dump justthe tables that match atleast one

    -t switch but no -T switches. If-T appears without-t, then tables matching -T are excludedfrom whatis otherwise a normal dump.

    -v--verbose

    Specifies verbose mode. This will cause pg_dump to output detailed object comments andstart/stop times to the dump file, and progress messages to standard error.

    -V--version

    Printthe pg_dump version and exit.

    -x--no-privileges --no-acl

  • 8/6/2019 Backups en Postgre

    15/29

    Prevent dumping of access privileges (grant/revoke commands).

    -Z 0..9--compress=0..9

    Specify the compression levelto use. Zero means no compression. Forthe custom archive

    format, this specifies compression ofindividualtable-data segments, and the defaultis tocompress at a moderate level. For plain text output, setting a nonzero compression levelcauses the entire output file to be compressed, as though it had been fed through gzip; butthedefaultis notto compress. The tar archive format currently does not support compression atall.

    --binary-upgrade

    This option is for use by in-place upgrade utilities. Its use for other purposes is notrecommended or supported. The behavior ofthe option may change in future releases withoutnotice.

    --column-inserts --attribute-inserts

    Dump data as INSERT commands with explicit column names (INSERT INTOtable(column, ...) VALUES ... ). This will make restoration very slow; itis mainly useful formaking dumps that can be loaded into non-PostgreSQL databases. However, since this optiongenerates a separate command for each row, an errorin reloading a row causes only that rowto be lost ratherthan the entire table contents.

    --disable-dollar-quoting

    This option disables the use of dollar quoting for function bodies, and forces them to be

    quoted using SQL standard string syntax.

    --disable-triggers

    This option is only relevant when creating a data-only dump. Itinstructs pg_dump to includecommands to temporarily disable triggers on the targettables while the data is reloaded. Usethis if you have referentialintegrity checks or othertriggers on the tables that you do notwantto invoke during data reload.

    Presently, the commands emitted for--disable-triggers must be done as superuser. So,

    you should also specify a superuser name with -S, or preferably be carefulto starttheresulting script as a superuser.

    This option is only meaningful forthe plain-text format. Forthe archive formats, you can

    specify the option when you callpg_restore.

    --inserts

    Dump data as INSERT commands (ratherthan COPY). This will make restoration very slow; itis mainly useful for making dumps that can be loaded into non-PostgreSQL databases.However, since this option generates a separate command for each row, an errorin reloading

  • 8/6/2019 Backups en Postgre

    16/29

    a row causes only that row to be lost ratherthan the entire table contents. Note thatthe restoremight fail altogetherif you have rearranged column order. The --column-inserts option issafe against column order changes, though even slower.

    --lock-wait-timeout=timeout

    Do not wait foreverto acquire shared table locks atthe beginning ofthe dump. Instead failifunable to lock a table within the specified timeout. The timeout may be specified in any ofthe formats accepted by SET statement_timeout . (Allowed values vary depending on theserver version you are dumping from, but an integer number of milliseconds is accepted byall versions since 7.3. This option is ignored when dumping from a pre-7.3 server.)

    --no-security-labels

    Do not dump security labels.

    --no-tablespaces

    Do not output commands to selecttablespaces. With this option, all objects will be created inwhichevertablespace is the default during restore.

    This option is only meaningful forthe plain-text format. Forthe archive formats, you can

    specify the option when you callpg_restore.

    --no-unlogged-table-data

    Do not dump the contents of unlogged tables. This option has no effect on whether or notthetable definitions (schema) are dumped; it only suppresses dumping the table data.

    --quote-all-identifiers

    Force quoting of allidentifiers. This may be useful when dumping a database for migration toa future version that may have introduced additional keywords.

    --serializable-deferrable

    Use a serializable transaction forthe dump, to ensure thatthe snapshot used is consistentwith later database states; but do this by waiting for a pointin the transaction stream at whichno anomalies can be present, so thatthere isn't a risk ofthe dump failing or causing other

    transactions to roll back with a serialization_failure . See Chapter 13 for moreinformation abouttransaction isolation and concurrency control.

    This option is not beneficial for a dump which is intended only for disaster recovery. It couldbe useful for a dump used to load a copy ofthe database for reporting or other read-only loadsharing while the original database continues to be updated. Withoutitthe dump may reflecta state which is not consistent with any serial execution ofthe transactions eventuallycommitted. For example, if batch processing techniques are used, a batch may show as closedin the dump without all ofthe items which are in the batch appearing.

    This option will make no difference ifthere are no read-write transactions active whenpg_dump is started. If read-write transactions are active, the start ofthe dump may be delayed

  • 8/6/2019 Backups en Postgre

    17/29

    for an indeterminate length oftime. Once running, performance with or withoutthe switch isthe same.

    --use-set-session-authorization

    Output SQL-standard SET SESSION AUTHORIZATION commands instead ofALTER OWNER

    commands to determine object ownership. This makes the dump more standards-compatible,but depending on the history ofthe objects in the dump, might not restore properly. Also, adump using SET SESSION AUTHORIZATION will certainly require superuser privileges to

    restore correctly, whereas ALTER OWNER requires lesser privileges.

    -?--help

    Show help about pg_dump command line arguments, and exit.

    The following command-line options controlthe database connection parameters.

    -h host--host=host

    Specifies the host name ofthe machine on which the serveris running. Ifthe value beginswith a slash, itis used as the directory forthe Unix domain socket. The defaultis taken from

    the PGHOST environment variable, if set, else a Unix domain socket connection is attempted.

    -p port--port=port

    Specifies the TCP port orlocal Unix domain socket file extension on which the serveris

    listening for connections. Defaults to the PGPORT environment variable, if set, or a compiled-in default.

    -U username--username=username

    User name to connect as.

    -w--no-password

    Neverissue a password prompt. Ifthe server requires password authentication and a

    password is not available by other means such as a .pgpass file, the connection attempt willfail. This option can be usefulin batch jobs and scripts where no useris presentto enter a

    password.

    -W--password

    Force pg_dump to prompt for a password before connecting to a database.

    This option is never essential, since pg_dump will automatically prompt for a password iftheserver demands password authentication. However, pg_dump will waste a connection attempt

  • 8/6/2019 Backups en Postgre

    18/29

    finding outthatthe server wants a password. In some cases itis worth typing -Wto avoid theextra connection attempt.

    --role=rolename

    Specifies a role name to be used to create the dump. This option causes pg_dump to issue a

    SET ROLErolename command after connecting to the database. Itis useful when theauthenticated user (specified by -U) lacks privileges needed by pg_dump, but can switch to arole with the required rights. Some installations have a policy againstlogging in directly as asuperuser, and use ofthis option allows dumps to be made without violating the policy.

    Environment

    PGDATABASEPGHOSTPGOPTIONSPGPORTPGUSER

    Default connection parameters.

    This utility, like most otherPostgreSQL utilities, also uses the environment variables supported bylibpq (see Section 31.13).

    Diagnostics

    pg_dump internally executes SELECT statements. If you have problems running pg_dump, make sureyou are able to selectinformation from the database using, for example,psql. Also, any defaultconnection settings and environment variables used by the libpq front-end library will apply.

    The database activity of pg_dump is normally collected by the statistics collector. Ifthis is

    undesirable, you can set parametertrack_countsto false via PGOPTIONS orthe ALTER USER command.

    Notes

    If your database cluster has any local additions to the template1 database, be carefulto restore theoutput of pg_dump into a truly empty database; otherwise you are likely to get errors due toduplicate definitions ofthe added objects. To make an empty database without any local additions,

    copy from template0 nottemplate1, for example:

    CREATE DATABASE foo WITH TEMPLATE template0;

    When a data-only dump is chosen and the option --disable-triggers is used, pg_dump emitscommands to disable triggers on usertables before inserting the data, and then commands to re-enable them afterthe data has been inserted. Ifthe restore is stopped in the middle, the systemcatalogs might be leftin the wrong state.

  • 8/6/2019 Backups en Postgre

    19/29

    Members oftar archives are limited to a size less than 8 B. (This is an inherentlimitation ofthe tarfile format.) Therefore this format cannot be used ifthe textual representation of any one table

    exceeds that size. The total size of a tar archive and any ofthe other output formats is notlimited,except possibly by the operating system.

    The dump file produced by pg_dump does not contain the statistics used by the optimizerto make

    query planning decisions. Therefore, itis wise to run ANALYZE after restoring from a dump file toensure optimal performance; see Section 23.1.3 and Section 23.1.5 for more information. The dump

    file also does not contain any ALTER DATABASE ... SET commands; these settings are dumped bypg_dumpall, along with database users and otherinstallation-wide settings.

    Because pg_dump is used to transfer data to newer versions ofPostgreSQL, the output of pg_dumpcan be expected to load into PostgreSQL server versions newerthan pg_dump's version. pg_dumpcan also dump from PostgreSQL servers olderthan its own version. (Currently, servers backtoversion 7.0 are supported.) However, pg_dump cannot dump from PostgreSQL servers newerthan itsown major version; it will refuse to even try, ratherthan risk making an invalid dump. Also, itis notguaranteed that pg_dump's output can be loaded into a server of an older major version not evenifthe dump was taken from a server ofthat version. Loading a dump file into an older server may

    require manual editing ofthe dump file to remove syntax not understood by the older server.

    Examples

    To dump a database called mydbinto a SQL-script file:

    $ pg_dump mydb > db.sql

    To reload such a scriptinto a (freshly created) database namednewdb:

    $ psql -d newdb -f db.sql

    To dump a database into a custom-format archive file:

    $ pg_dump -Fc mydb > db.dump

    To dump a database into a directory-format archive:

    $ pg_dump -Fd mydb -f dumpdir

    To reload an archive file into a (freshly created) database namednewdb:

    $ pg_restore -d newdb db.dump

    To dump a single table named mytab:

    $ pg_dump -t mytab mydb > db.sql

    To dump alltables whose names start with empin the detroit schema, except forthe table named

    employee_log:

    $ pg_dump -t 'detroit.emp*' -T detroit.employee_log mydb > d b.sql

  • 8/6/2019 Backups en Postgre

    20/29

    To dump all schemas whose names start with east orwest and end in gsm, excluding any schemas

    whose names contain the word test:

    $ pg_dump -n 'east*gsm' -n 'west*gsm' -N '*test*' mydb > db.sql

    The same, using regular expression notation to consolidate the switches:

    $ pg_dump -n '(east|west)*gsm' -N '*test*' mydb > db.sql

    To dump all database objects except fortables whose names begin withts_:

    $ pg_dump -T 'ts_*' mydb > db.sql

    To specify an upper-case or mixed-case name in -t and related switches, you need to double-quote

    the name; else it will be folded to lower case (seePatterns). But double quotes are specialto theshell, so in turn they must be quoted. Thus, to dump a single table with a mixed-case name, you need

    something like

    $ pg_dump -t '"MixedCaseName"' mydb > mytab.sql

    See Also

    pg_dumpall,pg_restore,psql

  • 8/6/2019 Backups en Postgre

    21/29

    pg_restore

    Name

    pg_restore -- restore aP

    ostgreSQL database from an archive file created by pg_dump

    Synopsis

    pg_restore [connection-option...] [option...] [filename]

    Description

    pg_restore is a utility for restoring a PostgreSQL database from an archive created bypg_dumpinone ofthe non-plain-text formats. It willissue the commands necessary to reconstructthe database tothe state it was in atthe time it was saved. The archive files also allow pg_restore to be selective

    about whatis restored, or even to reorderthe items priorto being restored. The archive files aredesigned to be portable across architectures.

    pg_restore can operate in two modes. If a database name is specified, pg_restore connects to thatdatabase and restores archive contents directly into the database. Otherwise, a script containing theSQL commands necessary to rebuild the database is created and written to a file or standard output.This script outputis equivalentto the plain text output format of pg_dump. Some ofthe optionscontrolling the output are therefore analogous to pg_dump options.

    Obviously, pg_restore cannot restore information thatis not presentin the archive file. Forinstance,

    ifthe archive was made using the "dump data as INSERT commands" option, pg_restore will not be

    able to load the data using COPY statements.

    Options

    pg_restore accepts the following command line arguments.

    filename

    Specifies the location ofthe archive file (or directory, for a directory-format archive) to berestored. If not specified, the standard inputis used.

    -a--data-only

    Restore only the data, notthe schema (data definitions).

    -c--clean

    Clean (drop) database objects before recreating them.

  • 8/6/2019 Backups en Postgre

    22/29

    -C--create

    Create the database before restoring into it. (When this option is used, the database named

    with -dis used only to issue the initialCREATE DATABASE command. All data is restored intothe database name that appears in the archive.)

    -d dbname--dbname=dbname

    Connectto database dbname and restore directly into the database.

    -e--exit-on-error

    Exitif an erroris encountered while sending SQL commands to the database. The defaultisto continue and to display a count of errors atthe end ofthe restoration.

    -f filename

    --file=filename

    Specify output file for generated script, or forthe listing when used with -l. Defaultis thestandard output.

    -F format--format=format

    Specify format ofthe archive. Itis not necessary to specify the format, since pg_restore willdetermine the format automatically. If specified, it can be one ofthe following:

    c

    custom

    The archive is in the custom format of pg_dump.

    ddirectory

    The archive is a directory archive.

    ttar

    The archive is a tar archive.

    -i--ignore-version

    A deprecated option thatis now ignored.

    -I index--index=index

  • 8/6/2019 Backups en Postgre

    23/29

    Restore definition of named index only.

    -j number-of-jobs --jobs=number-of-jobs

    Run the mosttime-consuming parts of pg_restore those which load data, create indexes, or

    create constraints using multiple concurrent jobs. This option can dramatically reduce thetime to restore a large database to a server running on a multiprocessor machine.

    Each job is one process or one thread, depending on the operating system, and uses a separateconnection to the server.

    The optimal value forthis option depends on the hardware setup ofthe server, ofthe client,and ofthe network. Factors include the number of CPU cores and the disk setup. A good

    place to startis the number of CPU cores on the server, but values largerthan that can alsolead to faster restore times in many cases. Of course, values that are too high willlead todecreased performance because ofthrashing.

    Only the custom archive formatis supported with this option. The input file must be a regularfile (not, for example, a pipe). This option is ignored when emitting a script ratherthanconnecting directly to a database server. Also, multiple jobs cannot be used together with theoption --single-transaction .

    -l--list

    Listthe contents ofthe archive. The output ofthis operation can be used as inputto the -Loption. Note thatif filtering switches such as -n or-t are used with -l, they will restricttheitems listed.

    -L list-file--use-list=list-file

    Restore only those archive elements that are listed inlist-file, and restore them in the

    orderthey appearin the file. Note thatif filtering switches such as -n or-t are used with -L,they will further restrictthe items restored.

    list-fileis normally created by editing the output of a previous -l operation. Lines can be

    moved or removed, and can also be commented out by placing a semicolon (;) atthe start ofthe line. See below for examples.

    -n namespace

    --schema=schema

    Restore only objects that are in the named schema. This can be combined with the -t optionto restore just a specific table.

    -O--no-owner

  • 8/6/2019 Backups en Postgre

    24/29

    Do not output commands to set ownership of objects to match the original database. Bydefault, pg_restore issues ALTER OWNER orSET SESSION AUTHORIZATION statements to setownership of created schema elements. These statements will fail unless the initialconnection to the database is made by a superuser (orthe same userthat owns all ofthe

    objects in the script). With -O, any user name can be used forthe initial connection, and thisuser will own allthe created objects.

    -Pfunction-name(argtype [, ...]) --function=function-name(argtype [, ...])

    Restore the named function only. Be carefulto spellthe function name and arguments exactly

    as they appearin the dump file's table of contents.

    -R--no-reconnect

    This option is obsolete but still accepted for backwards compatibility.

    -s--schema-only

    Restore only the schema (data definitions), notthe data (table contents). Current sequence

    values will not be restored, either. (Do not confuse this with the --schema option, which usesthe word "schema" in a different meaning.)

    -S username--superuser=username

    Specify the superuser user name to use when disabling triggers. This is only relevantif--

    disable-triggers is used.

    -t table--table=table

    Restore definition and/or data of named table only. This can be combined with the -n optionto specify a schema.

    -T trigger--trigger=trigger

    Restore named trigger only.

    -v

    --verbose

    Specifies verbose mode.

    -V--version

    Printthe pg_restore version and exit.

  • 8/6/2019 Backups en Postgre

    25/29

    -x--no-privileges --no-acl

    Prevent restoration of access privileges (grant/revoke commands).

    -1--single-transaction

    Execute the restore as a single transaction (thatis, wrap the emitted commands in

    BEGIN/COMMIT). This ensures that either allthe commands complete successfully, or no

    changes are applied. This option implies --exit-on-error.

    --disable-triggers

    This option is only relevant when performing a data-only restore. Itinstructs pg_restore toexecute commands to temporarily disable triggers on the targettables while the data isreloaded. Use this if you have referentialintegrity checks or othertriggers on the tables thatyou do not wantto invoke during data reload.

    Presently, the commands emitted for--disable-triggers must be done as superuser. So,you should also specify a superuser name with -S, or preferably run pg_restore as aPostgreSQL superuser.

    --no-data-for-failed-tables

    By default, table data is restored even ifthe creation command forthe table failed (e.g.,because it already exists). With this option, data for such a table is skipped. This behaviorisusefulifthe target database already contains the desired table contents. For example,auxiliary tables forPostgreSQL extensions such as PostGIS might already be loaded in thetarget database; specifying this option prevents duplicate or obsolete data from being loadedinto them.

    This option is effective only when restoring directly into a database, not when producingSQL script output.

    --no-security-labels

    Do not output commands to restore security labels, even ifthe archive contains them.

    --no-tablespaces

    Do not output commands to selecttablespaces. With this option, all objects will be created inwhichevertablespace is the default during restore.

    --use-set-session-authorization

    Output SQL-standard SET SESSION AUTHORIZATION commands instead ofALTER OWNERcommands to determine object ownership. This makes the dump more standards-compatible,

    but depending on the history ofthe objects in the dump, might not restore properly.

  • 8/6/2019 Backups en Postgre

    26/29

    -?--help

    Show help about pg_restore command line arguments, and exit.

    pg_restore also accepts the following command line arguments for connection parameters:

    -h host--host=host

    Specifies the host name ofthe machine on which the serveris running. Ifthe value beginswith a slash, itis used as the directory forthe Unix domain socket. The defaultis taken from

    the PGHOST environment variable, if set, else a Unix domain socket connection is attempted.

    -p port--port=port

    Specifies the TCP port orlocal Unix domain socket file extension on which the serveris

    listening for connections. Defaults to the PGPORT environment variable, if set, or a compiled-in default.

    -U username--username=username

    User name to connect as.

    -w--no-password

    Neverissue a password prompt. Ifthe server requires password authentication and a

    password is not available by other means such as a .pgpass file, the connection attempt will

    fail. This option can be usefulin batch jobs and scripts where no useris presentto enter apassword.

    -W--password

    Force pg_restore to prompt for a password before connecting to a database.

    This option is never essential, since pg_restore will automatically prompt for a password ifthe server demands password authentication. However, pg_restore will waste a connection

    attempt finding outthatthe server wants a password. In some cases itis worth typing -Wtoavoid the extra connection attempt.

    --role=rolename

    Specifies a role name to be used to perform the restore. This option causes pg_restore to issue

    a SET ROLErolename command after connecting to the database. Itis useful when the

    authenticated user (specified by -U) lacks privileges needed by pg_restore, but can switch to arole with the required rights. Some installations have a policy againstlogging in directly as a

    superuser, and use ofthis option allows restores to be performed without violating the policy.

  • 8/6/2019 Backups en Postgre

    27/29

    Environment

    PGHOSTPGOPTIONSPGPORTPGUSER

    Default connection parameters

    This utility, like most otherPostgreSQL utilities, also uses the environment variables supported bylibpq (see Section 31.13).

    Diagnostics

    When a direct database connection is specified using the -d option, pg_restore internally executesSQL statements. If you have problems running pg_restore, make sure you are able to selectinformation from the database using, for example,psql. Also, any default connection settings and

    environment variables used by the libpq front-end library will apply.

    Notes

    If yourinstallation has any local additions to the template1 database, be carefulto load the output ofpg_restore into a truly empty database; otherwise you are likely to get errors due to duplicatedefinitions ofthe added objects. To make an empty database without any local additions, copy from

    template0 nottemplate1, for example:

    CREATE DATABASE foo WITH TEMPLATE template0;

    The limitations of pg_restore are detailed below.

    y When restoring data to a pre-existing table and the option --disable-triggers is used,pg_restore emits commands to disable triggers on usertables before inserting the data, thenemits commands to re-enable them afterthe data has been inserted. Ifthe restore is stopped inthe middle, the system catalogs might be leftin the wrong state.

    y pg_restore cannot restore large objects selectively; forinstance, only those for a specifictable. If an archive contains large objects, then alllarge objects will be restored, or none of

    them ifthey are excluded via -L, -t, or other options.

    See also thepg_dump documentation for details on limitations of pg_dump.

    Once restored, itis wise to run ANALYZE on each restored table so the optimizer has useful statistics;see Section 23.1.3 and Section 23.1.5 for more information.

    Examples

    Assume we have dumped a database called mydbinto a custom-format dump file:

    $ pg_dump -Fc mydb > db.dump

  • 8/6/2019 Backups en Postgre

    28/29

  • 8/6/2019 Backups en Postgre

    29/29

    could be used as inputto pg_restore and would only restore items 10 and 6, in that order:

    $ pg_restore -L db.list db.dump

    See Also

    pg_dump,pg_dumpall,psql