PostgreSQL is a one of the robust, open source database server. Like MySQL 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 for backing 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 payrollHowever, in real life you need to compress database:
$ psql payroll $ pg_dump payroll | gzip -c > payroll.dump.out.gzTo restore database use the following command:$ gunzip payroll.dump.out.gzHere is a shell script for same task:
$ psql -d payroll -f payroll.dump.out
#!/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 it dumps (backs up) each database, and preserves cluster-wide data such as users 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:
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_a<script language="javascript1.1" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-7825705102693166&oe=utf-8&dt=1217401112419&lmt=1217399696&prev_slotnames=0573728892&output=textlink&slotname=1427449066&correlator=1217401112248&url=http%3a%2f%2fwww.cyberciti.biz%2ftips%2fhowto-backup-postgresql-databases.html&region=_google_cpa_region_&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&frm=0&ff=verdana&cc=100&ga_vid=2836092585708641000.1217401112&ga_sid=1217401112&ga_hid=139355008&flash=9.0.124&u_h=1050&u_w=1680&u_ah=1020&u_aw=1680&u_cd=32&u_tz=480&u_his=1&u_java=true&u_nplug=15&u_nmime=59"></script>lug=15&u_nmime=59">
本文介绍了如何使用PostgreSQL自带的工具进行数据库备份和恢复操作。包括登录pgsql用户、列出所有数据库、使用pg_dump命令备份单个数据库以及压缩备份文件的方法。此外还提供了使用pg_dumpall命令备份所有数据库及集群数据的示例。
579

被折叠的 条评论
为什么被折叠?



