PostgreSQL教程FG069-PG服务启动/停止/重启:多种方式与故障排查
本文档风哥主要介绍PostgreSQL数据库服务的启动、停止、重启操作方法,包括多种管理方式和故障排查技巧,风哥教程参考PostgreSQL官方文档Server Administration章节,适合DBA人员在学习和测试中使用,如果要应用于生产环境则需要自行确认。
Part01-基础概念与理论知识
1.1 PostgreSQL数据库服务管理概述
PostgreSQL数据库服务管理是DBA日常工作的核心内容之一。正确管理数据库服务对于保障系统稳定运行至关重要。PostgreSQL提供了多种服务管理方式,包括pg_ctl命令、systemd服务、postmaster进程直接启动等。更多视频教程www.fgedu.net.cn
- pg_ctl命令:PostgreSQL自带的管理工具
- systemd服务:Linux系统服务管理
- postmaster进程:直接启动主进程
- pg_ctlcluster:Debian/Ubuntu专用工具
- pgservice:Windows服务管理
1.2 PostgreSQL数据库pg_ctl工具介绍
pg_ctl是PostgreSQL自带的数据库服务管理工具,提供启动、停止、重启、重新加载配置等功能。
pg_ctl init[db] [-D DATADIR] [-s] [-o OPTIONS]
pg_ctl start [-D DATADIR] [-l FILENAME] [-s] [-t SECS] [-w] [-W] [-o OPTIONS]
pg_ctl stop [-D DATADIR] [-s] [-t SECS] [-w] [-W] [-m SHUTDOWN-MODE]
pg_ctl restart [-D DATADIR] [-l FILENAME] [-s] [-t SECS] [-w] [-W] [-o OPTIONS] [-m SHUTDOWN-MODE]
pg_ctl reload [-D DATADIR] [-s]
pg_ctl status [-D DATADIR]
pg_ctl promote [-D DATADIR] [-s] [-w] [-t SECS]
pg_ctl logrotate [-D DATADIR] [-s]
# 常用选项说明
-D, –pgdata=DATADIR # 数据目录位置
-l, –log=FILENAME # 日志文件路径
-s, –silent # 静默模式
-t, –timeout=SECS # 等待超时时间(秒)
-w, –wait # 等待操作完成
-W, –no-wait # 不等待操作完成
-m, –mode=MODE # 停止模式(smart/fast/immediate)
-o, –options=OPTIONS # 传递给postgres的选项
# 查看pg_ctl帮助
$ pg_ctl –help
pg_ctl 是一个用于初始化、启动、停止或控制 PostgreSQL 服务器的工具。
用法:
pg_ctl init[db] [-D DATADIR] [-s] [-o “选项”]
pg_ctl start [-D DATADIR] [-l 文件名] [-s] [-t 秒数] [-w] [-W] [-o “选项”]
pg_ctl stop [-D DATADIR] [-s] [-t 秒数] [-w] [-W] [-m 关闭模式]
pg_ctl restart [-D DATADIR] [-l 文件名] [-s] [-t 秒数] [-w] [-W] [-o “选项”] [-m 关闭模式]
pg_ctl reload [-D DATADIR] [-s]
pg_ctl status [-D DATADIR]
pg_ctl promote [-D DATADIR] [-s] [-w] [-t 秒数]
pg_ctl logrotate [-D DATADIR] [-s]
常用选项:
-D, –pgdata=DATADIR 数据库存储区域的位置
-l, –log=FILENAME 写入服务器日志的文件名
-s, –silent 只打印错误信息,不打印信息性消息
-t, –timeout=SECS 等待操作完成的最大秒数
-w, –wait 等待操作完成
-W, –no-wait 不等待操作完成
-m, –mode=MODE 关闭模式,可以是 “smart”, “fast”, 或 “immediate”
1.3 PostgreSQL数据库服务停止模式
PostgreSQL数据库服务停止有三种模式,了解各模式的特点对于选择合适的停止方式至关重要。
## 1. Smart模式(智能模式)
– 等待所有客户端断开连接
– 等待所有在线备份完成
– 最安全的停止方式
– 可能需要很长时间等待
– 适用于:计划维护,可以等待用户断开
## 2. Fast模式(快速模式)- 默认模式
– 断开所有客户端连接
– 回滚未完成的事务
– 正常关闭数据库
– 不等待客户端断开
– 适用于:大多数情况下的正常关闭
## 3. Immediate模式(立即模式)
– 立即终止所有进程
– 不回滚未完成的事务
– 下次启动需要恢复
– 可能导致数据不一致
– 适用于:紧急情况,数据库异常
# 停止模式对比
+———-+—————-+——————+—————-+
| 模式 | 等待客户端断开 | 回滚事务 | 恢复时间 |
+———-+—————-+——————+—————-+
| Smart | 是 | 是 | 最短 |
| Fast | 否 | 是 | 较短 |
| Immediate| 否 | 否 | 最长(需恢复) |
+———-+—————-+——————+—————-+
# 停止模式选择建议
– 日常维护: fast模式
– 计划停机: smart模式(如果可以等待)
– 紧急情况: immediate模式(谨慎使用)
– 系统崩溃: immediate模式(被迫使用)
Part02-生产环境规划与建议
2.1 PostgreSQL数据库服务管理规划
PostgreSQL数据库服务管理规划建议:
from oracle:www.itpux.com
## 管理方式选择
– 生产环境: systemd服务管理(推荐)
– 测试环境: pg_ctl命令管理
– 开发环境: 任意方式
## 服务配置规划
– 服务名称: postgresql-18
– 数据目录: /postgresql/data
– 日志目录: /postgresql/log
– PID文件: /postgresql/data/postmaster.pid
## 启动参数规划
– 监听地址: listen_addresses = ‘*’
– 端口: port = 5432
– 最大连接数: max_connections = 200
– 超级用户保留连接: superfgedu_reserved_connections = 5
## 停止策略规划
– 正常停止: fast模式
– 紧急停止: immediate模式
– 超时设置: 300秒
## 日志规划
– 启动日志: /postgresql/log/startup.log
– 运行日志: /postgresql/log/postgresql-%Y-%m-%d.log
– 日志轮转: 按天轮转,保留30天
2.2 PostgreSQL数据库Systemd服务配置
PostgreSQL数据库Systemd服务配置建议:
$ sudo cat > /etc/systemd/system/postgresql-18.service << 'EOF' [Unit] Description=PostgreSQL 18 Database Server Documentation=https://www.postgresql.org/docs/18/static/ After=network-online.target Wants=network-online.target [Service] Type=notify User=pgsql Group=pgsql # 环境变量 Environment=PGDATA=/postgresql/data Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj Environment=PG_OOM_ADJUST_VALUE=0 # 执行命令 ExecStart=/postgresql/fgapp/pgsql-18/bin/pgsql -D ${PGDATA} ExecReload=/bin/kill -HUP $MAINPID # 停止命令和超时 KillMode=mixed KillSignal=SIGINT TimeoutSec=300 # 资源限制 LimitNOFILE=65536 LimitNPROC=65536 LimitCORE=unlimited LimitMEMLOCK=infinity # OOM设置 OOMScoreAdjust=-1000 # 安全设置 PrivateTmp=true ProtectSystem=full ProtectHome=true NoNewPrivileges=true [Install] WantedBy=multi-fgedu.target EOF # 重新加载systemd配置 $ sudo systemctl daemon-reload # 启用开机自启 $ sudo systemctl enable postgresql-18 Created symlink /etc/systemd/system/multi-fgedu.target.wants/postgresql-18.service → /etc/systemd/system/postgresql-18.service. # 验证服务配置 $ systemctl cat postgresql-18 # /etc/systemd/system/postgresql-18.service [Unit] Description=PostgreSQL 18 Database Server Documentation=https://www.postgresql.org/docs/18/static/ After=network-online.target Wants=network-online.target ...
2.3 PostgreSQL数据库停止策略规划
PostgreSQL数据库停止策略规划建议:
## 正常停止流程
1. 通知应用停止新连接
2. 等待现有事务完成(可选)
3. 执行fast模式停止
4. 确认进程已完全停止
5. 验证数据完整性
## 紧急停止流程
1. 记录当前状态
2. 执行immediate模式停止
3. 重启后检查恢复日志
4. 验证数据完整性
## 停止超时处理
– 超时时间: 300秒
– 超时后动作: 强制终止(SIGKILL)
– 恢复检查: 下次启动时自动恢复
## 停止前检查脚本
#!/bin/bash
# pg_pre_stop.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# from:www.itpux.com.qq113257174.wx:itpux-com
# from:www.itpux.com.qq113257174.wx:itpux-com
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: `http://www.fgedu.net.cn`
# web: `http://www.fgedu.net.cn`
# web: `http://www.fgedu.net.cn`
# web: http://www.fgedu.net.cn
echo “=== PostgreSQL停止前检查 ===”
echo “当前时间: $(date)”
echo “”
# 检查活动连接数
ACTIVE_CONN=$(psql -t -c “SELECT count(*) FROM pg_stat_activity WHERE state = ‘active’;”)
echo “活动连接数: $ACTIVE_CONN”
# 检查长事务
LONG_TRANS=$(psql -t -c “SELECT count(*) FROM pg_stat_activity WHERE state = ‘active’ AND now() – query_start > interval ‘5 minutes’;”)
echo “长事务数: $LONG_TRANS”
# 检查复制状态
REPL_STATUS=$(psql -t -c “SELECT count(*) FROM pg_stat_replication;”)
echo “复制连接数: $REPL_STATUS”
# 检查备份状态
BACKUP_STATUS=$(psql -t -c “SELECT pg_is_in_backup();”)
echo “备份状态: $BACKUP_STATUS”
echo “”
echo “检查完成”
Part03-生产环境项目实施方案
3.1 PostgreSQL数据库服务启动方法
3.1.1 使用pg_ctl启动服务
## 基本启动命令
$ pg_ctl -D /postgresql/data start
等待服务器进程启动 … 完成
服务器进程已经启动
## 指定日志文件启动
$ pg_ctl -D /postgresql/data -l /postgresql/log/startup.log start
等待服务器进程启动 … 完成
服务器进程已经启动
## 等待启动完成
$ pg_ctl -D /postgresql/data -l /postgresql/log/startup.log -w start
等待服务器进程启动 … 完成
服务器进程已经启动
## 指定超时时间
$ pg_ctl -D /postgresql/data -l /postgresql/log/startup.log -w -t 120 start
等待服务器进程启动 … 完成
服务器进程已经启动
## 传递启动参数
$ pg_ctl -D /postgresql/data -o “-c max_connections=300″ start
等待服务器进程启动 … 完成
服务器进程已经启动
## 查看启动日志
$ cat /postgresql/log/startup.log
2026-04-02 10:00:00.000 CST [12345] LOG: 正在启动 PostgreSQL 18.3
2026-04-02 10:00:00.100 CST [12345] LOG: 正在监听IPv4地址”0.0.0.0″,端口 5432
2026-04-02 10:00:00.100 CST [12345] LOG: 正在监听IPv6地址”::”,端口 5432
2026-04-02 10:00:00.200 CST [12345] LOG: 正在监听Unix套接字”/tmp/.s.PGSQL.5432″
2026-04-02 10:00:00.300 CST [12345] LOG: 数据库系统准备接受连接
## 检查服务状态
$ pg_ctl -D /postgresql/data status
pg_ctl: 服务器正在运行 (PID: 12345)
/postgresql/fgapp/pgsql-18/bin/pgsql “-D” “/postgresql/data”
3.1.2 使用systemd启动服务
## 启动服务
$ sudo systemctl start postgresql-18
## 查看服务状态
$ sudo systemctl status postgresql-18
● postgresql-18.service – PostgreSQL 18 Database Server
Loaded: loaded (/etc/systemd/system/postgresql-18.service; enabled; preset: disabled)
Active: active (running) since Thu 2026-04-02 10:00:00 CST; 10s ago
Docs: https://www.postgresql.org/docs/18/static/
Main PID: 12345 (postgres)
Tasks: 7 (limit: 49128)
Memory: 256M
CGroup: /system.slice/postgresql-18.service
├─12345 /postgresql/fgapp/pgsql-18/bin/pgsql -D /postgresql/data
├─12346 pgsql: checkpointer
├─12347 pgsql: background writer
├─12348 pgsql: walwriter
├─12349 pgsql: autovacuum launcher
├─12350 pgsql: logical replication launcher
└─12351 pgsql: stats collector
4月 02 10:00:00 fgedu.net.cn systemd[1]: Starting PostgreSQL 18 Database Server…
4月 02 10:00:00 fgedu.net.cn systemd[1]: Started PostgreSQL 18 Database Server.
## 查看服务日志
$ sudo journalctl -u postgresql-18 -n 20
4月 02 10:00:00 fgedu.net.cn systemd[1]: Starting PostgreSQL 18 Database Server…
4月 02 10:00:00 fgedu.net.cn postgres[12345]: 2026-04-02 10:00:00.000 CST [12345] LOG: 正在启动 PostgreSQL 18.3
4月 02 10:00:00 fgedu.net.cn postgres[12345]: 2026-04-02 10:00:00.100 CST [12345] LOG: 正在监听IPv4地址”0.0.0.0″,端口 5432
4月 02 10:00:00 fgedu.net.cn systemd[1]: Started PostgreSQL 18 Database Server.
## 检查服务是否开机自启
$ sudo systemctl is-enabled postgresql-18
enabled
## 查看服务详细信息
$ sudo systemctl show postgresql-18 | head -20
Type=notify
Restart=no
NotifyAccess=main
RestartUSec=100ms
TimeoutStartUSec=5min
TimeoutStopUSec=5min
TimeoutAbortUSec=5min
RuntimeMaxUSec=infinity
WatchdogUSec=0
WatchdogTimestamp=Thu 2026-04-02 10:00:00 CST
WatchdogTimestampMonotonic=123456789
3.2 PostgreSQL数据库服务停止方法
3.2.1 使用pg_ctl停止服务
## Smart模式停止(等待客户端断开)
$ pg_ctl -D /postgresql/data stop -m smart
等待服务器进程关闭 … 完成
服务器进程已经停止
## Fast模式停止(默认模式)
$ pg_ctl -D /postgresql/data stop -m fast
等待服务器进程关闭 … 完成
服务器进程已经停止
## Immediate模式停止(立即停止)
$ pg_ctl -D /postgresql/data stop -m immediate
等待服务器进程关闭 … 完成
服务器进程已经停止
## 指定超时时间停止
$ pg_ctl -D /postgresql/data stop -m fast -t 300
等待服务器进程关闭 … 完成
服务器进程已经停止
## 查看停止日志
$ tail -20 /postgresql/log/postgresql-2026-04-02.log
2026-04-02 10:00:00.000 CST [12345] LOG: 收到快速关闭请求
2026-04-02 10:00:00.100 CST [12345] LOG: 中止任何活动事务…
2026-04-02 10:00:00.200 CST [12345] LOG: 后台工作进程 “logical replication launcher” (PID 12350) 已退出, 退出代码 0
2026-04-02 10:00:00.300 CST [12345] LOG: 关闭自动清理启动器…
2026-04-02 10:00:00.400 CST [12345] LOG: 正在关闭自动清理启动器…
2026-04-02 10:00:00.500 CST [12345] LOG: 正在关闭 WAL 日志…
2026-04-02 10:00:00.600 CST [12345] LOG: 数据库系统已关闭
## 验证服务已停止
$ pg_ctl -D /postgresql/data status
pg_ctl: 没有服务器正在运行
# 检查进程
$ ps -ef | grep pgsql | grep -v grep
(无输出)
3.2.2 使用systemd停止服务
## 停止服务
$ sudo systemctl stop postgresql-18
## 查看服务状态
$ sudo systemctl status postgresql-18
○ postgresql-18.service – PostgreSQL 18 Database Server
Loaded: loaded (/etc/systemd/system/postgresql-18.service; enabled; preset: disabled)
Active: inactive (dead) since Thu 2026-04-02 10:00:00 CST; 10s ago
Docs: https://www.postgresql.org/docs/18/static/
Main PID: 12345 (code=exited, status=0/SUCCESS)
4月 02 10:00:00 fgedu.net.cn systemd[1]: Stopping PostgreSQL 18 Database Server…
4月 02 10:00:00 fgedu.net.cn systemd[1]: postgresql-18.service: Deactivated successfully.
4月 02 10:00:00 fgedu.net.cn systemd[1]: Stopped PostgreSQL 18 Database Server.
## 查看停止日志
$ sudo journalctl -u postgresql-18 -n 10
4月 02 10:00:00 fgedu.net.cn systemd[1]: Stopping PostgreSQL 18 Database Server…
4月 02 10:00:00 fgedu.net.cn postgres[12345]: 2026-04-02 10:00:00.000 CST [12345] LOG: 收到快速关闭请求
4月 02 10:00:00 fgedu.net.cn postgres[12345]: 2026-04-02 10:00:00.100 CST [12345] LOG: 中止任何活动事务…
4月 02 10:00:00 fgedu.net.cn systemd[1]: Stopped PostgreSQL 18 Database Server.
## 强制停止(如果正常停止失败)
$ sudo systemctl kill postgresql-18
## 使用信号停止
$ sudo systemctl kill postgresql-18 –signal=SIGINT
$ sudo systemctl kill postgresql-18 –signal=SIGTERM
$ sudo systemctl kill postgresql-18 –signal=SIGQUIT
3.3 PostgreSQL数据库服务重启方法
3.3.1 使用pg_ctl重启服务
## 基本重启命令
$ pg_ctl -D /postgresql/data restart
等待服务器进程关闭 … 完成
服务器进程已经停止
等待服务器进程启动 … 完成
服务器进程已经启动
## 指定停止模式重启
$ pg_ctl -D /postgresql/data restart -m fast
等待服务器进程关闭 … 完成
服务器进程已经停止
等待服务器进程启动 … 完成
服务器进程已经启动
## 指定日志文件重启
$ pg_ctl -D /postgresql/data -l /postgresql/log/startup.log restart -m fast
等待服务器进程关闭 … 完成
服务器进程已经停止
等待服务器进程启动 … 完成
服务器进程已经启动
## 重新加载配置(不重启)
$ pg_ctl -D /postgresql/data reload
服务器进程发出信号
# 验证配置已重新加载
$ psql -c “SELECT pg_reload_conf();”
pg_reload_conf
—————-
t
(1 行记录)
## 查看重启日志
$ tail -30 /postgresql/log/postgresql-2026-04-02.log
2026-04-02 10:00:00.000 CST [12345] LOG: 收到快速关闭请求
2026-04-02 10:00:00.100 CST [12345] LOG: 中止任何活动事务…
2026-04-02 10:00:00.200 CST [12345] LOG: 数据库系统已关闭
2026-04-02 10:00:01.000 CST [12400] LOG: 正在启动 PostgreSQL 18.3
2026-04-02 10:00:01.100 CST [12400] LOG: 正在监听IPv4地址”0.0.0.0″,端口 5432
2026-04-02 10:00:01.200 CST [12400] LOG: 数据库系统准备接受连接
3.3.2 使用systemd重启服务
## 重启服务
$ sudo systemctl restart postgresql-18
## 查看服务状态
$ sudo systemctl status postgresql-18
● postgresql-18.service – PostgreSQL 18 Database Server
Loaded: loaded (/etc/systemd/system/postgresql-18.service; enabled; preset: disabled)
Active: active (running) since Thu 2026-04-02 10:00:00 CST; 10s ago
Docs: https://www.postgresql.org/docs/18/static/
Main PID: 12400 (postgres)
Tasks: 7 (limit: 49128)
Memory: 256M
## 重新加载配置(不重启)
$ sudo systemctl reload postgresql-18
## 查看重载日志
$ sudo journalctl -u postgresql-18 -n 5
4月 02 10:00:00 fgedu.net.cn systemd[1]: Reloading PostgreSQL 18 Database Server…
4月 02 10:00:00 fgedu.net.cn systemd[1]: Reloaded PostgreSQL 18 Database Server.
## 重启并查看日志
$ sudo systemctl restart postgresql-18 && sudo journalctl -u postgresql-18 -f
— Logs begin at Thu 2026-04-02 09:00:00 CST. —
4月 02 10:00:00 fgedu.net.cn systemd[1]: Stopping PostgreSQL 18 Database Server…
4月 02 10:00:00 fgedu.net.cn systemd[1]: Stopped PostgreSQL 18 Database Server.
4月 02 10:00:00 fgedu.net.cn systemd[1]: Starting PostgreSQL 18 Database Server…
4月 02 10:00:00 fgedu.net.cn systemd[1]: Started PostgreSQL 18 Database Server.
Part04-生产案例与实战讲解
4.1 PostgreSQL数据库启动故障排查
4.1.1 端口被占用导致启动失败
$ pg_ctl -D /postgresql/data start
等待服务器进程启动 … 已停止
pg_ctl: 无法启动服务器进程
# 查看日志
$ tail -20 /postgresql/log/postgresql-2026-04-02.log
2026-04-02 10:00:00.000 CST [12345] 致命错误: 无法创建套接字进行监听
2026-04-02 10:00:00.000 CST [12345] 提示: 是否有其他服务器正在端口 5432 上运行?
# 排查步骤
$ netstat -tlnp | grep 5432
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 12340/pgsql $ lsof -i :5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
pgsql 12340 pgsql 3u IPv4 12345 0t0 TCP *:postgresql (LISTEN)
# 问题原因:端口5432已被占用
# 解决方案1:停止占用端口的进程
$ kill 12340
# 或
$ pg_ctl -D /postgresql/data_old stop
# 解决方案2:修改端口
$ echo “port = 5433” >> /postgresql/data/postgresql.conf
$ pg_ctl -D /postgresql/data start
# 验证
$ netstat -tlnp | grep pgsql tcp 0 0 0.0.0.0:5433 0.0.0.0:* LISTEN 12400/pgsql
4.1.2 数据目录权限问题导致启动失败
$ pg_ctl -D /postgresql/data start
pg_ctl: 无法访问目录 “/postgresql/data”: 权限不够
# 排查步骤
$ ls -la /postgresql/
drwxr-xr-x. 2 root root 6 Apr 2 10:00 data
$ whoami
pgsql # 问题原因:数据目录权限不正确
# 解决方案
$ sudo chown -R pgsql: pgsql /postgresql/data
$ sudo chmod 700 /postgresql/data
# 验证
$ ls -la /postgresql/
drwx——. 2 pgsql pgsql 6 Apr 2 10:00 data
# 重新启动
$ pg_ctl -D /postgresql/data start
等待服务器进程启动 … 完成
服务器进程已经启动
4.1.3 锁文件存在导致启动失败
$ pg_ctl -D /postgresql/data start
pg_ctl: 看起来已经有一个服务器在运行
pg_ctl: 试图启动服务器进程, 等待服务器进程启动 … 已停止
pg_ctl: 无法启动服务器进程
# 排查步骤
$ ls -la /postgresql/data/postmaster.pid
-rw——-. 1 pgsql pgsql 5 Apr 2 10:00 /postgresql/data/postmaster.pid
$ cat /postgresql/data/postmaster.pid
12345
$ ps -p 12345
Error, no such process
# 问题原因:postmaster.pid文件存在但进程不存在
# 解决方案:删除锁文件
$ rm /postgresql/data/postmaster.pid
# 重新启动
$ pg_ctl -D /postgresql/data start
等待服务器进程启动 … 完成
服务器进程已经启动
# 验证
$ cat /postgresql/data/postmaster.pid
12400
4.2 PostgreSQL数据库停止故障排查
4.2.1 停止超时问题
$ pg_ctl -D /postgresql/data stop -m fast -t 60
等待服务器进程关闭 … pg_ctl: 服务器没有在60秒内关闭
# 排查步骤
# 查看活动连接
$ psql -c “SELECT pid, state, query_start, query FROM pg_stat_activity WHERE state = ‘active’;”
pid | state | query_start | query
——-+——–+——————————-+—————————–
12350 | active | 2026-04-02 09:50:00.123456+08 | SELECT * FROM large_table…
# 查看长时间运行的查询
$ psql -c “SELECT pid, now() – query_start AS duration, query FROM pg_stat_activity WHERE state = ‘active’ AND now() – query_start > interval ‘1 minute’;”
pid | duration | query
——-+—————–+—————————–
12350 | 00:10:00.123456 | SELECT * FROM large_table…
# 问题原因:有长时间运行的查询未完成
# 解决方案1:等待查询完成
# 增加超时时间
$ pg_ctl -D /postgresql/data stop -m fast -t 600
# 解决方案2:终止长时间运行的查询
$ psql -c “SELECT pg_cancel_backend(12350);”
pg_cancel_backend
——————-
t
(1 行记录)
# 然后停止
$ pg_ctl -D /postgresql/data stop -m fast
等待服务器进程关闭 … 完成
服务器进程已经停止
# 解决方案3:使用immediate模式
$ pg_ctl -D /postgresql/data stop -m immediate
等待服务器进程关闭 … 完成
服务器进程已经停止
4.2.2 进程无响应问题
$ pg_ctl -D /postgresql/data stop -m fast
等待服务器进程关闭 … (无响应)
# 排查步骤
$ ps -ef | grep pgsql pgsql 12345 1 0 10:00 ? 00:00:00 pgsql -D /postgresql/data
pgsql 12346 12345 0 10:00 ? 00:00:00 pgsql: checkpointer
pgsql 12347 12345 0 10:00 ? 00:00:00 pgsql: background writer
# 检查进程状态
$ cat /proc/12345/status | grep State
State: S (sleeping)
# 检查是否有进程卡住
$ strace -p 12345 -c
strace: Process 12345 attached
# 问题原因:进程可能卡在某个系统调用上
# 解决方案1:发送SIGTERM信号
$ kill -TERM 12345
# 等待几秒
$ sleep 5
# 检查是否停止
$ ps -p 12345
Error, no such process
# 解决方案2:发送SIGQUIT信号(immediate模式)
$ kill -QUIT 12345
# 解决方案3:强制终止(最后手段)
$ kill -9 12345
$ kill -9 12346 12347 12348 12349 12350
# 注意:强制终止后需要恢复
$ pg_ctl -D /postgresql/data start
# 查看恢复日志
$ tail -f /postgresql/log/postgresql-2026-04-02.log
2026-04-02 10:00:00.000 CST [12400] LOG: 数据库系统上次关闭时间: 2026-04-02 09:55:00 CST
2026-04-02 10:00:00.100 CST [12400] LOG: 数据库系统未正常关闭,正在恢复…
2026-04-02 10:00:00.200 CST [12400] LOG: 重放点: 0/12345678
2026-04-02 10:00:00.300 CST [12400] LOG: 重放完成,恢复点: 0/12345999
2026-04-02 10:00:00.400 CST [12400] LOG: 数据库系统准备接受连接
4.3 PostgreSQL数据库服务管理自动化
4.3.1 服务管理脚本
$ cat > /postgresql/scripts/pg_service.sh << 'EOF' #!/bin/bash # pg_service.sh # from:www.itpux.com.qq113257174.wx:itpux-com # from:www.itpux.com.qq113257174.wx:itpux-com # from:www.itpux.com.qq113257174.wx:itpux-com # from:www.itpux.com.qq113257174.wx:itpux-com # web: `http://www.fgedu.net.cn` # web: `http://www.fgedu.net.cn` # web: `http://www.fgedu.net.cn` # web: http://www.fgedu.net.cn PGDATA=/postgresql/data PGLOG=/postgresql/log/postgresql-$(date +%Y-%m-%d).log case "$1" in start) echo "正在启动PostgreSQL..." pg_ctl -D $PGDATA -l $PGLOG -w start ;; stop) echo "正在停止PostgreSQL..." pg_ctl -D $PGDATA stop -m fast -w ;; restart) echo "正在重启PostgreSQL..." pg_ctl -D $PGDATA restart -m fast -w ;; reload) echo "正在重新加载配置..." pg_ctl -D $PGDATA reload ;; status) pg_ctl -D $PGDATA status ;; *) echo "用法: $0 {start|stop|restart|reload|status}" exit 1 ;; esac EOF chmod +x /postgresql/scripts/pg_service.sh # 使用脚本 $ /postgresql/scripts/pg_service.sh start 正在启动PostgreSQL... 等待服务器进程启动 ... 完成 服务器进程已经启动 $ /postgresql/scripts/pg_service.sh status pg_ctl: 服务器正在运行 (PID: 12345) /postgresql/fgapp/pgsql-18/bin/pgsql "-D" "/postgresql/data" $ /postgresql/scripts/pg_service.sh stop 正在停止PostgreSQL... 等待服务器进程关闭 ... 完成 服务器进程已经停止
4.3.2 健康检查脚本
$ cat > /postgresql/scripts/pg_health_check.sh << 'EOF' #!/bin/bash # pg_health_check.sh # from:www.itpux.com.qq113257174.wx:itpux-com # from:www.itpux.com.qq113257174.wx:itpux-com # from:www.itpux.com.qq113257174.wx:itpux-com # from:www.itpux.com.qq113257174.wx:itpux-com # web: `http://www.fgedu.net.cn` # web: `http://www.fgedu.net.cn` # web: `http://www.fgedu.net.cn` # web: http://www.fgedu.net.cn PGDATA=/postgresql/data LOG_FILE=/postgresql/log/health_check.log echo "========================================" >> $LOG_FILE
echo “PostgreSQL健康检查 – $(date)” >> $LOG_FILE
echo “========================================” >> $LOG_FILE
# 检查进程状态
if pg_ctl -D $PGDATA status >/dev/null 2>&1; then
echo “[OK] PostgreSQL服务正在运行” >> $LOG_FILE
else
echo “[ERROR] PostgreSQL服务未运行” >> $LOG_FILE
exit 1
fi
# 检查连接
if psql -c “SELECT 1” >/dev/null 2>&1; then
echo “[OK] 数据库连接正常” >> $LOG_FILE
else
echo “[ERROR] 数据库连接失败” >> $LOG_FILE
exit 1
fi
# 检查活动连接数
ACTIVE_CONN=$(psql -t -c “SELECT count(*) FROM pg_stat_activity WHERE state = ‘active’;”)
echo “[INFO] 活动连接数: $ACTIVE_CONN” >> $LOG_FILE
# 检查空闲连接数
IDLE_CONN=$(psql -t -c “SELECT count(*) FROM pg_stat_activity WHERE state = ‘idle’;”)
echo “[INFO] 空闲连接数: $IDLE_CONN” >> $LOG_FILE
# 检查锁等待
LOCK_WAIT=$(psql -t -c “SELECT count(*) FROM pg_stat_activity WHERE wait_event_type = ‘Lock’;”)
if [ “$LOCK_WAIT” -gt 0 ]; then
echo “[WARN] 存在锁等待: $LOCK_WAIT 个” >> $LOG_FILE
else
echo “[OK] 无锁等待” >> $LOG_FILE
fi
# 检查磁盘空间
DATA_USAGE=$(df -h $PGDATA | tail -1 | awk ‘{print $5}’ | tr -d ‘%’)
if [ “$DATA_USAGE” -gt 80 ]; then
echo “[WARN] 数据目录磁盘使用率: ${DATA_USAGE}%” >> $LOG_FILE
else
echo “[OK] 数据目录磁盘使用率: ${DATA_USAGE}%” >> $LOG_FILE
fi
echo “健康检查完成” >> $LOG_FILE
EOF
chmod +x /postgresql/scripts/pg_health_check.sh
# 执行健康检查
$ /postgresql/scripts/pg_health_check.sh
# 查看检查结果
$ cat /postgresql/log/health_check.log
========================================
PostgreSQL健康检查 – Thu Apr 2 10:00:00 CST 2026
========================================
[OK] PostgreSQL服务正在运行
[OK] 数据库连接正常
[INFO] 活动连接数: 5
[INFO] 空闲连接数: 10
[OK] 无锁等待
[OK] 数据目录磁盘使用率: 45%
健康检查完成
Part05-风哥经验总结与分享
5.1 PostgreSQL数据库服务管理最佳实践
PostgreSQL数据库服务管理最佳实践风哥教程风哥教程风哥教程总结:
- 统一管理:生产环境使用systemd统一管理服务
- 合理停止:优先使用fast模式,避免使用immediate模式
- 日志监控:定期检查启动和运行日志
- 健康检查:定期执行健康检查脚本
- 权限正确:确保数据目录权限正确
- 资源限制:配置合理的系统资源限制
5.2 PostgreSQL数据库服务检查清单
## 启动前检查
□ 数据目录权限正确(700)
□ 配置文件语法正确
□ 端口未被占用
□ 磁盘空间充足
□ 内存配置合理
## 启动后检查
□ 进程运行正常
□ 端口监听正常
□ 日志无错误
□ 连接测试成功
□ 系统表正常
## 停止前检查
□ 无活动事务
□ 无长事务运行
□ 无备份进行
□ 应用已断开
## 停止后检查
□ 进程已完全停止
□ 锁文件已删除
□ 日志记录完整
5.3 PostgreSQL数据库服务管理工具推荐
PostgreSQL数据库服务管理相关工具推荐:
- pg_ctl:PostgreSQL自带服务管理工具
- systemctl:Linux系统服务管理工具
- psql:数据库连接测试工具
- pg_isready:连接就绪检查工具
- netstat/ss:网络端口检查工具
- journalctl:系统日志查看工具
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
