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.out
To restore a payroll database:
$ psql -d payroll -f payroll.dump.out
OR$ createdb payroll
However,in real life you need to compress database:
$ psql payroll $ pg_dump payroll | gzip -c > payroll.dump.out.gz
To restore database use the following command:$ gunzip payroll.dump.out.gz
Here 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.out
OR$ pg_dumpall | gzip -c > all.dbs.out.gz
To restore backup use the following command:
$ psql -f all.dbs.out postgres
References:
原文地址:https://www.jb51.cc/postgresql/197355.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。