Ubuntu开启自启动PostgreSQL读取HDD失败处理思路

前置文章:

背景:

启动实体Ubuntu机器后后很大的概率PostgreSQL不会成功启动,查看日志:
Ubuntu启动时间:

bash 复制代码
root@Pine-Tree:~# uptime -s
2025-04-19 09:52:24

查看PostgreSQL运行状态

bash 复制代码
root@Pine-Tree:~# sudo systemctl status postgresql@15-main
× [email protected] - PostgreSQL Cluster 15-main
     Loaded: loaded (/lib/systemd/system/[email protected]; enabled; vendor preset: enabled)
     Active: failed (Result: protocol) since Sat 2025-04-19 09:52:26 CST; 15min ago
    Process: 700 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 15-main start (code=exited, status=1/FAILURE)
        CPU: 41ms

4月 19 09:52:26 Pine-Tree systemd[1]: Starting PostgreSQL Cluster 15-main...
4月 19 09:52:26 Pine-Tree postgresql@15-main[700]: Error: /mnt/pgdata/main is not accessible or does not exist
4月 19 09:52:26 Pine-Tree systemd[1]: [email protected]: Can't open PID file /run/postgresql/15-main.pid (yet?) after start: Operation not permitted
4月 19 09:52:26 Pine-Tree systemd[1]: [email protected]: Failed with result 'protocol'.
4月 19 09:52:26 Pine-Tree systemd[1]: Failed to start PostgreSQL Cluster 15-main.

可知在系统启动2秒后就开始尝试启动PostgreSQL了,但是挂载目录/mnt/pgdata/main还无法访问,导致PostgreSQL启动失败。

查询相关资料发现冷启动HDD通过USB3.0连接从开机到系统检测完毕大概需要3-20秒 。

解决思路:

使用systemctl edit调整启动策略

方案一、设置PostgreSQL延迟5秒启动

创建文件夹用于systemctl edit配置
bash 复制代码
sudo mkdir -p /etc/systemd/system/[email protected]
新增片段覆盖文件
bash 复制代码
sudo nano /etc/systemd/system/[email protected]/override.conf
在打开的编辑器中添加以下内容
bash 复制代码
[Service]
ExecStartPre=/bin/sleep 5
保存并退出,然后重新加载systemd配置
bash 复制代码
sudo systemctl daemon-reload
重新启动验证
bash 复制代码
reboot
确认PostgreSQL运行状况

启动成功:

bash 复制代码
root@Pine-Tree:~# sudo systemctl status postgresql@15-main
● [email protected] - PostgreSQL Cluster 15-main
     Loaded: loaded (/lib/systemd/system/[email protected]; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/[email protected]
             └─override.conf
     Active: active (running) since Sat 2025-04-19 11:20:15 CST; 2min 14s ago
    Process: 814 ExecStartPre=/bin/sleep 5 (code=exited, status=0/SUCCESS)
    Process: 1440 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 15-main start (code=exited, status=0/SUCCESS)
   Main PID: 1446 (postgres)

Ubuntu启动时间:

bash 复制代码
root@Pine-Tree:~# uptime -s
2025-04-19 11:19:56

确认PostgreSQL启动时间,可知延迟启动生效

bash 复制代码
root@Pine-Tree:~# ps -eo pid,lstart,cmd | grep postgres | grep -v grep
   1446 Sat Apr 19 11:20:05 2025 /usr/lib/postgresql/15/bin/postgres -D /mnt/pgdata/main -c config_file=/etc/postgresql/15/main/postgresql.conf
   1483 Sat Apr 19 11:20:08 2025 postgres: 15/main: checkpointer 
   1484 Sat Apr 19 11:20:08 2025 postgres: 15/main: background writer 
   1486 Sat Apr 19 11:20:10 2025 postgres: 15/main: walwriter 
   1487 Sat Apr 19 11:20:10 2025 postgres: 15/main: autovacuum launcher 
   1488 Sat Apr 19 11:20:10 2025 postgres: 15/main: logical replication launcher 
   1838 Sat Apr 19 11:22:32 2025 postgres: 15/main: postgres dbname 192.168.125.2(6139) idle

方案二、PostgreSQL开机自启动失败后重试2次(间隔10秒)

修改override.conf
bash 复制代码
sudo nano /etc/systemd/system/[email protected]/override.conf

配置调整为:

bash 复制代码
[Service]
Restart=on-failure
RestartSec=10s
StartLimitBurst=2
保存并退出,然后重新加载systemd配置
bash 复制代码
sudo systemctl daemon-reload
重新启动验证
bash 复制代码
reboot
确认PostgreSQL运行状况

启动成功:

bash 复制代码
root@Pine-Tree:~# sudo systemctl status postgresql@15-main
● [email protected] - PostgreSQL Cluster 15-main
     Loaded: loaded (/lib/systemd/system/[email protected]; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/[email protected]
             └─override.conf
     Active: active (running) since Sat 2025-04-19 12:30:06 CST; 7min ago
    Process: 1479 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 15-main start (code=exited, status=0/SUCCESS)
   Main PID: 1487 (postgres)

Ubuntu启动时间:

bash 复制代码
root@Pine-Tree:~# uptime -s
2025-04-19 12:29:47

查看PostgreSQL历史启动记录,可知12:29:50s首次启动PostgreSQL失败,10秒过后启动成功:

bash 复制代码
 root@Pine-Tree:~# sudo journalctl -u postgresql@15-main --no-pager -n 50
 -- Boot 0ba0937613c14ba8b47c6bb17de28bcd --
4月 19 12:29:50 Pine-Tree systemd[1]: Starting PostgreSQL Cluster 15-main...
4月 19 12:29:50 Pine-Tree postgresql@15-main[794]: Error: /mnt/pgdata/main is not accessible or does not exist
4月 19 12:29:50 Pine-Tree systemd[1]: [email protected]: Can't open PID file /run/postgresql/15-main.pid (yet?) after start: Operation not permitted
4月 19 12:29:50 Pine-Tree systemd[1]: [email protected]: Failed with result 'protocol'.
4月 19 12:29:50 Pine-Tree systemd[1]: Failed to start PostgreSQL Cluster 15-main.
4月 19 12:30:00 Pine-Tree systemd[1]: [email protected]: Scheduled restart job, restart counter is at 1.
4月 19 12:30:00 Pine-Tree systemd[1]: Stopped PostgreSQL Cluster 15-main.
4月 19 12:30:00 Pine-Tree systemd[1]: Starting PostgreSQL Cluster 15-main...
4月 19 12:30:06 Pine-Tree systemd[1]: Started PostgreSQL Cluster 15-main.

方案三、设置PostgreSQL延迟5秒启动同时设置启动失败后重试2次(间隔10秒 )

修改override.conf后重新验证

bash 复制代码
sudo nano /etc/systemd/system/[email protected]/override.conf

配置调整为:

bash 复制代码
[Service]
ExecStartPre=/bin/sleep 5
Restart=on-failure
RestartSec=10s
StartLimitBurst=2
保存并退出,然后重新加载systemd配置

大部分情况下,延迟5秒即可保证启动成功,不会走到重试逻辑

bash 复制代码
sudo systemctl daemon-reload

问题汇总

sudo systemctl edit postgresql@15-main编辑后保存失败,提示文件不存在

bash 复制代码
root@Pine-Tree:~# sudo systemctl edit postgresql@15-main
Editing "/etc/systemd/system/[email protected]/override.conf" canceled: temporary file is empty.

解决措施:

创建文件夹用于systemctl edit配置

bash 复制代码
sudo mkdir -p /etc/systemd/system/[email protected]

新增片段覆盖文件,然后编辑

bash 复制代码
sudo nano /etc/systemd/system/[email protected]/override.conf
相关推荐
珹洺14 分钟前
Jsp技术入门指南【九】详细讲解JSTL
java·linux·开发语言·前端·jsp
晚风_END1 小时前
node.js|环境部署|源码编译高版本的node.js
linux·服务器·数据库·node.js·编辑器·个人开发
鱼嘻2 小时前
指针----------C语言经典题目(2)
linux·c语言·开发语言·数据结构·算法
敲上瘾2 小时前
网络基础(协议,地址,OSI模型、Socket编程......)
linux·运维·服务器·网络·c++·websocket
nLif2 小时前
LicheeRV Nano 与Ubuntu官方risc-v 镜像混合
linux·ubuntu·risc-v
青春:一叶知秋2 小时前
【MySQL数据库】数据类型
linux·服务器·mysql
chian-ocean2 小时前
深入理解Linux中的线程控制:多线程编程的实战技巧
linux·运维·服务器
一只帆記3 小时前
Vim 编辑器的常用命令整理
linux·编辑器·vim
习惯就好zz6 小时前
ubuntu1804服务器开启ftp,局域网共享特定文件给匿名用户
ubuntu·ftp·anonymous