PostgreSQL database backup

PostgreSQL database backup pg_dump

First, backup and restore

  Note: pg_dump command in the directory

1, database backup test

pg_dump -h 127.0.0.1 -p 5432 -U username -c -f db_back.sql test

2, restore the data to a database test2

psql -U postgres -f /db_back.sql test2

 

Second, the command Detailed

  • Basic Commands
pg_dump [OPTION]... [DBNAME]

  Note: The database name put Finally, do not specify the default system variable PGDATABASE specified database.

 

  •  Detailed  

General options :( general options)
  -f, --file = save after export of FILENAME output file or directory name filename
  -F, --format = c | d | t | p output file format (custom, directory, tar, plain text format export file (default))
  -j, --jobs use the this MANY NUM = parallel to the dump Jobs concurrent
  -v, --verbose verbose mode verbose mode
  -V, --version output version information, then exit the output version information, then exit
  -Z, --compress = 0-9 compression level for compressed formats is a compressed format compression levels
  --lock-wait-timeout = tIMEOUT fail after waiting tIMEOUT for a table lock wait list in the operating lock out failure
  - ?, --help show this help, then exit show this help, then exit

options controlling the output content :( output control options)
  -a, --data-only dump only the data, not the schema of data to export only, does not include a mode
  -b, --blobs include large objects in dump comprises a large object in the dump
  -c, --clean clean (drop ) database objects before recreating before re-create, to clear (delete) database objects
  before -C, --create include commands to create database in dump commands included in the dump in order to create a database (including building a database statement without import first build the database)
  -E, the dump --encoding = eNCODING the encoding eNCODING data Transfer in the form of data encoded in eNCODING
  -n, --schema = SCHEMA dump the named schema (s) only dump mode only the specified name
  - N, --exclude-schema = SCHEMA do nOT dump the named schema (s) is not named dump mode
  -o, --oids include OIDs in dump dump comprising OID
  -O, --no-owner skip restoration of object ownership in plaintext format, is ignored by the object belongs to restore the format text-Plain
  -s, only the dump --schema The Schema-only, NO Data dump mode only, not including data (not export data)
  -S, --superuser nAME = user name to use in the superuser Plain text-dump in the format specified root name
  -t, --table = TABLE dump the named table (s) only dumps only the specified name table
  -T, --exclude-table = tABLE do NOT dump the named table (s) only dump the table specified name
  -x, --no-privileges do not dump privileges (grant / revoke) Do not dump permission (Grant / REVOKE)
  --binary-upgrade for use by upgrade Utilities only be used only by the upgrade tool
  --column-inserts dump data as INSERT commands INSERT commands with column names to column names with the form of dump data
  --disable-dollar-quoting disable dollar quoting , use SQL standard quoting cancel dollar (sign) in quotation marks, using standard SQL quotes
  --disable-triggers disable triggers during data- only restore disable the trigger only in the process of restoring data
  - exclude-table-data = TABLE do nOT dump data for the named table (s) to the INSERT command, rather than the form of the COPY command dump data
  --inserts dump the dump INSERT AS data commands, COPY rather Within last
  --no-Security-Labels not the dump Security label Assignments do
  --no-snapshots do not use the synchronized-snapshots in the synchronized Parallel Jobs
  --no-not the tablespaces do not dump the dump Assignments tABLESPACE table space allocation information
  --no-unlogged-table-data do not dump Data Table unlogged
  --quote All-All-quote identifiers identifiers, the even Key words IF Not
  = the dump the named sectionTop the SECTION --section (pre-Data, Data, or Data-POST)
  --serializable DEFERRABLE the wait-CAN RUN an until the without the dump Anomalies The
  --use the session-Authorization-SET-
                               use of the SET the SESSION the AUTHORIZATION Commands INSTEAD
                               the ALTER Commands to the SET Ownership OWNER

connection options :( control option connection)
  -d, --dbname = DBNAME database to dump the database name
  -h, --host = hOSTNAME database server host or a host name or database server socket socket directory directory
  -p, --port = port number pORT database server port number of the database server
  -U, --username = NAME connect as specified database user to specify the database user to connect
  -w, --no-password never prompt for password forever Do not prompt for a password
  -W, --password force password prompt (should happen automatically) Force password prompt (auto)
  --role = ROLENAME do the SET before the dump the ROLE

 

A: a flat file format script: 
Example:
1. postgres database data export only, excluding mode -s
    

pg_dump - the U-Postgres - F / postgres.sql - S Postgres (database)

2. Export postgres database (including data)

   

pg_dump - U Postgres - f / postgres.sql Postgres (database name)

 

3. Export data postgres database tables of test01
    

Create  Database "Test01" with owner = "Postgres" encoding = ' UTF-. 8 ' ; (single and double quotation marks can not be wrong) 
pg_dump - the U-Postgres - F / postgres.sql - T Test01 Postgres (database)

4. Export test01 postgres database data table to insert statements in the form of

   

pg_dump - the U-Postgres - F / postgres.sql - T Test01 - column-inserts Postgres (database)

 


The restored data to the database bk01
   

psql -U postgres -f /postgres.sql bk01

 


Second, the use of archive formats:
pg_restore
use plain text pg_restore restore script in plain text format can not restore
[root @ localhost postgres-9.3.5] # pg_restore -U postgres -d bk01 / mnt / hgfs / window \ & ubuntu \ shared \ Folder / vendemo.sql 
pg_restore: [Archiver] the INPUT file the Appears to BE A text format dump Please use psql..

use with pg_restore to rebuild the database and archive file formats.

1. To back up: 
   pg_dump -U Postgres -F T -f /vendemo.tar vendemo backed up more than 800 k
 Recovery:.
   Pg_restore to Postgres -d -U BK01 /vendemo.tar 
2. First backup: 
   pg_dump -U Postgres -F c -f /vendemo.tar vendemo backed up more than 300 k
 recovery:
   pg_restore -d -U Postgres BK01 /vendemo.tar 

Third, compressed backup and recovery:
processing large databases:
1. use compressed dumps use your favorite compression program, for example gzip.
 . Back up:
   pg_dump -U postgres vendemo | gzip> /vendemo.gz backed down only 30 k
 recovery:.
   gunzip -c /vendemo.gz | Postgres psql -U bk02
 or
   CAT /vendemo.gz | gunzip | bk02 Postgres psql -U
2. use split. . Split command allows you to use the following method your output decomposed into the operating system can accept size. For example, so that each block size of 1 megabyte: 
 first backup:.
   Pg_dump -U Postgres -d vendemo | Split -b 100K - / Vend / Vend
   guide out of the way is 100K vendaa
   vendab 100K
   vendac 100K
   vendad 16K
 . Recovery:
  cat / vend / vend * | psql -U postgres bk02

Guess you like

Origin www.cnblogs.com/BillyYoung/p/11057854.html
Recommended