基于Red Hat Enterprise Linux 7操作系统的PostgresSql15的备份恢复(实践笔记)

news/2024/7/9 20:29:18 标签: linux, 笔记, 运维, postgresql, 数据库

零、前言

本文是基于阿里云ECS服务器进行的实践操作,操作系统版本:Red Hat Enterprise Linux 7
PG数据库版本:PostgresSql 15
PG安装方式:yum
由于本人新接触pg数据,本次也是出于好奇,就对pg数据库的pg_dump备份操作进行了一次试验,操作过程可能有些不当,敬请谅解。
本文只供留做笔记或新手作为参考,不作为技术指导类文章。操作需具备pg基础及linux基本操作知识。

一、备份

--- 1.1、首先新建备份sql输出的目录并授权
# root用户操作
[root@wstech ~]# mkdir -p /usr/pgsql-15/backup

--- 1.2、备份操作
-- 切换postgres用户
su - postgres

cd /usr/pgsql-15/bin
--- 1.3、执行备份:注意文件目录——可执行命令的目录以及输出备份文件的目录,不然恢复的时候找不到。另外需要注意输出目录的磁盘余量。
-bash-4.2$ ./bin/pg_dump mydb > ./backup/mydb.sql
--- 1.4、查看文件
-bash-4.2$ cd pgsql-15/backup/
-bash-4.2$ ll
total 4
-rw-r--r-- 1 postgres postgres 1073 Aug 26 23:42 mydb.sql
--- 1.5、查看备份文件sql文本内容
-bash-4.2$ cat mydb.sql
--
-- PostgreSQL database dump
--

-- Dumped from database version 15.4
-- Dumped by pg_dump version 15.4

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: weather_info; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public.weather_info (
    city character varying(80),
    temp_lo integer,
    temp_hi integer,
    prcp real,
    date date
);


ALTER TABLE public.weather_info OWNER TO postgres;

--
-- Data for Name: weather_info; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.weather_info (city, temp_lo, temp_hi, prcp, date) FROM stdin;
US      46      50      0.25    1994-11-27
UK      43      57      0       1994-11-29
Hayward 37      54      \N      1994-11-29
\.


--
-- PostgreSQL database dump complete
--


--- 1.6、删除数据库mydb
-bash-4.2$ dropdb mydb;
-bash-4.2$ psql
psql (15.4)
Type "help" for help.
--- 1.7、查看是否删除成功(已经没有了,说明删除成功了)
postgres=# \l
                                                 List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
 dbname    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 dengyu    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
(5 rows)


在这里插入图片描述

二、恢复

--- 2.1、新建目标库newdb数据库
-bash-4.2$ createdb newdb;
--- 2.2、进入psql可执行文件目录(bin目录)
-bash-4.2$ cd /usr/pgsql-15/
-bash-4.2$ ls
backup  bin  lib  share
--- 2.3、恢复
-- 执行恢复(注意可执行文件路径及备份文件路径)
-bash-4.2$ ./bin/psql newdb < ./backup/mydb.sql
SET
SET
SET
SET
SET
 set_config
------------

(1 row)

SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
COPY 3
-bash-4.2$

--- 2.4、恢复完成后链接数据库
-bash-4.2$ psql
psql (15.4)
Type "help" for help.
--- 2.5、切换到newdb数据库
postgres=# \c newdb
You are now connected to database "newdb" as user "postgres".
--- 2.6、看数据库中表信息
newdb=# \d
            List of relations
 Schema |     Name     | Type  |  Owner
--------+--------------+-------+----------
 public | weather_info | table | postgres
(1 row)
--- 2.7、查询newdb中恢复的表weather_info的数据
newdb=# select * from weather_info;
  city   | temp_lo | temp_hi | prcp |    date
---------+---------+---------+------+------------
 US      |      46 |      50 | 0.25 | 1994-11-27
 UK      |      43 |      57 |    0 | 1994-11-29
 Hayward |      37 |      54 |      | 1994-11-29
(3 rows)

newdb=#

在这里插入图片描述
在这里插入图片描述


http://www.niftyadmin.cn/n/4976754.html

相关文章

初阶数据结构(三)链表

&#x1f493;博主csdn个人主页&#xff1a;小小unicorn&#x1f493; ⏩专栏分类&#xff1a;c &#x1f69a;代码仓库&#xff1a;小小unicorn的学习足迹&#x1f69a; &#x1f339;&#x1f339;&#x1f339;关注我带你学习编程知识 前面我们讲的线性表的顺序存储结构。它…

F#奇妙游(24):函数式编程与代数数据类型ADT

ADT与表达式 这篇主要是写函数式编程中的元素与ADT的关系&#xff0c;特别的就是关于表达式的讨论。在函数式编程中&#xff0c;表达式也是一个比较重要概念。下图是Scott Wlaschin关于F#核心概念的图。 #mermaid-svg-dKiBqZZ2EZmScf19 {font-family:"trebuchet ms"…

在Windows操作系统上安装Neo4j数据库

在Windows操作系统上安装Neo4j数据库 一、在Windows操作系统上安装Neo4j数据库 一、在Windows操作系统上安装Neo4j数据库 点击 MySQL可跳转至MySQL的官方下载地址。 在VUE3项目的工程目录中&#xff0c;通过以下命令可生成node_modules文件夹。 npm install&#xff08;1&am…

Android常用组件:空布局empty_view,占位图封装

最近在做一件费劲不讨好的事情&#xff0c;那就是把项目中无关业务的代码功能模块抽出来&#xff0c;供以后使用。 既然费劲心机&#xff0c;不妨分享出来&#xff0c;大家可以一块学习借鉴。 直接来看看空布局的界面效果&#xff1a; 基本的布局就是以下结构&#xff1a; xm…

7 Python的模块和包

概述 在上一节&#xff0c;我们介绍了Python的异常处理&#xff0c;包括&#xff1a;异常、异常处理、抛出异常、用户自定义异常等内容。在这一节中&#xff0c;我们将介绍Python的模块和包。Python的模块&#xff08;Module&#xff09;和包&#xff08;Package&#xff09;是…

HTML <tfoot> 标签

实例 带有 thead、tbody 以及 tfoot 元素的 HTML 表格: <table border="1"><thead><tr><th>Month</th><th>Savings</th></tr></thead><tfoot><tr><td>Sum</td><td>$180<…

Go 自学:struct结构体

以下代码展示如何建立一个结构体struct。 我们可以使用%v查看结构体的详情。 package mainimport ("fmt" )func main() {Jeff : User{"Jeff", "Jeffgo.dev", true, 16}fmt.Println((Jeff))fmt.Printf("Jeff details are: %v\n", Jeff…

Fei-Fei Li-Lecture 16:3D Vision 【斯坦福大学李飞飞CV课程第16讲:3D Vision】

目录 P1 2D Detection and Segmentation​编辑 P2 Video 2D time series P3 Focus on Two Problems P4 Many more topics in 3D Vision P5-10 Multi-View CNN P11 Experiments – Classification & Retrieval P12 3D Shape Representations P13--17 3D Shape Rep…