备份:
[postgres9.6@db bin]$ pg_dump -h localhost -U postgres9.6 test > /tmp/test.bak
恢复:
postgres# drop database test;
DROP DATABASE
[postgres9.6@db tmp]$ psql -h localhost -U postgres9.6 -d test < /tmp/test.bak
psql: FATAL: database "test" does not exist
postgres# create database test;
CREATE DATABASE
[postgres9.6@db tmp]$ psql -h localhost -U postgres9.6 -d test < /tmp/test.bak
Null display is "NULL".
Pager is always used.
Timing is on.
SET
Time: 0.137 ms
SET
Time: 0.444 ms
SET
Time: 0.085 ms
SET
Time: 0.069 ms
SET
Time: 0.072 ms
SET
Time: 0.060 ms
SET
Time: 0.060 ms
SET
Time: 0.058 ms
CREATE SCHEMA
Time: 5.376 ms
ALTER SCHEMA
Time: 4.268 ms
CREATE SCHEMA
Time: 1.322 ms
ALTER SCHEMA
Time: 4.456 ms
CREATE EXTENSION
Time: 1.637 ms
COMMENT
Time: 1.
[postgres9.6@db bin]$ pg_dump -h localhost -U postgres9.6 test > /tmp/test.bak
恢复:
postgres# drop database test;
DROP DATABASE
[postgres9.6@db tmp]$ psql -h localhost -U postgres9.6 -d test < /tmp/test.bak
psql: FATAL: database "test" does not exist
postgres# create database test;
CREATE DATABASE
[postgres9.6@db tmp]$ psql -h localhost -U postgres9.6 -d test < /tmp/test.bak
Null display is "NULL".
Pager is always used.
Timing is on.
SET
Time: 0.137 ms
SET
Time: 0.444 ms
SET
Time: 0.085 ms
SET
Time: 0.069 ms
SET
Time: 0.072 ms
SET
Time: 0.060 ms
SET
Time: 0.060 ms
SET
Time: 0.058 ms
CREATE SCHEMA
Time: 5.376 ms
ALTER SCHEMA
Time: 4.268 ms
CREATE SCHEMA
Time: 1.322 ms
ALTER SCHEMA
Time: 4.456 ms
CREATE EXTENSION
Time: 1.637 ms
COMMENT
Time: 1.

本文介绍了如何使用pg_dump进行PostgreSQL数据库的备份,以及如何通过psql和pg_restore进行数据库的恢复。首先,使用pg_dump命令备份数据库到文件test.bak,然后删除并重新创建数据库test,接着使用psql将备份文件导入。在恢复过程中,需要注意数据库不存在的情况,并通过psql和pg_restore的正确用法完成数据的导入。
最低0.47元/天 解锁文章
5928

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



