维护指南
本章提供的是本应用自身特殊等维护与配置。而配置域名、HTTPS设置、数据迁移、应用集成、Web Server 配置、Docker 配置、修改数据库连接、服务器上安装更多应用、操作系统升级、快照备份等操作通用操作请参考:管理员指南 和 安装后配置 相关章节。
场景
PostgreSQL 备份与恢复
PostgreSQL上的应用备份有多种备份方案,常见包括:
- 使用 pg_dump, pg_dumpall, pgAdmin, phpPgAdmin等工具进行数据库导出(SQL转存)
- 使用 pg_basebackup等工具实现增量备份和基于时间的恢复
- 数据库文件目录直接复制
其中数据库文件目录直接复制等同于服务器快照备份,无需重复再做。
PostgreSQL 升级
PostgreSQL 提供了大版本升级工具 pg_upgrade。升级总是比较复杂,这里只列出 pg_upgrade 常用参数
pg_upgrade --help
pg_upgrade upgrades a PostgreSQL cluster to a different major version.
Usage:
pg_upgrade [OPTION]...
Options:
-b, --old-bindir=BINDIR old cluster executable directory
-B, --new-bindir=BINDIR new cluster executable directory
-c, --check check clusters only, don't change any data
-d, --old-datadir=DATADIR old cluster data directory
-D, --new-datadir=DATADIR new cluster data directory
-j, --jobs=NUM number of simultaneous processes or threads to use
-k, --link link instead of copying files to new cluster
-o, --old-options=OPTIONS old cluster options to pass to the server
-O, --new-options=OPTIONS new cluster options to pass to the server
-p, --old-port=PORT old cluster port number (default 50432)
-P, --new-port=PORT new cluster port number (default 50432)
-r, --retain retain SQL and log files after success
-U, --username=NAME cluster superuser (default "root")
-v, --verbose enable verbose internal logging
-V, --version display version information, then exit
-?, --help show this help, then exit
Before running pg_upgrade you must:
create a new database cluster (using the new version of initdb)
shutdown the postmaster servicing the old cluster
shutdown the postmaster servicing the new cluster
When you run pg_upgrade, you must provide the following information:
the data directory for the old cluster (-d DATADIR)
the data directory for the new cluster (-D DATADIR)
the "bin" directory for the old version (-b BINDIR)
the "bin" directory for the new version (-B BINDIR)
For example:
pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin
or
$ export PGDATAOLD=oldCluster/data
$ export PGDATANEW=newCluster/data
$ export PGBINOLD=oldCluster/bin
$ export PGBINNEW=newCluster/bin
$ pg_upgrade
Report bugs to <[email protected]>.
故障排除
除以下列出的 PostgreSQL 故障问题之外, 通用故障处理 专题章节提供了更多的故障方案。
运行 psql 命令显示 "cannot be run as root Failure, exiting"?
为了安全考量,默认安装已经创建了一个数据库账号 postgres
,如果使用 root
账号登录,请切换用户后再使用 psql
sudo -i -u postgres
常见问题
什么是 PostgreSQL 的 Client 和 Server?
PostgreSQL Server 是指 PostgreSQL 程序本体,而 PostgreSQL Client 指采用TCP协议用于连接程序本地的客户端工具。
它们是两个完全不同的程序,也就是说它们并需要同时安装到同一台服务上。
PostgreSQL 有默认数据库吗?
有,名称为 postgres
PostgreSQL 支持哪些数据类型?
PostgreSQL 支持官方的数据类型,包括:数据、JASON、JSONB 以及几何类型,还可以使用 SQL 命令创建自定义类型