PG如何备份数据库:
备份数据::##单数据库:三种格式备份:pg_dump
-bash-4.2$ history |grep pg_dump
13 2023-11-22 15:18:17 postgres pg_dump mydb >mydb.bak
21 2023-11-22 15:20:03 postgres pg_dump mydb >mydb.bak
25 2023-11-22 15:20:20 postgres pg_dump mydb >mydb.tar
33 2023-11-22 15:21:08 postgres pg_dump mydb >mydb.sql
37 2023-11-22 15:21:39 postgres history |grep pg_dump
-f 指定备份的路径:
-bash-4.2$ pg_dump -f /tmp/mydb.sql mydb
-bash-4.2$ ls /tmp
mydb.sql vmware-root_701-3979708482 yum_save_tx.2023-11-21.15-54.Qfy3Xo.yumtx
vmware-root_677-3980363868 yum_save_tx.2023-11-21.15-52.3C3imO.yumtx
-bash-4.2$ psql mydb < mydb.bak
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: database "mydb" does not exist
-bash-4.2$ psql
psql (14.10)
Type "help" for help.
###恢复前,需要创建一个空的数据库
postgres=# create database mydb;
CREATE DATABASE
postgres=# \c mydb
You are now connected to database "mydb" as user "postgres".
mydb=# \d
Did not find any relations.
mydb=# exit
###恢复单数据库数据:
-bash-4.2$ psql mydb < mydb.bak
测试被删除:
所有库: pg_dumpall备份
45 2023-11-22 15:35:18 postgres pg_dumpall >postall.sql
46 2023-11-22 15:35:26 postgres du -sh postall.sql
47 2023-11-22 15:35:31 postgres cat postall.sql
48 2023-11-22 15:35:39 postgres history
恢复数据:
-bash-4.2$ psql -f postall.sql postgres