暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Postgresql逻辑备份方法

IT那活儿 2024-08-28
175

点击上方“IT那活儿”公众号--专注于企业全栈运维技术分享,不管IT什么活儿,干就完了!!!   



Postgresql备份分为逻辑备份和物理备份,本文浅谈逻辑备份及恢复方法。




环境

1.1 逻辑备份工具
1)pg_dump
2)pg_restore
3)pg_dumpall
4)pgadmin(可视化工具,不演示)
1.2 准备环境

[pgsql@test:/home/pgsql]$psql -h 127.0.0.1
postgres=# create user test_user with password 'test_user' nocreatedb;
postgres=# create database test_db with owner='test_user';
[pgsql@test:/home/pgsql]$psql -h 127.0.0.1 -p 5432 -U 'test_user' -d test_db
test_db=> create table test_tb (name varchar(50));
test_db=> insert into test_tb values('123');
test_db=> insert into test_tb values('456');test_db=> select * from test_db;
test_db=> select * from test_tb;
name
------
123
456
(2 rows)


演示逻辑备份/还原-pg_dump

导出的文件可读:
帮助命令:
[pgsql@test:/home/pgsql]$pg_dump –help
  • -h 指定要连接的主机;
  • -U 连接数据库的用户名;
  • -d 导出的数据库名称;
  • -t 指定导出数据的表名,不加即导出所有表数据;
  • -C 带查询语句,并将查询结果导出到文件;
  • -f 指定导出的文件,也可使用 > 转储。
此处只演示常用操作:
备份:
切记:此处要加上 -h 参数,否则会报错。
[pgsql@test:/home/pgsql]$pg_dump -U test_user -d test_db -h 127.0.0.1 > test_db.sql
删除数据:
test_db=> select * from test_tb;
name
------
123
456
(2 rows)
test_db=> drop table test_tb;

恢复:
[pgsql@test:/home/pgsql]$psql -h 127.0.0.1 -U test_user -d test_db -f test_db.sql
SET
SET
SET
SET
SET
set_config
------------

(1 row)

SET
SET
SET
SET
SET
SET
psql:test_db.sql:29: ERROR: relation "test" already exists   --如数据库中已存在对象,则会报错并跳过
ALTER TABLE
CREATE TABLE
ALTER TABLE
COPY 0
COPY 2

验证数据:

[pgsql@test:/home/pgsql]$psql -h 127.0.0.1 -U test_user -d test_db -p 5432
test_db=> select * from test_tb;
name
------
123
456
(2 rows)


演示逻辑备份/还原-pg_dump/pg_restore

帮助命令:
[pgsql@test:/home/pgsql]$pg_restore --help
  • pd_dump的备份默认是不能用pg_restore 导入的,pg_restore导入需加 -F参数就可支持;
  • -F, --format=c|d|t|p 输出文件格式 ,(c定制, d目录, t tar, p默认)。
备份:
[pgsql@test:/home/pgsql]$pg_dump -h 127.0.0.1 -U test_user -d test_db -t test_tb -F c -f test_tb.sql
还原(表):
[pgsql@test:/home/pgsql]$pg_restore -U test_user -d test_db -h 127.0.0.1 test_tb.sql
注:如果是还原数据库,则需要先把库创建好之后再去执行还原。
验证数据:

[pgsql@test:/home/pgsql]$psql -h 127.0.0.1 -U test_user -d test_db
test_db=> \d
List of relations
Schema | Name | Type | Owner
--------+---------+-------+-----------
public |
 test | table | test_user
public | test_tb | table | test_user
(2 rows)

test_db=> select * from test_tb;
name
------
123
456
(2 rows)


演示逻辑备份/还原-pg_dumpall

帮助命令:
[pgsql@test:/home/pgsql]$pg_dumpall –help
备份:
[pgsql@test:/home/pgsql]$pg_dumpall -h 127.0.0.1 > pg_all.sql
删除数据:
test_db=> drop table test_tb;
还原:
[pgsql@test:/home/pgsql]$psql -h 127.0.0.1 -f pg_all.sql
SET
SET
SET
psql:pg_all.sql:14: ERROR: role "postgres" already exists
ALTER ROLE
psql:pg_all.sql:16: ERROR: role "test_user" already exists
ALTER ROLE
You are now connected to database "template1" as user "postgres".
SET
SET
SET
SET
SET
set_config
------------

(1 row)

SET
SET
SET
SET
You are now connected to database "postgres" as user "postgres".
SET
SET
SET
SET
SET
set_config
------------

(1 row)

SET
SET
SET
SET
SET
SET
SET
SET
SET
set_config
------------

(1 row)

SET
SET
SET
SET
psql:pg_all.sql:110: ERROR: database "test_db" already exists
ALTER DATABASE
You are now connected to database "test_db" as user "postgres".
SET
SET
SET
SET
SET
set_config
------------

(1 row)

SET
SET
SET
SET
SET
SET
psql:pg_all.sql:138: ERROR: relation "test" already exists
ALTER TABLE
CREATE TABLE
ALTER TABLE
COPY 2
COPY 2

验证数据:
[pgsql@test:/home/pgsql]$psql -h 127.0.0.1 -U test_user -d test_db
test_db=> select * from test_tb;
name
------
123
456
(2 rows)


END


本文作者:戚传海(上海新炬中北团队)

本文来源:“IT那活儿”公众号

文章转载自IT那活儿,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论