当前位置: 首页 > web >正文

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

前置文章:

  • windows通用网线连接ubuntu实现ssh登录、桌面控制、文件共享
  • Ubuntu挂载HDD迁移存储PostgreSQL数据

背景:

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

root@Pine-Tree:~# uptime -s
2025-04-19 09:52:24

查看PostgreSQL运行状态


root@Pine-Tree:~# sudo systemctl status postgresql@15-main
× postgresql@15-main.service - PostgreSQL Cluster 15-mainLoaded: loaded (/lib/systemd/system/postgresql@.service; enabled; vendor preset: enabled)Active: failed (Result: protocol) since Sat 2025-04-19 09:52:26 CST; 15min agoProcess: 700 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 15-main start (code=exited, status=1/FAILURE)CPU: 41ms419 09:52:26 Pine-Tree systemd[1]: Starting PostgreSQL Cluster 15-main...
419 09:52:26 Pine-Tree postgresql@15-main[700]: Error: /mnt/pgdata/main is not accessible or does not exist
419 09:52:26 Pine-Tree systemd[1]: postgresql@15-main.service: 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]: postgresql@15-main.service: Failed with result 'protocol'.
419 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配置
sudo mkdir -p /etc/systemd/system/postgresql@15-main.service.d
新增片段覆盖文件
sudo nano /etc/systemd/system/postgresql@15-main.service.d/override.conf
在打开的编辑器中添加以下内容
[Service]
ExecStartPre=/bin/sleep 5
保存并退出,然后重新加载systemd配置
sudo systemctl daemon-reload
重新启动验证
reboot
确认PostgreSQL运行状况

启动成功:

root@Pine-Tree:~# sudo systemctl status postgresql@15-main
● postgresql@15-main.service - PostgreSQL Cluster 15-mainLoaded: loaded (/lib/systemd/system/postgresql@.service; enabled; vendor preset: enabled)Drop-In: /etc/systemd/system/postgresql@15-main.service.d└─override.confActive: active (running) since Sat 2025-04-19 11:20:15 CST; 2min 14s agoProcess: 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启动时间:

root@Pine-Tree:~# uptime -s
2025-04-19 11:19:56

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

root@Pine-Tree:~# ps -eo pid,lstart,cmd | grep postgres | grep -v grep1446 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.conf1483 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
sudo nano /etc/systemd/system/postgresql@15-main.service.d/override.conf

配置调整为:

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

启动成功:

root@Pine-Tree:~# sudo systemctl status postgresql@15-main
● postgresql@15-main.service - PostgreSQL Cluster 15-mainLoaded: loaded (/lib/systemd/system/postgresql@.service; enabled; vendor preset: enabled)Drop-In: /etc/systemd/system/postgresql@15-main.service.d└─override.confActive: active (running) since Sat 2025-04-19 12:30:06 CST; 7min agoProcess: 1479 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 15-main start (code=exited, status=0/SUCCESS)Main PID: 1487 (postgres)

Ubuntu启动时间:

root@Pine-Tree:~# uptime -s
2025-04-19 12:29:47

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

 root@Pine-Tree:~# sudo journalctl -u postgresql@15-main --no-pager -n 50-- Boot 0ba0937613c14ba8b47c6bb17de28bcd --
419 12:29:50 Pine-Tree systemd[1]: Starting PostgreSQL Cluster 15-main...
419 12:29:50 Pine-Tree postgresql@15-main[794]: Error: /mnt/pgdata/main is not accessible or does not exist
419 12:29:50 Pine-Tree systemd[1]: postgresql@15-main.service: 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]: postgresql@15-main.service: Failed with result 'protocol'.
419 12:29:50 Pine-Tree systemd[1]: Failed to start PostgreSQL Cluster 15-main.
419 12:30:00 Pine-Tree systemd[1]: postgresql@15-main.service: Scheduled restart job, restart counter is at 1.
419 12:30:00 Pine-Tree systemd[1]: Stopped PostgreSQL Cluster 15-main.
419 12:30:00 Pine-Tree systemd[1]: Starting PostgreSQL Cluster 15-main...
419 12:30:06 Pine-Tree systemd[1]: Started PostgreSQL Cluster 15-main.

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

修改override.conf后重新验证

sudo nano /etc/systemd/system/postgresql@15-main.service.d/override.conf

配置调整为:

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

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

sudo systemctl daemon-reload

问题汇总

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

root@Pine-Tree:~# sudo systemctl edit postgresql@15-main
Editing "/etc/systemd/system/postgresql@15-main.service.d/override.conf" canceled: temporary file is empty.

解决措施:
创建文件夹用于systemctl edit配置

sudo mkdir -p /etc/systemd/system/postgresql@15-main.service.d

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

sudo nano /etc/systemd/system/postgresql@15-main.service.d/override.conf
http://www.xdnf.cn/news/431.html

相关文章:

  • 动态规划经典例题:最长单调递增子序列、完全背包、二维背包、数字三角形硬币找零
  • Linux Privilege Escalation: LD_PRELOAD
  • 实战设计模式之备忘录模式
  • Python爬虫实战:获取B站查询数据
  • 【T型三电平仿真】SVPWM调制
  • stack和queue的使用和模拟实现
  • 【Linux】线程ID、线程管理、与线程互斥
  • 【Hot100】 73. 矩阵置零
  • 红帽RHEL与国产Linux系统对比:技术、生态与自主可控的博弈
  • 深入理解 Java 多线程:锁策略与线程安全
  • uniapp-x 二维码生成
  • AI速读 Seed-Thinking-v1.5:大模型推理的新飞跃
  • 从零开始学A2A五:A2A 协议的安全性与多模态支持
  • 利用 Deepseek 和 Mermaid 画流程图
  • Linux教程-常用命令系列二
  • 【SAP ME 45】并发SFC拆分导致 SFC_STEP中的QTY_IN_QUEUE与SFC表中的QTY不一致
  • React Article模块
  • 深入解析NotaGen:5亿参数+三阶段训练,解锁高质量AI音乐生成
  • 【大模型框架】LLAMA-FACTORY使用总结
  • 6547网:2025年3月 Python编程等级考试一级真题试卷
  • java浮点数运算判断
  • ESP-ADF外设子系统深度解析:esp_peripherals组件架构与核心设计(显示输出类外设之LCD)
  • 致远OA——自定义开发rest接口
  • Android开发四大组件和生命周期及setFlags
  • 触发器(详解)
  • jmeter利用csv进行参数化和自动断言
  • C算术运算符 printf输出格式 字符指针打印输出 使用scanf函数进行输入
  • ReSearch:基于强化学习的大语言模型推理搜索框架
  • CCLinkIE转EtherCAT边缘计算网关构建智能产线:跨协议设备动态组网与数据优化传输
  • 【机器学习-周总结】-第4周