五、使用纯文本格式的备份文件备份数据库
下面我们详细介绍如何使用pg_dump程序创建数据库的纯文本格式的备份文件。
第一步:登录到Postgres Plus数据库服务器所在的计算机。在该计算机上任何有效的帐户都可以。
第二步:使用cd命令将当前工作目录切换至Postgres Plus所在的bin目录。
cd /opt/PostgresPlus/8.4SS/bin
注意:如果您使用的是Postgres Plus Advanced Server的话,请将当前工作目录通过cd命令切换至dbserver/bin。
第三步:使用具有超级用户权限(-U选项)的角色运行pg_dump程序,使用–C来包含备份文件中的CREATE DATABASE语句,给备份文件指定的名称(-f选项),以及用来制作备份文件的数据库的名称(命令行的最后一个参数)。
./pg_dump -U postgres -C -f /home/user/sample_backup sample
上面的命令就会为数据库sample建立一个备份文件,名为sample_backup。下面是sample_backup文件的一部分内容:
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;
--
-- Name: sample; Type: DATABASE; Schema: -; Owner: postgres
--
CREATE DATABASE sample WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE
= 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8';
ALTER DATABASE sample OWNER TO postgres;
\connect sample
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;
--
-- Name: plpgsql; Type: PROCEDURAL LANGUAGE; Schema: -; Owner: postgres
--
CREATE PROCEDURAL LANGUAGE plpgsql;
ALTER PROCEDURAL LANGUAGE plpgsql OWNER TO postgres;
SET search_path = public, pg_catalog;
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;
--
-- Name: sample; Type: DATABASE; Schema: -; Owner: postgres
--
CREATE DATABASE sample WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE
= 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8';
ALTER DATABASE sample OWNER TO postgres;
\connect sample
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;
--
-- Name: plpgsql; Type: PROCEDURAL LANGUAGE; Schema: -; Owner: postgres
--
CREATE PROCEDURAL LANGUAGE plpgsql;
ALTER PROCEDURAL LANGUAGE plpgsql OWNER TO postgres;
SET search_path = public, pg_catalog;