postgresql数据库备份恢复

Call pg_basebackup with the option --checkpoint=fast to force a fast checkpoint rather than waiting for a spread one to complete.

Restore a postgres backup file using the command line?

Backing up and Restoring Postgres using pg_basebackup 30/08/2019

全量导出方式暂未尝试成功,使用dump + restore方式只导出对应的数据库。


使用默认管理员账号postgres

备份:

  • dump数据库:pg_dump -F c -b -v -f backup/sonar.backup sonar

另外一台机器:

  • 删除旧数据库,新建同名数据库: dropdb sonar; createdb sonar
  • 恢复数据库: pg_restore -d sonar sonar.backup

如果使用非superuser用户导出,恢复时提示could not execute query: ERROR: must be owner of extension plpgsql,log信息类似如下——

sonar@stf:~$ pg_restore -d sonar sonarmain.backup
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 4332; 0 0 COMMENT EXTENSION plpgsql
pg_restore: [archiver (db)] could not execute query: ERROR:  must be owner of extension plpgsql
    Command was: COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';

WARNING: errors ignored on restore: 1

参考Seeing error "Must be owner of extension plpgsql" during a Postgres database restore,大部分场景下可以忽略这个错误(实测上述报错不影响导入结果,包含1213个项目的数据库重新连接后ES基于数据库重建索引成功)。因为尝试导入自身无权限的数据,可以使用-n public参数忽略。参考PostgreSQL 9.1 pg_restore error regarding PLPGSQL

# pg_restroe --help查看参数含义: -c 对应的数据库如果存在内容,先删除内容; -n 只导入指定的schema;
pg_restore -U username -c -n public -d database_name

提示这个错误的原因是因为执行pg_dump导出时,创建了schemaplpgsql,参考How to solve privileges issues when restore PostgreSQL Database

# actual content of pg_dump
CREATE SCHEMA public;
COMMENT ON SCHEMA public IS 'standard public schema';
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';

你可能感兴趣的:(postgresql数据库备份恢复)