Howto Backup PostgreSQL Databases Server With pg_dump command

来源:互联网 发布:优化很好的单机游戏 编辑:程序博客网 时间:2024/06/08 07:59
 Trac back: http://www.cyberciti.biz/tips/howto-backup-postgresql-databases.html

PostgreSQL is a one of the robust, open source database server. LikeMySQL database server, it provides 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 forbacking up a PostgreSQL database. 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.outTo restore a payroll database:
$ psql -d payroll -f payroll.dump.outOR$ createdb payroll
$ psql payroll
However, in real life you need to compress database:$ pg_dump payroll | gzip -c > payroll.dump.out.gzTo restore database use the following command:$ gunzip payroll.dump.out.gz
$ psql -d payroll -f payroll.dump.out
Here is a shell script for same task:

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

Another option is use to pg_dumpall command. As a name suggest itdumps (backs up) each database, and preserves cluster-wide data such asusers and groups. You can use it as follows:$ pg_dumpall > all.dbs.outOR$ pg_dumpall | gzip -c > all.dbs.out.gzTo restore backup use the following command:
$ psql -f all.dbs.out postgresReferences:

  • PostgreSQL documentation : Backup and Restore chapter

<script type="text/javascript"><!--google_ad_client = "pub-7825705102693166";google_ad_slot = "1427449066";google_ad_output = "textlink";google_ad_format = "ref_text";google_cpa_choice = ""; // on file//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script><script language="JavaScript1.1" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-7825705102693166&amp;oe=UTF-8&amp;dt=1217401112419&amp;lmt=1217399696&amp;prev_slotnames=0573728892&amp;output=textlink&amp;slotname=1427449066&amp;correlator=1217401112248&amp;url=http%3A%2F%2Fwww.cyberciti.biz%2Ftips%2Fhowto-backup-postgresql-databases.html&amp;region=_google_cpa_region_&amp;ref=http%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Dpostgresql%2Bbackup%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dorg.mozilla%3Aen-GB%3Aofficial%26client%3Dfirefox-a&amp;frm=0&amp;ff=Verdana&amp;cc=100&amp;ga_vid=2836092585708641000.1217401112&amp;ga_sid=1217401112&amp;ga_hid=139355008&amp;flash=9.0.124&amp;u_h=1050&amp;u_w=1680&amp;u_ah=1020&amp;u_aw=1680&amp;u_cd=32&amp;u_tz=480&amp;u_his=1&amp;u_java=true&amp;u_nplug=15&amp;u_nmime=59"></script>

原创粉丝点击