Centos7上PostgreSQL数据备份与恢复
作者:@skyflask
转载本文请注明出处:https://www.cnblogs.com/skyflask/p/17405958.html
目录
一、安装 PostgreSQL 数据库
1、yum安装
2、rpm包安装
3、初始化数据库
4、启动数据库
二、恢复数据
1、下载备份数据
2、上传数据
3、解压备份文件
4、删除多余的临时文件
5、修改配置文件
6、使用 root 用户更改文件夹权限
7、 使用 postgres 用户启动数据库
8、登录数据库验证
9、导出数据库或表
10、导入数据
一、安装 PostgreSQL 数据库
1、yum安装
1 2 3 4 5 6 7 8 | yum install -y https: //download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm yum install -y postgresql12-server # Optionally initialize the database and enable automatic start: sudo /usr/pgsql-12/bin/postgresql-12-setup initdb sudo systemctl enable postgresql-12 sudo systemctl start postgresql-12 |
但是有时候可能yum文件过期了,就导致没法安装。
所以这里采用了第二种方法,通过rpm包安装。
2、rpm包安装
1 2 3 4 5 6 7 8 9 | mkdir -p /data/postgres-12.4-down cd $_ wget https: //download.postgresql.org/pub/repos/yum/12/redhat/rhel-7-x86_64/postgresql12-libs-12.4-1PGDG.rhel7.x86_64.rpm wget https: //download.postgresql.org/pub/repos/yum/12/redhat/rhel-7-x86_64/postgresql12-12.4-1PGDG.rhel7.x86_64.rpm wget https: //download.postgresql.org/pub/repos/yum/12/redhat/rhel-7-x86_64/postgresql12-server-12.4-1PGDG.rhel7.x86_64.rpm # contrib 是一些第三方组织贡献出来的一些工具,在日常维护中也很有用,如果需要的话,也可以安装上 wget https: //download.postgresql.org/pub/repos/yum/12/redhat/rhel-7-x86_64/postgresql12-contrib-12.4-1PGDG.rhel7.x86_64.rpm |
# yum localinstall 本地目录安装方式进行安装(如果有其他额外的组件包依赖、该方式安装也会自动安装依赖包解决依赖问题)
1 | yum localinstall -y *.rpm |
当然,还有一种方式,也可以用传统的 yum install 方式安装
# 先安装相应依赖,否则后面安装会提示依赖组件不存在
1 | yum install -y libicu systemd-sysv |
3、初始化数据库
1 | /usr/pgsql-12/bin/postgresql-12-setup initdb |
4、启动数据库
1 2 3 | # 启动服务&设置开机自启动 systemctl start postgresql-12 systemctl enable postgresql-12 |
二、恢复数据
1、下载备份数据
从腾讯云上下载最近的备份文件,如果数据比较大的话,可能下载时间较长。
2、上传数据
将下载下来的tar.gz文件进行zip压缩后,再上传到服务器上,因为下载下来的tar.gz文件可能有十几G,zip压缩后可以缩小很多。
3、解压备份文件
1 2 3 | cd / var /lib/pgsql/12/recovery unzip pgsql_902163_data_2023-05-15_automatic-20230515002117.tar.gz.zip tar -xf pgsql_902163_data_2023-05-15_automatic-20230515002117.tar.gz |
4、删除多余的临时文件
1 | rm -rf backup_label |
5、修改配置文件
将配置文件postgresql.conf
中的以下选项注释掉,注释方法:在行首使用#。 如有多个该选项,则全部注释掉。
1 2 3 4 5 6 7 8 | shared_preload_libraries local_preload_libraries pg_stat_statements.max pg_stat_statements.track archive_mode archive_command synchronous_commit synchronous_standby_names |
如果恢复版本为 PostgreSQL 12.4,还需要注释 include = 'standby.conf'
这一行。
修改配置文件:
1 2 | port = '5432' ##将port参数的值修改为5432 unix_socket_directories = '/var/run/postgresql/' ##将unix_socket_directories的值修改为/ var /run/postgresql/,如未设置此值,可跳过此项 |
在postgresql.conf
文件末尾追加配置,表示不再使用强同步模式
1 2 | synchronous_commit = local synchronous_standby_names = '' |
6、使用 root 用户更改文件夹权限
1 2 | chmod 0700 / var /lib/pgsql/12/recovery chown postgres:postgres / var /lib/pgsql/12/recovery -R |
7、 使用 postgres 用户启动数据库
1 | /usr/pgsql-12/bin/pg_ctl start -D / var /lib/pgsql/12/recovery |
8、登录数据库验证
登录 PostgreSQL 数据库。
1 2 | export PGDATA=/ var /lib/pgsql/10/recovery psql |
查看要导出数据量:
1 2 3 4 5 | bash-4.2$ psql -c 'select count(1) from email_esp_flow' -Uroot postgres count --------- 1818131 (1 row) |
9、导出数据库或表
我们这里恢复email_esp_flow这个表:
1 | /usr/pgsql-12/bin/pg_dump -d postgres -t email_esp_flow >email_esp_flow.sql |
-d 数据库
-t 数据表
10、导入数据
1 | psql -h 10.190.13.108 -Uroot postgres <email_esp_flow.sql |
验证导入的数据量是否和备份库的一致:
1 2 3 4 5 | postgres=> select count(*) from email_esp_flow; count --------- 1818131 (1 row) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?