日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

postgresql 备份恢复(一)

發(fā)布時(shí)間:2024/9/5 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 postgresql 备份恢复(一) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

備份恢復(fù)對(duì)于每個(gè)數(shù)據(jù)來(lái)說(shuō)都是非常重要的。一般的數(shù)據(jù)庫(kù)都支持冷備份的方式,冷備份可以保證數(shù)據(jù)庫(kù)在此刻的完整性。但是其缺點(diǎn)也非常的明顯,為保持?jǐn)?shù)據(jù)一致性。冷備份期間數(shù)據(jù)庫(kù)中相關(guān)數(shù)據(jù)是不能夠使用的,就大大影響的系統(tǒng)的可用性。不管怎樣冷備份在很多的情況下還是很有用的。

數(shù)據(jù)庫(kù)的冷備份一般支持兩種方式:

1,操作系統(tǒng)級(jí)別的命令備份(cp,copy)

2,數(shù)據(jù)庫(kù)工具備份(pg_dump)

針對(duì)postgresql數(shù)據(jù)庫(kù)的pg_dump工具進(jìn)行了一下測(cè)試(還碰到一個(gè)小問(wèn)題)。

pg_dump工具命令與參數(shù)的參考:

?

代碼 pg_dump dumps a database as a text file or to other formats.

Usage:
pg_dump [OPTION]... [DBNAME]

General options:
-f, --file=FILENAME output file name
-F, --format=c|t|p output file format (custom, tar, plain text)
-i, --ignore-version proceed even when server version mismatches
pg_dump version
-v, --verbose verbose mode
-Z, --compress=0-9 compression level for compressed formats
--help show this help, then exit
--version output version information, then exit

Options controlling the output content:
-a, --data-only dump only the data, not the schema
-b, --blobs include large objects in dump
-c, --clean clean (drop) schema prior to create
-C, --create include commands to create database in dump
-d, --inserts dump data as INSERT commands, rather than COPY
-D, --column-inserts dump data as INSERT commands with column names
-E, --encoding=ENCODING dump the data in encoding ENCODING
-n, --schema=SCHEMA dump the named schema(s) only
-N, --exclude-schema=SCHEMA do NOT dump the named schema(s)
-o, --oids include OIDs in dump
-O, --no-owner skip restoration of object ownership
in plain text format
-s, --schema-only dump only the schema, no data
-S, --superuser=NAME specify the superuser user name to use in
plain text format
-t, --table=TABLE dump the named table(s) only
-T, --exclude-table=TABLE do NOT dump the named table(s)
-x, --no-privileges do not dump privileges (grant/revoke)
--disable-dollar-quoting disable dollar quoting, use SQL standard quoting
--disable-triggers disable triggers during data-only restore
--use-set-session-authorization
use SESSION AUTHORIZATION commands instead of
ALTER OWNER commands to set ownership

Connection options:
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port number
-U, --username=NAME connect as specified database user
-W, --password force password prompt (should happen automatically)

If no database name is supplied, then the PGDATABASE environment
variable value is used.

Report bugs to <pgsql-bugs@postgresql.org>.

通過(guò)pg_dump工具分別進(jìn)行數(shù)據(jù)庫(kù),表,schema方式的備份。

?

1,數(shù)據(jù)庫(kù)備份

?

代碼 [postgre@daduxiong bin]$ dropdb wyz
[postgre@daduxiong bin]$ createdb wyz
[postgre@daduxiong bin]$ psql wyz
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

wyz=# create table abc(id integer);
CREATE TABLE
wyz=# insert into abc values(1);
INSERT 0 1
wyz=# \q
[postgre@daduxiong bin]$ pg_dump wyz >/usr/local/pgsql/data/wyz.dmp
[postgre@daduxiong bin]$ dropdb wyz
[postgre@daduxiong bin]$ createdb wyz
[postgre@daduxiong bin]$ psql wyz </usr/local/pgsql/data/wyz.dmp
SET
SET
SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
REVOKE
REVOKE
GRANT
GRANT
[postgre@daduxiong bin]$ psql wyz
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

wyz=# select * from abc;
id
----
1
(1 row)

wyz=#
對(duì)于空間不足或者文件較大時(shí)可以采用壓縮的方式。
[postgre@daduxiong bin]$ pg_dump wyz |gzip >/usr/local/pgsql/data/wyz.gz

在恢復(fù)數(shù)據(jù)庫(kù)時(shí),一定要確認(rèn)數(shù)據(jù)庫(kù)已經(jīng)創(chuàng)建。

?

使用pg_dump工具對(duì)數(shù)據(jù)庫(kù)進(jìn)行備份只是備份了數(shù)據(jù)庫(kù)中的數(shù)據(jù),對(duì)于配置文件等非數(shù)據(jù)文件沒(méi)有一起備份。顯然不如使用tar與gzip命令更方便。

2,表備份

?

代碼 [postgre@daduxiong ~]$ psql wyz
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

wyz=# select * from abc;
id
----
1
(1 row)

wyz=# insert into abc values(2);
INSERT 0 1
wyz=# \q
[postgre@daduxiong ~]$ pg_dump wyz --table abc >/usr/local/pgsql/data/abc.sql
[postgre@daduxiong ~]$ psql wyz
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

wyz=# drop table abc;
DROP TABLE
wyz=# \q
[postgre@daduxiong ~]$ more /usr/local/pgsql/data/abc.sql
--
-- PostgreSQL database dump
--

SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;

SET search_path = public, pg_catalog;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: abc; Type: TABLE; Schema: public; Owner: postgre; Tablespace:
--

CREATE TABLE abc (
id integer
);


ALTER TABLE public.abc OWNER TO postgre;

--
-- Data for Name: abc; Type: TABLE DATA; Schema: public; Owner: postgre
--

COPY abc (id) FROM stdin;
1
2
\.


--
-- PostgreSQL database dump complete
--

[postgre@daduxiong ~]$ psql wyz </usr/local/pgsql/data/abc.sql
SET
SET
SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
[postgre@daduxiong ~]$ psql wyz
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

wyz=# select count(*) from abc;
count
-------
2
(1 row)

wyz=#

表備份時(shí)還可以根據(jù)參數(shù)的選用將表的數(shù)據(jù)dump成insert語(yǔ)句。

?

3,SCHEMA備份

?

代碼 [postgre@daduxiong ~]$ rm -rf /usr/local/pgsql/data/123.sql
[postgre@daduxiong ~]$ pg_dump wyz -N postgre -h daduxiong -f /usr/local/pgsql/data/123.sql
[postgre@daduxiong ~]$ pg_dump wyz -N wyz -h daduxiong -f /usr/local/pgsql/data/234.sql
[postgre@daduxiong ~]$ more /usr/local/pgsql/data/123.sql
--
-- PostgreSQL database dump
--

SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;

SET search_path = public, pg_catalog;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: abc; Type: TABLE; Schema: public; Owner: postgre; Tablespace:
--

CREATE TABLE abc (
id integer
);


ALTER TABLE public.abc OWNER TO postgre;

--
-- Data for Name: abc; Type: TABLE DATA; Schema: public; Owner: postgre
--

COPY abc (id) FROM stdin;
1
2
\.


--
-- Name: public; Type: ACL; Schema: -; Owner: postgre
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM postgre;
GRANT ALL ON SCHEMA public TO postgre;
GRANT ALL ON SCHEMA public TO PUBLIC;


--
-- PostgreSQL database dump complete
--

[postgre@daduxiong ~]$ more /usr/local/pgsql/data/234.sql
--
-- PostgreSQL database dump
--

SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;

SET search_path = public, pg_catalog;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: abc; Type: TABLE; Schema: public; Owner: postgre; Tablespace:
--

CREATE TABLE abc (
id integer
);


ALTER TABLE public.abc OWNER TO postgre;

--
-- Data for Name: abc; Type: TABLE DATA; Schema: public; Owner: postgre
--

COPY abc (id) FROM stdin;
1
2
\.


--
-- Name: public; Type: ACL; Schema: -; Owner: postgre
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM postgre;
GRANT ALL ON SCHEMA public TO postgre;
GRANT ALL ON SCHEMA public TO PUBLIC;


--
-- PostgreSQL database dump complete
--

?

在我的wyz數(shù)據(jù)庫(kù)中有兩個(gè)用戶(postgre,wyz),可備份測(cè)試的時(shí)候都是備份的postgre用戶的數(shù)據(jù)。
在采用參數(shù)-n的時(shí)候,提示命令錯(cuò)誤。

[postgre@daxuxiong ~]$ pg_dump wyz -n wyz -f /usr/local/pgsql/data/456.sql
pg_dump: No matching schemas were found.

?

懷疑是bug,有不同結(jié)果的朋友請(qǐng)指正。
postgresql當(dāng)前版本:8.3.10

轉(zhuǎn)載于:https://www.cnblogs.com/daduxiong/archive/2010/08/30/1812403.html

總結(jié)

以上是生活随笔為你收集整理的postgresql 备份恢复(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。