当前位置: 首页 > news >正文

PG备份一(逻辑备份)

逻辑备份
pg_dump:只能备份单个数据库
pg_dumpall:可以备份所有数据库(用户,表空间,权限),pg_dumpall只支持文本格式,pg_dumpall需要多次连接postgresql服务器

一、pg_dump
1.创建数据库test并插入测试数据

postgres=# create database test;
CREATE DATABASE
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# CREATE TABLE test_data (id SERIAL PRIMARY KEY,name VARCHAR(100),email VARCHAR(100),age INTEGER,salary DECIMAL(10,2),is_active BOOLEAN,created_at TIMESTAMP
);INSERT INTO test_data (name, email, age, salary, is_active, created_at)
SELECT 'User_' || floor(random() * 1000)::INT,'user_' || floor(random() * 1000)::INT || '@example.com',floor(random() * 70 + 18)::INT,round((random() * 9000 + 1000)::NUMERIC, 2),random() > 0.5,NOW() - (random() * INTERVAL '365 days')
FROM generate_series(1, 100);
--查询验证
test=# select count(*) from test_data;count 
-------100

2.使用pg_dump备份并恢复
(1)创建备份目录

mkdir  -p /dbs/pg14/pgback

(2)pg_dump备份
备份方法 一:

postgres@pghost01 pgback]$ pg_dump test --file=/dbs/pg14/pgback/testdb.sql
--导出为tar文件方式
pg_dump --username=postgres --host=192.168.75.131 --port=5666 --dbname=test --format=tar --file=/dbs/pg14/pgback/testdb.tar

备份方法二:可以使用Insert进行导出

pg_dump test --inserts > /dbs/pg14/pgback/testdb01.sql 
--tar文件

备份方法三:

--可以进行导出压缩(文件为二进制,恢复需使用pg_restore恢复)
pg_dump --dbname=test --format=custom  --file=/dbs/pg14/pgback/testdb.dmp
--恢复方式
pg_restore --username=postgres --host=192.168.75.131 --port=5666  --dbname=test02 /dbs/pg14/pgback/testdb.dmp 

(3)创建新库执行恢复(方法一恢复)

postgres=# create database test02;
CREATE DATABASE
--恢复
[postgres@pghost01 pgback]$ psql --dbname=test02 --file=testdb.sql
SET
SET
SET
SET
SETset_config 
------------(1 row)SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
CREATE SEQUENCE
ALTER TABLE
ALTER SEQUENCE
ALTER TABLE
COPY 1
COPY 100setval 
--------100
(1 row)ALTER TABLE
--验证数据
[postgres@pghost01 pgback]$ psql
psql (14.11)
Type "help" for help.postgres=# \c test02
You are now connected to database "test02" as user "postgres".
test02=# \dtList of relationsSchema |   Name    | Type  |  Owner   
--------+-----------+-------+----------public | a         | table | postgrespublic | test_data | table | postgres
(2 rows)test02=# select count(*) from test_data;count 
-------100
(1 row)

二、pg_dumpall

1.导出全库

 pg_dumpall >/dbs/pg14/pgback/pg_all.sql

2.可单独导出role,tablespace ,owner 使用如下参数
-g, --globals-only dump only global objects, no databases
-O, --no-owner skip restoration of object ownership
-r, --roles-only dump only roles, no databases or tablespaces
-s, --schema-only dump only the schema, no data
-S, --superuser=NAME superuser user name to use in the dump
-t, --tablespaces-only dump only tablespaces, no databases or roles

注:pg_dumpall 导入的时候,不存在的会导入,存在的将不会导入


文章转载自:
http://cardioversion.gbfuy28.cn
http://anathematise.gbfuy28.cn
http://bioconversion.gbfuy28.cn
http://aloud.gbfuy28.cn
http://aneurism.gbfuy28.cn
http://adherence.gbfuy28.cn
http://amblygonite.gbfuy28.cn
http://antisickling.gbfuy28.cn
http://babywear.gbfuy28.cn
http://buzz.gbfuy28.cn
http://asynchronism.gbfuy28.cn
http://babu.gbfuy28.cn
http://barrage.gbfuy28.cn
http://aviation.gbfuy28.cn
http://breechloading.gbfuy28.cn
http://bolar.gbfuy28.cn
http://backyard.gbfuy28.cn
http://bearberry.gbfuy28.cn
http://chemistry.gbfuy28.cn
http://appendectomy.gbfuy28.cn
http://aftertreatment.gbfuy28.cn
http://carolinian.gbfuy28.cn
http://afond.gbfuy28.cn
http://audacity.gbfuy28.cn
http://boredom.gbfuy28.cn
http://amidate.gbfuy28.cn
http://caiquejee.gbfuy28.cn
http://anthrax.gbfuy28.cn
http://argala.gbfuy28.cn
http://behaviorism.gbfuy28.cn
http://www.dtcms.com/a/280835.html

相关文章:

  • Kafka——生产者消息分区机制原理剖析
  • Java基础教程(009): Java 的封装
  • Samba配置使用
  • 算法学习笔记:23.贪心算法之活动选择问题 ——从原理到实战,涵盖 LeetCode 与考研 408 例题
  • 重学前端005 --- 响应式网页设计 CSS 盒子模型
  • Python函数进阶
  • python 基于 httpx 的流式请求
  • 封装---统一处理接口与打印错误信息
  • Linux下调试器gdb/cgdb的使用
  • Linux系统调优和工具
  • [面试] 手写题-对象数组根据某个字段进行分组
  • mysql官网的版本历史版本下载
  • 令牌获取与认证机制详解
  • 关键点检测数据格式转换(.JSON转TXT)
  • 【超分论文精读】——LightBSR(ICCV2025)
  • 梳理Bean的创建流程
  • mongoDB的CRUD
  • Visual Studio 现已支持新的、更简洁的解决方案文件(slnx)格式
  • 云服务器如何管理数据库(MySQL/MongoDB)?
  • 基于STM32G431无刷电机驱动FOC软硬件学习
  • iOS高级开发工程师面试——常见第三方框架架构设计
  • C++学习笔记五
  • Gemma-3n-E4B-it本地部署教程:谷歌开源轻量级多模态大模型,碾压 17B 级同类模型!
  • SHAP 值的数值尺度
  • Conda 核心命令快速查阅表
  • 技术演进中的开发沉思-35 MFC系列:消息映射与命令
  • Keepalived双机热备
  • 网络安全职业指南:探索网络安全领域的各种角色
  • 003大模型基础知识
  • React 实现老虎机滚动动画效果实例